use std::fmt; use miette::Diagnostic; use knuffel::{Decode, span::Span}; #[derive(Debug, Decode, PartialEq)] struct Unit; #[derive(Debug, Decode, PartialEq)] struct Arg(#[knuffel(argument)] u32); #[derive(Debug, Decode, PartialEq)] struct Opt(Option); #[derive(Debug, Decode, PartialEq)] struct Extra(#[knuffel(argument)] Option, u32); #[derive(Debug, Decode, PartialEq)] enum Enum { Unit, Arg(#[knuffel(argument)] u32), Opt(Option), Extra(#[knuffel(argument)] Option, u32), } fn parse>(text: &str) -> T { let mut nodes: Vec = knuffel::parse("", text).unwrap(); assert_eq!(nodes.len(), 1); nodes.remove(0) } fn parse_err+fmt::Debug>(text: &str) -> String { let err = knuffel::parse::>("", text).unwrap_err(); err.related().unwrap() .map(|e| e.to_string()).collect::>() .join("\n") } #[test] fn parse_unit() { assert_eq!(parse::(r#"node"#), Unit); assert_eq!(parse_err::(r#"node something="world""#), "unexpected property `something`"); } #[test] fn parse_arg() { assert_eq!(parse::(r#"node 123"#), Arg(123)); assert_eq!(parse_err::(r#"node something="world""#), "additional argument is required"); } #[test] fn parse_extra() { assert_eq!(parse::(r#"node "123""#), Extra(Some("123".into()), 0)); assert_eq!(parse::(r#"node"#), Extra(None, 0)); assert_eq!(parse_err::(r#"node "123" 456"#), "unexpected argument"); } #[test] fn parse_opt() { assert_eq!(parse::(r#"node 123"#), Opt(Some(Arg(123)))); assert_eq!(parse::(r#"node"#), Opt(None)); assert_eq!(parse_err::(r#"node something="world""#), "additional argument is required"); } #[test] fn parse_enum() { assert_eq!(parse::(r#"unit"#), Enum::Unit); assert_eq!(parse::(r#"arg 123"#), Enum::Arg(123)); assert_eq!(parse::(r#"opt 123"#), Enum::Opt(Some(Arg(123)))); assert_eq!(parse::(r#"opt"#), Enum::Opt(None)); assert_eq!(parse::(r#"extra"#), Enum::Extra(None, 0)); assert_eq!(parse_err::(r#"unit something="world""#), "unexpected property `something`"); assert_eq!(parse_err::(r#"other something="world""#), "expected `unit`, `arg`, or one of 2 others"); assert_eq!(parse_err::(r#"extra "hello" "world""#), "unexpected argument"); }