Skip to content

Supporting user-defined string parsing #212

@bheisler

Description

@bheisler

uom's Quantity::from_str implementation is quite strict. It requires a number, a space and then an abbreviated unit. It does not support strings like "100g" or even "100 grams", even though these are natural ways for humans to write measurements.

I have implemented my own layer on top of uom's from_str to relax the requirement for a space, but there is no way for me to make it work for full units without manually listing all of the units in my code because uom doesn't seem to provide any functionality to work with full unit strings rather than abbreviations.

Could uom provide a function like Mass::from_str_unit(100.0f64, "grams") -> Result<...> (which would also accept "gram" and "g", but also "pounds" and "kg" and so on)? From my perspective, this would actually be better than changing from_str, as it makes it possible for me to implement my own human-friendly parsing code without needing to incorporate my opinions about human-friendly parsing into uom itself.

If this is not practical, could the from_str implementation be extended to allow the singular and plural units? Right now my human-friendly parsing layer works by creating a temporary string which does fit uom's desired format and then asking uom to parse that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions