How to use whippyunits with strings as unit expression?

I would like to save derived SI units, such as ohm, m/s or m/s^2 as string in a database together with a f64 value and found the crate whippyunits which at least seems like it could be capable of doing so. Unfortunately I cannot wrap my head around how to do that. In the documentation the unit expression always seems to be a TokenStream (I believe from the proc_macro2 crate?). How can I transform a string to that or is there something else? I am becoming desperate not knowing how to being able to do that. In the end I would like to do something like the whippyunits::quantity!(500.0, mm) (quantity in whippyunits - Rust) does just with the second argument (the unit_expr) being a string. Any ideas or suggestions?

See the deserialization example.

Note that for compile-time unit safety, there is no way to avoid specifying a unit literal at compile time, so you cannot fully replace the token streams with strings. You can deserialize from string, but the type you're deserializing into must be known at compile time. The library is zero-cost; at runtime, it's a regular old numeric type.

First, thanks for your reply!

That seems to be exactly my problem, I do not want to have to match the string to figure out which unit_expr has to be used as there are just to many possibilities, especially when supporting all of them. So what I want to do is just not possible with whippyunits, at least for now.

I will probably have to investigate whether and if yes how this can be accomplished with whippyunits.

If you want the dimension-safety to depend on the runtime value of the string, this cannot be done with any zero-cost units library (eg WhippyUnits or uom). Since errors that guarantee unit safety are generated at compile-time, the compiler has to be able to resolve the dimensions of the variable before any code runs.

If your code is querying a database at runtime, then the value of the string doesn't exist until runtime. So, the compiler cannot use that value to determine the concrete Quantity type, which must be known at compile time.

You either need a runtime units-of-measure library (which will have corresponding performance cost for your program - this may or may not matter to you, depending on application - and will generate runtime errors instead of compilation errors only when a dimensionality violation is encountered in practice), or you need to code-generate all the paths from your database (rust offers some very convenient ways to do this w/ proc macros). There's nothing in-between.

Note that WhippyUnits supports dimension-generic programming, which may help you if you decide to go the code-generation path. But, while most implementations can be made generic across arbitrary dimensions, you still will have to concretely and separately invoke those implementations for each actual unit your code wishes to interact with (or else manually bypass unit-safety).