Skip to content

Simplify .open() API with enums #25

@barjin

Description

@barjin

As the docblock says, the id, name, and alias options are mutually exclusive.

/// At most one of `id`, `name`, or `alias` may be provided.
pub async fn open(
id: Option<String>,
name: Option<String>,
alias: Option<String>,

Rust has sum types (called enums) that might help enforce this on the type level.

enum Identifier {
    Id(String),
    Name(String),
    Alias(String),
}

fn open(id: Identifier) {
    match id {
        Identifier::Id(s) => /// open with id
        Identifier::Name(s) => /// open with name
        Identifier::Alias(s) => /// open with alias
    }
}

open(Identifier::Alias('abcd'.to_string()))

The default storage access can be either implemented with a new explicit enum member (Identifier::Default), or id: Option<Identifier> (with the None variant opening the default storage).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions