#[macro_use]
extern crate log;
extern crate toml;
extern crate shareable;
#[macro_use]
mod test_macros;
mod data;
mod value;
mod issues;
mod item_def;
mod item_value;
mod enums;
mod notify;
mod toml_builder;
mod toml_data;
mod toml_def;
mod toml_iter;
pub use toml_data::TomlData;
pub use toml_def::TomlDef;
pub use data::{ItemDuration, ItemStr, ItemU16, ItemUsize};
pub use issues::Issues;
pub use enums::{Error, FormatDesc, Warning, ValidationError};
#[cfg(test)]
mod tests {
use super::{TomlDef, ItemDuration, ItemStr, ItemU16, ItemUsize};
#[test]
fn simple() {
let file = r#"
delay = "3d 4h 5m 6s 7ms"
name = "foo"
short = 16
large = 123456789
"#;
let data = TomlDef::new()
.add(ItemDuration::with_name("delay").min("6h").max("7d"))
.add(ItemStr::with_name("name").min(1).max(16))
.add(ItemU16::with_name("short").min(6).max(41))
.add(ItemUsize::with_name("large").min(3))
.parse_toml(file).unwrap();
assert_eq!(data.get_duration("delay"), 3 * 24 * 60 * 60 * 1000 + 4 * 60 * 60 * 1000 + 5 * 60 * 1000 + 6 * 1000 + 7);
assert_eq!(data.get_str("name"), "foo");
assert_eq!(data.get_u16("short"), 16);
assert_eq!(data.get_usize("large"), 123456789);
}
#[test]
fn complex() {
let file = r#"
delay = "3d 4h 5m 6s 7ms"
name = "foo"
short = 16
large = 123456789
[dup]
delay = "7d 6h 5m 4s 3ms"
name = "bar"
short = 89
large = 987654321
[other]
test = "testing"
"#;
let data = TomlDef::new()
.add(ItemDuration::with_name("delay").min("6h").max("7d"))
.add(ItemStr::with_name("name").min(1).max(16))
.add(ItemU16::with_name("short").min(6).max(41))
.add(ItemUsize::with_name("large").max(200000000))
.add(TomlDef::with_name("dup")
.add(ItemDuration::with_name("delay").min("4d").max("8d"))
.add(ItemStr::with_name("name").min(1).max(16))
.add(ItemU16::with_name("short").min(22).max(100))
.add(ItemUsize::with_name("large").min(300000000)))
.add(TomlDef::with_name("other")
.add(ItemStr::with_name("test").min(1).max(16)))
.parse_toml(file).unwrap();
assert_eq!(data.get_duration("delay"), 3 * 24 * 60 * 60 * 1000 + 4 * 60 * 60 * 1000 + 5 * 60 * 1000 + 6 * 1000 + 7);
assert_eq!(data.get_str("name"), "foo");
assert_eq!(data.get_u16("short"), 16);
assert_eq!(data.get_usize("large"), 123456789);
assert_eq!(data.get_duration("dup.delay"), 7 * 24 * 60 * 60 * 1000 + 6 * 60 * 60 * 1000 + 5 * 60 * 1000 + 4 * 1000 + 3);
assert_eq!(data.get_str("dup.name"), "bar");
assert_eq!(data.get_u16("dup.short"), 89);
assert_eq!(data.get_usize("dup.large"), 987654321);
assert_eq!(data.get_str("other.test"), "testing");
}
#[test]
fn optional_missing() {
let file = r#"
delay = "3d 4h 5m 6s 7ms"
short = 16
large = 123456789
[dup]
delay = "7d 6h 5m 4s 3ms"
name = "bar"
short = 89
"#;
let data = TomlDef::new()
.add(ItemDuration::with_name("delay").min("6h").max("7d"))
.add(ItemStr::with_name("name").min(1).max(16).optional())
.add(ItemU16::with_name("short").min(6).max(41))
.add(ItemUsize::with_name("large").max(200000000))
.add(TomlDef::with_name("dup")
.add(ItemDuration::with_name("delay").min("4d").max("8d"))
.add(ItemStr::with_name("name").min(1).max(16))
.add(ItemU16::with_name("short").min(22).max(100))
.add(ItemUsize::with_name("large").min(300000000).optional()))
.add(TomlDef::with_name("other").optional()
.add(ItemStr::with_name("test").min(1).max(16)))
.parse_toml(file).unwrap();
assert_eq!(data.get_duration("delay"), 3 * 24 * 60 * 60 * 1000 + 4 * 60 * 60 * 1000 + 5 * 60 * 1000 + 6 * 1000 + 7);
assert !(data.optional_str("name").is_none());
assert_eq!(data.get_u16("short"), 16);
assert_eq!(data.get_usize("large"), 123456789);
assert_eq!(data.get_duration("dup.delay"), 7 * 24 * 60 * 60 * 1000 + 6 * 60 * 60 * 1000 + 5 * 60 * 1000 + 4 * 1000 + 3);
assert_eq!(data.get_str("dup.name"), "bar");
assert_eq!(data.get_u16("dup.short"), 89);
assert !(data.optional_usize("dup.large").is_none());
assert !(data.optional_str("other.test").is_none());
}
#[test]
fn optional_exist() {
let file = r#"
delay = "3d 4h 5m 6s 7ms"
name = "foo"
short = 16
large = 123456789
[dup]
delay = "7d 6h 5m 4s 3ms"
name = "bar"
short = 89
large = 987654321
[other]
test = "testing"
"#;
let data = TomlDef::new()
.add(ItemDuration::with_name("delay").min("6h").max("7d"))
.add(ItemStr::with_name("name").min(1).max(16).optional())
.add(ItemU16::with_name("short").min(6).max(41))
.add(ItemUsize::with_name("large").max(200000000))
.add(TomlDef::with_name("dup")
.add(ItemDuration::with_name("delay").min("4d").max("8d"))
.add(ItemStr::with_name("name").min(1).max(16))
.add(ItemU16::with_name("short").min(22).max(100))
.add(ItemUsize::with_name("large").min(300000000).optional()))
.add(TomlDef::with_name("other").optional()
.add(ItemStr::with_name("test").min(1).max(16)))
.parse_toml(file).unwrap();
assert_eq!(data.get_duration("delay"), 3 * 24 * 60 * 60 * 1000 + 4 * 60 * 60 * 1000 + 5 * 60 * 1000 + 6 * 1000 + 7);
assert_eq!(data.optional_str("name").unwrap(), "foo");
assert_eq!(data.get_u16("short"), 16);
assert_eq!(data.get_usize("large"), 123456789);
assert_eq!(data.get_duration("dup.delay"), 7 * 24 * 60 * 60 * 1000 + 6 * 60 * 60 * 1000 + 5 * 60 * 1000 + 4 * 1000 + 3);
assert_eq!(data.get_str("dup.name"), "bar");
assert_eq!(data.get_u16("dup.short"), 89);
assert_eq!(data.optional_usize("dup.large").unwrap(), 987654321);
assert_eq!(data.optional_str("other.test").unwrap(), "testing");
}
#[test]
fn default() {
let file = r#"
delay = "3d 4h 5m 6s 7ms"
short = 16
large = 123456789
[dup]
delay = "7d 6h 5m 4s 3ms"
name = "bar"
short = 89
"#;
let data = TomlDef::new()
.add(ItemDuration::with_name("delay").min("6h").max("7d"))
.add(ItemStr::with_name("name").min(1).max(16).default("foo"))
.add(ItemU16::with_name("short").min(6).max(41))
.add(ItemUsize::with_name("large").max(200000000))
.add(TomlDef::with_name("dup")
.add(ItemDuration::with_name("delay").min("4d").max("8d"))
.add(ItemStr::with_name("name").min(1).max(16))
.add(ItemU16::with_name("short").min(22).max(100))
.add(ItemUsize::with_name("large").min(300000000).default(987654321)))
.add(TomlDef::with_name("other").optional()
.add(ItemStr::with_name("test").min(1).max(16).default("testing")))
.parse_toml(file).unwrap();
assert_eq!(data.get_duration("delay"), 3 * 24 * 60 * 60 * 1000 + 4 * 60 * 60 * 1000 + 5 * 60 * 1000 + 6 * 1000 + 7);
assert_eq!(data.get_str("name"), "foo");
assert_eq!(data.get_u16("short"), 16);
assert_eq!(data.get_usize("large"), 123456789);
assert_eq!(data.get_duration("dup.delay"), 7 * 24 * 60 * 60 * 1000 + 6 * 60 * 60 * 1000 + 5 * 60 * 1000 + 4 * 1000 + 3);
assert_eq!(data.get_str("dup.name"), "bar");
assert_eq!(data.get_u16("dup.short"), 89);
assert_eq!(data.get_usize("dup.large"), 987654321);
assert!(data.optional_str("other.test").is_none());
}
}