Skip to content

Feature request: Expose Unit enum as pub #112

@Sheape

Description

@Sheape

Parsing unit from string is already implemented but the Unit enum isn't exposed as pub. I'm not sure if there any implications by simply changing it to pub aside from adding explicit documentation. Anyways, this is the actual missing piece to resolve nushell/nushell#15007. Feel free to close this issue if this is unreasonable or if it's not the goal of the project 😊
Otherwise, I can open a PR about this.

bytesize/src/parse.rs

Lines 162 to 185 in ac756bb

impl str::FromStr for Unit {
type Err = String;
fn from_str(unit: &str) -> Result<Self, Self::Err> {
match unit.to_lowercase().as_str() {
"b" => Ok(Self::Byte),
// power of tens
"k" | "kb" => Ok(Self::KiloByte),
"m" | "mb" => Ok(Self::MegaByte),
"g" | "gb" => Ok(Self::GigaByte),
"t" | "tb" => Ok(Self::TeraByte),
"p" | "pb" => Ok(Self::PetaByte),
"e" | "eb" => Ok(Self::ExaByte),
// power of twos
"ki" | "kib" => Ok(Self::KibiByte),
"mi" | "mib" => Ok(Self::MebiByte),
"gi" | "gib" => Ok(Self::GibiByte),
"ti" | "tib" => Ok(Self::TebiByte),
"pi" | "pib" => Ok(Self::PebiByte),
"ei" | "eib" => Ok(Self::ExbiByte),
_ => Err(format!("couldn't parse unit of {unit:?}")),
}
}
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions