Comments (3)
Hello @ada-x64, if you are interested, I'd be happy to accept a PR for this feature. It seems super useful. There are quite a few corner cases we need to have figured out though:
- What should
AsRefStr
andIntoStaticStr
do when you provide a templatized format? My gut feeling is that they should be disallowed and you can't use this feature with that derive simultaneously, but maybe we can find a better solution. - How should this work with various casing features? The inner values probably won't be affected by the case modifiers, but can we respect the casing for the rest of the parse string.
- Can it work for Tuple like sum types as well?
- What should happen if you use some of the values, but not all of them? Right now, they are
Default::default
, and that probably makes sense to continue doing? - What happens if the template references the same argument in multiple places, but when we go to parse them with
FromString
, they are different values?
from strum.
@Peternator7 I had a very quick look into display
macro I dont exactly understand whats going on with arms without actually logging a couple of things but it seems you are calling formatter.pad
on the value.
thiserror
crate seems to be doing this parsing it seems the same use case.
Do you think if it is possible to use write!
instead of pad
? Reading the docs it seems like padding is handled automatically by format_args!
which means even if you dont call pad
it will still get padded by the user-provided format!
or println!
. Am I missing something here?
By this logic it just seems we could check for existence of {} and not {{?}} and prepare a write!
able format string.
So to answer your questions
- this is for
Display
only which returns an owned value anyway or perhaps for this particular case a new attribute (fserialize
perhaps?) is in order soDisplay
andIntoStaticStr
,AsRefStr
can be used together. - as you mentioned casing is not really applicable here but if strongly desired another
format!
could be used and the result could be cased at the cost of creating another intermediary string - since tuple like variants are simply a comma separated list as long as those types implement
std::fmt::Display
index based access would just work - I dont quite understand why it would make any difference whether to use all of them or not probably i am missing something here and what does formatting do anything with
Default
since the values are already there EnumString
always felt weird anyway I think it should have had adeserialize
option. Since many people are used to the lingo from serde anyway it would be more intuitive and a design improvement i believe.
To summarise \
- serialize could stay as is and for static generation s_serialize could be introduce or the other way around f_serialize for formatted serialize
- for deserialization it should in my opinion really be called
deserialize
usingserialize
key for deserialization is confusing - accessing sub elements could be done with very minimal parsing of the provided string (it could be as simple as get raw string, push things to a vec in order so they can be passed as arguments to format in correct order.
- For struct like variants indexing could be based on names and a map holding
ident -> value
and again push them to e vec so they can be passed to format in correct order would do the trick and since this is compile time generated code there would be no runtime penalty for doing so
from strum.
I think a problem might be ambiguous when parsing,
#[strum(serialize = "B: ({a}, {b})")]
B {
a: String,
b: i32,
},
how would B
accept string like B { a: String::from("1, 2"), c: 3}
, which gives string B: (1, 2, 3)
?
one possible solution is report failure when is happens,
replacing all slots to regex B: \((.*?), (.*?)\)
, match the string and parse inner capture to corresponding fields.
in the way B: (1, 2, 3)
would capture a
with "1"
, and b
with "2, 3"
, then "2, 3".parse::<i32>()
would failed.
from strum.
Related Issues (20)
- derive(FromRepr) broken in 0.25.0 HOT 13
- Unwrap methods HOT 6
- strum(serialize_all vs serde(rename_all HOT 1
- rustfmt::skip is broken by derive(FromRepr) HOT 1
- Maybe EnumCount should respect `#[repr(...)]` like FromRepr HOT 2
- EnumDiscriminants should inherit the repr of the enum they are derived from HOT 1
- `could not find fmt in core` when upgrading to 0.25 HOT 1
- Proposal: Exclude macro for EnumIter macro
- IntoStaticStr but from immutable reference to an enum From<&MyEnum> HOT 7
- `EnumString` match with non-default variables HOT 1
- Feature request: A function to convert to integer by variant HOT 1
- EnumIter does not work for enums named 'Option'
- [Feature] Macro to Implement PartialEq<str> HOT 1
- strum::Display doesn't work with non-unit non-string variants
- `EnumString` should derive `From<&str>` instead of `TryFrom<&str>` when `#[strum(default)]` is present HOT 1
- to_string and serialize when used together causes unreachable code HOT 2
- `get_bool` and `get_int` in `EnumProperty` are undocumented and always return None
- AsRef<str> and default variants
- EnumDiscriminants: can't infer type HOT 3
- Getting the discriminant from an enum variant HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from strum.