Skip to content

Commit

Permalink
impl FromMeta for PathBuf
Browse files Browse the repository at this point in the history
  • Loading branch information
TedDriggs committed Nov 17, 2023
1 parent 12285c0 commit 0c79e55
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Accept bare paths in `#[darling(default = ...)]` [#258](https://github.com/TedDriggs/darling/pull/258)
- Add `FromMeta` impl for `PathBuf` [#259](https://github.com/TedDriggs/darling/pull/259)

## v0.20.3 (July 12, 2023)

Expand Down
14 changes: 14 additions & 0 deletions core/src/from_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ impl FromMeta for String {
}
}

impl FromMeta for std::path::PathBuf {
fn from_string(s: &str) -> Result<Self> {
Ok(s.into())
}
}

/// Generate an impl of `FromMeta` that will accept strings which parse to numbers or
/// integer literals.
macro_rules! from_meta_num {
Expand Down Expand Up @@ -795,6 +801,14 @@ mod tests {
assert_eq!(&fm::<String>(quote!(ignore = r#"world"#)), "world");
}

#[test]
fn pathbuf_succeeds() {
assert_eq!(
fm::<std::path::PathBuf>(quote!(ignore = r#"C:\"#)),
std::path::PathBuf::from(r#"C:\"#)
);
}

#[test]
#[allow(clippy::float_cmp)] // we want exact equality
fn number_succeeds() {
Expand Down

0 comments on commit 0c79e55

Please sign in to comment.