-
Notifications
You must be signed in to change notification settings - Fork 68
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
Allow usage of map
to handle fields that don't impl FromMeta
#256
Comments
just to be clear, the following does not compile using darling v0.20.3 use std::path::PathBuf;
use darling::FromDeriveInput;
#[derive(Debug, FromDeriveInput)]
#[darling(attributes(my_attribute))]
struct Args {
#[darling(map = PathBuf::from)]
path: PathBuf,
} and neither does this use std::path::PathBuf;
use darling::FromDeriveInput;
#[derive(Debug, FromDeriveInput)]
#[darling(attributes(my_attribute))]
struct Args {
#[darling(map = "parse_path_from_string")]
path: PathBuf,
}
fn parse_path_from_string(s: String) -> PathBuf {
PathBuf::from(s)
} |
The issue you're running into is what happens when the caller doesn't specify There are a couple options here:
Here's an example using #[derive(Debug, FromDeriveInput)]
#[darling(attributes(my_attribute))]
struct Args {
#[darling(map = parse_path_from_string, default)]
path: Option<PathBuf>,
}
fn parse_path_from_string(s: String) -> Option<PathBuf> {
Some(PathBuf::from(s))
} |
Thanks for taking the time, I really appreciate it. Wrapping the type in #259 looks like it would solve my problem! I have a similar issue where I need to parse a comma-separated list of strings into a vec of strings. use darling::FromDeriveInput;
#[derive(Debug, FromDeriveInput)]
#[darling(attributes(my_attribute))]
struct Args {
#[darling(map = parse_strings)]
strings: Vec<String>,
}
fn parse_strings(s: String) -> Vec<String> {
todo!()
} this doesn't work either, i'm guessing for similar reasons. Is there a way to make this work without wrapping the |
this seems to work, and i think it meets my use-case: use darling::FromDeriveInput;
#[derive(Debug, FromDeriveInput)]
#[darling(attributes(my_attribute))]
struct Args {
#[darling(map = parse_strings, default)]
strings: Vec<String>,
}
fn parse_strings(s: String) -> Vec<String> {
vec![]
} though i admit i don't fully understand why adding |
It's because of this line, which governs how Having a default expression, whether explicit or implicit, causes that piece not to be emitted because we will always have a value for the field. I deliberately don't want the I suppose one alternative would be to allow opting out of the |
map
to handle fields that don't impl FromMeta
i may be misunderstanding the use of the 'mapping' feature
I'm attempting to parse a PathBuf from a derive input. something like-
to parse-
is this how 'map' is supposed to be used?
Same question for parsing a comma-separated list into a
Vec<String>
The text was updated successfully, but these errors were encountered: