Crates.io | strflags |
lib.rs | strflags |
version | 0.3.1 |
source | src |
created_at | 2023-09-18 14:02:59.377406 |
updated_at | 2023-09-26 06:34:52.133276 |
description | A string-enum and string-flags with fixed variants that can also accept arbitrary data. |
homepage | |
repository | https://github.com/mintlu8/strflags |
max_upload_size | |
id | 975942 |
size | 34,107 |
A string-enum and string-flags with fixed variants that can also accept arbitrary data.
This is more extensible than traditional enums, bitflags or enumset,
while being more ergonomic and more typo-resiliant than a set of strings like HashSet<String>
.
str_flags! {
#[derive(PartialOrd)]
pub Color: [
Red,
Green,
DarkBlue,
]
}
pub struct Color(..)
And consts:
pub const Red: Color = "red";
pub const Green: Color = "green";
pub const DarkBlue: Color = "darkblue";
Debug
, Clone
, Eq
, Hash
Display
FromStr
Borrow<str>
AsRef<str>
PartialEq<impl AsRef<str>>
Into<Flags<Self>>
BitOr<Output = Flags<Self>>
Serialize
, with the serde
feature enabledDeserialize
, with the serde
feature enabledYou can create a Flags<Color>
like this.
str_enum!()
use the str_enum!()
macro to opt out of flags related features.
let flags = Color::Red | Color::Green | Color::new("Yellow");
We stores all data in flatlowercase
to avoid case mismatches and some typos.
We use a string to serialize our "string enums".
e.g. "red"
We use a csv like string to serialize our Flags
e.g. "dog|cat|giraffe"
This ensures converting from enum to Flags
does not break serialization formats.
debug
featureAllowing any string to be an enum variant is obviously prone to typos.
When the debug feature is enabled, we performs a fuzzy string check
every time a comparison is made and emit a warn!
though the log
crate
if similar strings are found.
This is obviously slow so be careful when using this feature.
Currently we use EcoString
which can only inline 15 bytes. Having a larger compile time constant string
is a runtime error if the constant is USED.
A proc macro implementation in the future will address this issue.