-
Notifications
You must be signed in to change notification settings - Fork 241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add trimming option for the de::from_* functions #561
base: master
Are you sure you want to change the base?
Conversation
There is a lot of changes because I changed the |
I prefer to not change the creation functions and instead provide the get / set functions for config parameters (or the whole config struct). I think, that is is better to not change the |
I understand the point, but I've tried to do it like that, which is not satisfying. There is no way to do a global config variable that can be changed and used by And the main problem with this version is that each |
86cbc33
to
b4355c6
Compare
Ok well, I used what you did in #572 and what you said here #285 (comment), I think it's a great idea to enable/disable trimming per Struct or per field to have more control. #[serde(no_trim)]
struct Foo {
#[serde(no_trim)]
elem: String,
} And the default value of this is to trim content to keep compatibility. |
Additional attribute would be great to have. Unfortunately, serde does not provide a way to attach attributes, so we need to encode everything in the name, like we do that for the attribute indicator ( #[xml] // runs first and could rewrite #[xml] helpers to #[serde(rename = "-field")] for example
#[derive(Deserialize)]
struct Xml {
#[xml(no_trim)] // helper attribute for #[xml] on struct
field: String,
} But this is advanced usage which, I think, you shouldn't care here about. This change is better to make in the separate PR. While working on #574 I've found this: https://www.w3.org/TR/xmlschema11-2/#built-in-primitive-datatypes This means, that We could also add a flag "trim / not trim" to the |
Reference #520 (comment).
So I did the feature in ONE way possible because there are many different ways this can be implemented.
I choose to modify the current functions for this to work but this can change easily and I will explain the other alternatives I've thought about at the end. What I really wanted to discuss here is if the logic of extending the
StartTrimmer
to a generic start and endTrimmer
is fine? And if giving to all the differentReader
is also good? I choose a rather generic and extensible solution. Just want to know if you don't see it made like that or if this is rather fine.For the different way of expressing the config parameter, I've chosen to add a parameter to
from_str
but its breaks the current API so it might not be the best solution. I thought of 2 other ways of doing this.The first one: add another function
from_str_option
that will do the exact same as the currentfrom_str
but will take the extra parameter, and the oldfrom_str
will just callfrom_str_option
with a default Trimmer.The second option: Transform
from_str
to a macro to be able to have theTrimmer
parameter optional, and if not given construct a default one.When I talk about
from_str
it is the high-level one not the inner function ofDeserializer