Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ fn default_value(ty: String) -> String {
.to_string()
}

/// return type and unwrap with Option and Vec; or return the value type of HashMap and BTreeMap
/// return type and unwrap with Option, Vec, HashSet, BTreeSet; or return the value type of HashMap and BTreeMap
fn parse_type(
ty: &Type,
default: &mut String,
Expand All @@ -194,7 +194,7 @@ fn parse_type(
r#type = parse_type(ty, default, &mut false, nesting_format);
}
}
} else if id == "Vec" {
} else if id == "Vec" || id == "HashSet" || id == "BTreeSet" {
if nesting_format.is_some() {
*nesting_format = Some(NestingFormat::Section(NestingType::Vec));
}
Expand Down
30 changes: 30 additions & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,36 @@ c = [ 0, ]
# Config.d
# d = [ 0, ]

"#
);
assert!(toml::from_str::<Config>(&Config::toml_example()).is_ok())
}

#[test]
fn hashset_btreeset() {
use std::collections::{BTreeSet, HashSet};

#[derive(TomlExample, Deserialize, Default, PartialEq, Debug)]
#[allow(dead_code)]
struct Config {
/// Config.a is a set of string
a: HashSet<String>,
/// Config.b is a set of number
b: BTreeSet<usize>,
/// Config.c is optional
c: Option<HashSet<String>>,
}
assert_eq!(
Config::toml_example(),
r#"# Config.a is a set of string
a = [ "", ]

# Config.b is a set of number
b = [ 0, ]

# Config.c is optional
# c = [ "", ]

"#
);
assert!(toml::from_str::<Config>(&Config::toml_example()).is_ok())
Expand Down