Support the new 0.5 syntax in the toml! macro.

- Space in datetime.
- +- inf, nan, 0.0
- Dotted keys.
This commit is contained in:
Eric Huss 2018-07-27 11:35:05 -07:00
parent 22f00d37bf
commit 36a5318feb
2 changed files with 214 additions and 46 deletions

View file

@ -80,54 +80,75 @@ macro_rules! toml_internal {
(@toplevel $root:ident [$($path:tt)*]) => {}; (@toplevel $root:ident [$($path:tt)*]) => {};
// Parse negative number `key = -value`. // Parse negative number `key = -value`.
(@toplevel $root:ident [$($path:tt)*] $($k:tt)-+ = - $v:tt $($rest:tt)*) => { (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = - $v:tt $($rest:tt)*) => {
toml_internal!(@toplevel $root [$($path)*] $($k)-+ = (-$v) $($rest)*); toml_internal!(@toplevel $root [$($path)*] $($($k)-+).+ = (-$v) $($rest)*);
};
// Parse positive number `key = +value`.
(@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = + $v:tt $($rest:tt)*) => {
toml_internal!(@toplevel $root [$($path)*] $($($k)-+).+ = ($v) $($rest)*);
}; };
// Parse offset datetime `key = 1979-05-27T00:32:00.999999-07:00`. // Parse offset datetime `key = 1979-05-27T00:32:00.999999-07:00`.
(@toplevel $root:ident [$($path:tt)*] $($k:tt)-+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt - $tzh:tt : $tzm:tt $($rest:tt)*) => { (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt - $tzh:tt : $tzm:tt $($rest:tt)*) => {
toml_internal!(@topleveldatetime $root [$($path)*] $($k)-+ = ($yr - $mo - $dhr : $min : $sec . $frac - $tzh : $tzm) $($rest)*); toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec . $frac - $tzh : $tzm) $($rest)*);
};
// Space instead of T.
(@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt . $frac:tt - $tzh:tt : $tzm:tt $($rest:tt)*) => {
toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec . $frac - $tzh : $tzm) $($rest)*);
}; };
// Parse offset datetime `key = 1979-05-27T00:32:00-07:00`. // Parse offset datetime `key = 1979-05-27T00:32:00-07:00`.
(@toplevel $root:ident [$($path:tt)*] $($k:tt)-+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt - $tzh:tt : $tzm:tt $($rest:tt)*) => { (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt - $tzh:tt : $tzm:tt $($rest:tt)*) => {
toml_internal!(@topleveldatetime $root [$($path)*] $($k)-+ = ($yr - $mo - $dhr : $min : $sec - $tzh : $tzm) $($rest)*); toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec - $tzh : $tzm) $($rest)*);
};
// Space instead of T.
(@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt - $tzh:tt : $tzm:tt $($rest:tt)*) => {
toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec - $tzh : $tzm) $($rest)*);
}; };
// Parse local datetime `key = 1979-05-27T00:32:00.999999`. // Parse local datetime `key = 1979-05-27T00:32:00.999999`.
(@toplevel $root:ident [$($path:tt)*] $($k:tt)-+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt $($rest:tt)*) => { (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt $($rest:tt)*) => {
toml_internal!(@topleveldatetime $root [$($path)*] $($k)-+ = ($yr - $mo - $dhr : $min : $sec . $frac) $($rest)*); toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec . $frac) $($rest)*);
};
// Space instead of T.
(@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt . $frac:tt $($rest:tt)*) => {
toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec . $frac) $($rest)*);
}; };
// Parse offset datetime `key = 1979-05-27T07:32:00Z` and local datetime `key = 1979-05-27T07:32:00`. // Parse offset datetime `key = 1979-05-27T07:32:00Z` and local datetime `key = 1979-05-27T07:32:00`.
(@toplevel $root:ident [$($path:tt)*] $($k:tt)-+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt $($rest:tt)*) => { (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt $($rest:tt)*) => {
toml_internal!(@topleveldatetime $root [$($path)*] $($k)-+ = ($yr - $mo - $dhr : $min : $sec) $($rest)*); toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec) $($rest)*);
};
// Space instead of T.
(@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt $($rest:tt)*) => {
toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec) $($rest)*);
}; };
// Parse local date `key = 1979-05-27`. // Parse local date `key = 1979-05-27`.
(@toplevel $root:ident [$($path:tt)*] $($k:tt)-+ = $yr:tt - $mo:tt - $day:tt $($rest:tt)*) => { (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $($rest:tt)*) => {
toml_internal!(@topleveldatetime $root [$($path)*] $($k)-+ = ($yr - $mo - $day) $($rest)*); toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $day) $($rest)*);
}; };
// Parse local time `key = 00:32:00.999999`. // Parse local time `key = 00:32:00.999999`.
(@toplevel $root:ident [$($path:tt)*] $($k:tt)-+ = $hr:tt : $min:tt : $sec:tt . $frac:tt $($rest:tt)*) => { (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $hr:tt : $min:tt : $sec:tt . $frac:tt $($rest:tt)*) => {
toml_internal!(@topleveldatetime $root [$($path)*] $($k)-+ = ($hr : $min : $sec . $frac) $($rest)*); toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($hr : $min : $sec . $frac) $($rest)*);
}; };
// Parse local time `key = 07:32:00`. // Parse local time `key = 07:32:00`.
(@toplevel $root:ident [$($path:tt)*] $($k:tt)-+ = $hr:tt : $min:tt : $sec:tt $($rest:tt)*) => { (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $hr:tt : $min:tt : $sec:tt $($rest:tt)*) => {
toml_internal!(@topleveldatetime $root [$($path)*] $($k)-+ = ($hr : $min : $sec) $($rest)*); toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($hr : $min : $sec) $($rest)*);
}; };
// Parse any other `key = value` including string, inline array, inline // Parse any other `key = value` including string, inline array, inline
// table, number, and boolean. // table, number, and boolean.
(@toplevel $root:ident [$($path:tt)*] $($k:tt)-+ = $v:tt $($rest:tt)*) => { (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $v:tt $($rest:tt)*) => {{
$crate::macros::insert_toml( $crate::macros::insert_toml(
&mut $root, &mut $root,
&[$($path)* &concat!($("-", toml_internal!(@path $k),)+)[1..]], &[$($path)* $(&concat!($("-", toml_internal!(@path $k),)+)[1..], )+],
toml_internal!(@value $v)); toml_internal!(@value $v));
toml_internal!(@toplevel $root [$($path)*] $($rest)*); toml_internal!(@toplevel $root [$($path)*] $($rest)*);
}; }};
// Parse array header `[[bin]]`. // Parse array header `[[bin]]`.
(@toplevel $root:ident $oldpath:tt [[$($($path:tt)-+).+]] $($rest:tt)*) => { (@toplevel $root:ident $oldpath:tt [[$($($path:tt)-+).+]] $($rest:tt)*) => {
@ -147,10 +168,10 @@ macro_rules! toml_internal {
}; };
// Parse datetime from string and insert into table. // Parse datetime from string and insert into table.
(@topleveldatetime $root:ident [$($path:tt)*] $($k:tt)-+ = ($($datetime:tt)+) $($rest:tt)*) => { (@topleveldatetime $root:ident [$($path:tt)*] $($($k:tt)-+).+ = ($($datetime:tt)+) $($rest:tt)*) => {
$crate::macros::insert_toml( $crate::macros::insert_toml(
&mut $root, &mut $root,
&[$($path)* &concat!($("-", toml_internal!(@path $k),)+)[1..]], &[$($path)* $(&concat!($("-", toml_internal!(@path $k),)+)[1..], )+],
$crate::Value::Datetime(concat!($(stringify!($datetime)),+).parse().unwrap())); $crate::Value::Datetime(concat!($(stringify!($datetime)),+).parse().unwrap()));
toml_internal!(@toplevel $root [$($path)*] $($rest)*); toml_internal!(@toplevel $root [$($path)*] $($rest)*);
}; };
@ -168,9 +189,9 @@ macro_rules! toml_internal {
// Construct a Value from an inline table. // Construct a Value from an inline table.
(@value { $($inline:tt)* }) => {{ (@value { $($inline:tt)* }) => {{
let mut table = $crate::value::Table::new(); let mut table = $crate::Value::Table($crate::value::Table::new());
toml_internal!(@trailingcomma (@table table) $($inline)*); toml_internal!(@trailingcomma (@table table) $($inline)*);
$crate::Value::Table(table) table
}}; }};
// Construct a Value from an inline array. // Construct a Value from an inline array.
@ -180,6 +201,30 @@ macro_rules! toml_internal {
$crate::Value::Array(array) $crate::Value::Array(array)
}}; }};
(@value (-nan)) => {
$crate::Value::Float(-::std::f64::NAN)
};
(@value (nan)) => {
$crate::Value::Float(::std::f64::NAN)
};
(@value nan) => {
$crate::Value::Float(::std::f64::NAN)
};
(@value (-inf)) => {
$crate::Value::Float(::std::f64::NEG_INFINITY)
};
(@value (inf)) => {
$crate::Value::Float(::std::f64::INFINITY)
};
(@value inf) => {
$crate::Value::Float(::std::f64::INFINITY)
};
// Construct a Value from any other type, probably string or boolean or number. // Construct a Value from any other type, probably string or boolean or number.
(@value $v:tt) => {{ (@value $v:tt) => {{
// TODO: Implement this with something like serde_json::to_value instead. // TODO: Implement this with something like serde_json::to_value instead.
@ -191,57 +236,80 @@ macro_rules! toml_internal {
(@table $root:ident) => {}; (@table $root:ident) => {};
// Parse negative number `key = -value`. // Parse negative number `key = -value`.
(@table $root:ident $($k:tt)-+ = - $v:tt , $($rest:tt)*) => { (@table $root:ident $($($k:tt)-+).+ = - $v:tt , $($rest:tt)*) => {
toml_internal!(@table $root $($k)-+ = (-$v) , $($rest)*); toml_internal!(@table $root $($($k)-+).+ = (-$v) , $($rest)*);
};
// Parse positive number `key = +value`.
(@table $root:ident $($($k:tt)-+).+ = + $v:tt , $($rest:tt)*) => {
toml_internal!(@table $root $($($k)-+).+ = ($v) , $($rest)*);
}; };
// Parse offset datetime `key = 1979-05-27T00:32:00.999999-07:00`. // Parse offset datetime `key = 1979-05-27T00:32:00.999999-07:00`.
(@table $root:ident $($k:tt)-+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => { (@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => {
toml_internal!(@tabledatetime $root $($k)-+ = ($yr - $mo - $dhr : $min : $sec . $frac - $tzh : $tzm) $($rest)*); toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec . $frac - $tzh : $tzm) $($rest)*);
};
// Space instead of T.
(@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt . $frac:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => {
toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec . $frac - $tzh : $tzm) $($rest)*);
}; };
// Parse offset datetime `key = 1979-05-27T00:32:00-07:00`. // Parse offset datetime `key = 1979-05-27T00:32:00-07:00`.
(@table $root:ident $($k:tt)-+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => { (@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => {
toml_internal!(@tabledatetime $root $($k)-+ = ($yr - $mo - $dhr : $min : $sec - $tzh : $tzm) $($rest)*); toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec - $tzh : $tzm) $($rest)*);
};
// Space instead of T.
(@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => {
toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec - $tzh : $tzm) $($rest)*);
}; };
// Parse local datetime `key = 1979-05-27T00:32:00.999999`. // Parse local datetime `key = 1979-05-27T00:32:00.999999`.
(@table $root:ident $($k:tt)-+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt , $($rest:tt)*) => { (@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt , $($rest:tt)*) => {
toml_internal!(@tabledatetime $root $($k)-+ = ($yr - $mo - $dhr : $min : $sec . $frac) $($rest)*); toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec . $frac) $($rest)*);
};
// Space instead of T.
(@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt . $frac:tt , $($rest:tt)*) => {
toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec . $frac) $($rest)*);
}; };
// Parse offset datetime `key = 1979-05-27T07:32:00Z` and local datetime `key = 1979-05-27T07:32:00`. // Parse offset datetime `key = 1979-05-27T07:32:00Z` and local datetime `key = 1979-05-27T07:32:00`.
(@table $root:ident $($k:tt)-+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt , $($rest:tt)*) => { (@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt , $($rest:tt)*) => {
toml_internal!(@tabledatetime $root $($k)-+ = ($yr - $mo - $dhr : $min : $sec) $($rest)*); toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec) $($rest)*);
};
// Space instead of T.
(@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt , $($rest:tt)*) => {
toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec) $($rest)*);
}; };
// Parse local date `key = 1979-05-27`. // Parse local date `key = 1979-05-27`.
(@table $root:ident $($k:tt)-+ = $yr:tt - $mo:tt - $day:tt , $($rest:tt)*) => { (@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt , $($rest:tt)*) => {
toml_internal!(@tabledatetime $root $($k)-+ = ($yr - $mo - $day) $($rest)*); toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $day) $($rest)*);
}; };
// Parse local time `key = 00:32:00.999999`. // Parse local time `key = 00:32:00.999999`.
(@table $root:ident $($k:tt)-+ = $hr:tt : $min:tt : $sec:tt . $frac:tt , $($rest:tt)*) => { (@table $root:ident $($($k:tt)-+).+ = $hr:tt : $min:tt : $sec:tt . $frac:tt , $($rest:tt)*) => {
toml_internal!(@tabledatetime $root $($k)-+ = ($hr : $min : $sec . $frac) $($rest)*); toml_internal!(@tabledatetime $root $($($k)-+).+ = ($hr : $min : $sec . $frac) $($rest)*);
}; };
// Parse local time `key = 07:32:00`. // Parse local time `key = 07:32:00`.
(@table $root:ident $($k:tt)-+ = $hr:tt : $min:tt : $sec:tt , $($rest:tt)*) => { (@table $root:ident $($($k:tt)-+).+ = $hr:tt : $min:tt : $sec:tt , $($rest:tt)*) => {
toml_internal!(@tabledatetime $root $($k)-+ = ($hr : $min : $sec) $($rest)*); toml_internal!(@tabledatetime $root $($($k)-+).+ = ($hr : $min : $sec) $($rest)*);
}; };
// Parse any other type, probably string or boolean or number. // Parse any other type, probably string or boolean or number.
(@table $root:ident $($k:tt)-+ = $v:tt , $($rest:tt)*) => { (@table $root:ident $($($k:tt)-+).+ = $v:tt , $($rest:tt)*) => {
$root.insert( $crate::macros::insert_toml(
concat!($("-", toml_internal!(@path $k),)+)[1..].to_owned(), &mut $root,
&[$(&concat!($("-", toml_internal!(@path $k),)+)[1..], )+],
toml_internal!(@value $v)); toml_internal!(@value $v));
toml_internal!(@table $root $($rest)*); toml_internal!(@table $root $($rest)*);
}; };
// Parse a Datetime from string and continue in @table state. // Parse a Datetime from string and continue in @table state.
(@tabledatetime $root:ident $($k:tt)-+ = ($($datetime:tt)*) $($rest:tt)*) => { (@tabledatetime $root:ident $($($k:tt)-+).+ = ($($datetime:tt)*) $($rest:tt)*) => {
$root.insert( $crate::macros::insert_toml(
concat!($("-", toml_internal!(@path $k),)+)[1..].to_owned(), &mut $root,
&[$(&concat!($("-", toml_internal!(@path $k),)+)[1..], )+],
$crate::Value::Datetime(concat!($(stringify!($datetime)),+).parse().unwrap())); $crate::Value::Datetime(concat!($(stringify!($datetime)),+).parse().unwrap()));
toml_internal!(@table $root $($rest)*); toml_internal!(@table $root $($rest)*);
}; };
@ -254,25 +322,46 @@ macro_rules! toml_internal {
toml_internal!(@array $root (-$v) , $($rest)*); toml_internal!(@array $root (-$v) , $($rest)*);
}; };
// Parse positive number `+value`.
(@array $root:ident + $v:tt , $($rest:tt)*) => {
toml_internal!(@array $root ($v) , $($rest)*);
};
// Parse offset datetime `1979-05-27T00:32:00.999999-07:00`. // Parse offset datetime `1979-05-27T00:32:00.999999-07:00`.
(@array $root:ident $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => { (@array $root:ident $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => {
toml_internal!(@arraydatetime $root ($yr - $mo - $dhr : $min : $sec . $frac - $tzh : $tzm) $($rest)*); toml_internal!(@arraydatetime $root ($yr - $mo - $dhr : $min : $sec . $frac - $tzh : $tzm) $($rest)*);
}; };
// Space instead of T.
(@array $root:ident $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt . $frac:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => {
toml_internal!(@arraydatetime $root ($yr - $mo - $day T $hr : $min : $sec . $frac - $tzh : $tzm) $($rest)*);
};
// Parse offset datetime `1979-05-27T00:32:00-07:00`. // Parse offset datetime `1979-05-27T00:32:00-07:00`.
(@array $root:ident $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => { (@array $root:ident $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => {
toml_internal!(@arraydatetime $root ($yr - $mo - $dhr : $min : $sec - $tzh : $tzm) $($rest)*); toml_internal!(@arraydatetime $root ($yr - $mo - $dhr : $min : $sec - $tzh : $tzm) $($rest)*);
}; };
// Space instead of T.
(@array $root:ident $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => {
toml_internal!(@arraydatetime $root ($yr - $mo - $day T $hr : $min : $sec - $tzh : $tzm) $($rest)*);
};
// Parse local datetime `1979-05-27T00:32:00.999999`. // Parse local datetime `1979-05-27T00:32:00.999999`.
(@array $root:ident $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt , $($rest:tt)*) => { (@array $root:ident $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt , $($rest:tt)*) => {
toml_internal!(@arraydatetime $root ($yr - $mo - $dhr : $min : $sec . $frac) $($rest)*); toml_internal!(@arraydatetime $root ($yr - $mo - $dhr : $min : $sec . $frac) $($rest)*);
}; };
// Space instead of T.
(@array $root:ident $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt . $frac:tt , $($rest:tt)*) => {
toml_internal!(@arraydatetime $root ($yr - $mo - $day T $hr : $min : $sec . $frac) $($rest)*);
};
// Parse offset datetime `1979-05-27T07:32:00Z` and local datetime `1979-05-27T07:32:00`. // Parse offset datetime `1979-05-27T07:32:00Z` and local datetime `1979-05-27T07:32:00`.
(@array $root:ident $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt , $($rest:tt)*) => { (@array $root:ident $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt , $($rest:tt)*) => {
toml_internal!(@arraydatetime $root ($yr - $mo - $dhr : $min : $sec) $($rest)*); toml_internal!(@arraydatetime $root ($yr - $mo - $dhr : $min : $sec) $($rest)*);
}; };
// Space instead of T.
(@array $root:ident $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt , $($rest:tt)*) => {
toml_internal!(@arraydatetime $root ($yr - $mo - $day T $hr : $min : $sec) $($rest)*);
};
// Parse local date `1979-05-27`. // Parse local date `1979-05-27`.
(@array $root:ident $yr:tt - $mo:tt - $day:tt , $($rest:tt)*) => { (@array $root:ident $yr:tt - $mo:tt - $day:tt , $($rest:tt)*) => {

View file

@ -1,8 +1,10 @@
#![recursion_limit = "128"] #![recursion_limit = "256"]
#[macro_use] #[macro_use]
extern crate toml; extern crate toml;
use std::f64;
macro_rules! table { macro_rules! table {
($($key:expr => $value:expr,)*) => {{ ($($key:expr => $value:expr,)*) => {{
let mut table = toml::value::Table::new(); let mut table = toml::value::Table::new();
@ -143,6 +145,19 @@ fn test_number() {
negative = -1 negative = -1
table = { positive = 1, negative = -1 } table = { positive = 1, negative = -1 }
array = [ 1, -1 ] array = [ 1, -1 ]
neg_zero = -0
pos_zero = +0
float = 3.14
sf1 = inf
sf2 = +inf
sf3 = -inf
sf7 = +0.0
sf8 = -0.0
hex = 0xa_b_c
oct = 0o755
bin = 0b11010110
}; };
let expected = table! { let expected = table! {
@ -156,11 +171,34 @@ fn test_number() {
1, 1,
-1, -1,
}, },
"neg_zero" => -0,
"pos_zero" => 0,
"float" => 3.14,
"sf1" => f64::INFINITY,
"sf2" => f64::INFINITY,
"sf3" => f64::NEG_INFINITY,
"sf7" => 0.0,
"sf8" => -0.0,
"hex" => 2748,
"oct" => 493,
"bin" => 214,
}; };
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
#[test]
fn test_nan() {
let actual = toml! {
sf4 = nan
sf5 = +nan
sf6 = -nan
};
assert!(actual["sf4"].as_float().unwrap().is_nan());
assert!(actual["sf5"].as_float().unwrap().is_nan());
assert!(actual["sf6"].as_float().unwrap().is_nan());
}
#[test] #[test]
fn test_datetime() { fn test_datetime() {
let actual = toml! { let actual = toml! {
@ -168,6 +206,7 @@ fn test_datetime() {
odt1 = 1979-05-27T07:32:00Z odt1 = 1979-05-27T07:32:00Z
odt2 = 1979-05-27T00:32:00-07:00 odt2 = 1979-05-27T00:32:00-07:00
odt3 = 1979-05-27T00:32:00.999999-07:00 odt3 = 1979-05-27T00:32:00.999999-07:00
odt4 = 1979-05-27 07:32:00Z
ldt1 = 1979-05-27T07:32:00 ldt1 = 1979-05-27T07:32:00
ldt2 = 1979-05-27T00:32:00.999999 ldt2 = 1979-05-27T00:32:00.999999
ld1 = 1979-05-27 ld1 = 1979-05-27
@ -178,6 +217,7 @@ fn test_datetime() {
odt1 = 1979-05-27T07:32:00Z, odt1 = 1979-05-27T07:32:00Z,
odt2 = 1979-05-27T00:32:00-07:00, odt2 = 1979-05-27T00:32:00-07:00,
odt3 = 1979-05-27T00:32:00.999999-07:00, odt3 = 1979-05-27T00:32:00.999999-07:00,
odt4 = 1979-05-27 07:32:00Z,
ldt1 = 1979-05-27T07:32:00, ldt1 = 1979-05-27T07:32:00,
ldt2 = 1979-05-27T00:32:00.999999, ldt2 = 1979-05-27T00:32:00.999999,
ld1 = 1979-05-27, ld1 = 1979-05-27,
@ -189,6 +229,7 @@ fn test_datetime() {
1979-05-27T07:32:00Z, 1979-05-27T07:32:00Z,
1979-05-27T00:32:00-07:00, 1979-05-27T00:32:00-07:00,
1979-05-27T00:32:00.999999-07:00, 1979-05-27T00:32:00.999999-07:00,
1979-05-27 07:32:00Z,
1979-05-27T07:32:00, 1979-05-27T07:32:00,
1979-05-27T00:32:00.999999, 1979-05-27T00:32:00.999999,
1979-05-27, 1979-05-27,
@ -201,6 +242,7 @@ fn test_datetime() {
"odt1" => datetime!("1979-05-27T07:32:00Z"), "odt1" => datetime!("1979-05-27T07:32:00Z"),
"odt2" => datetime!("1979-05-27T00:32:00-07:00"), "odt2" => datetime!("1979-05-27T00:32:00-07:00"),
"odt3" => datetime!("1979-05-27T00:32:00.999999-07:00"), "odt3" => datetime!("1979-05-27T00:32:00.999999-07:00"),
"odt4" => datetime!("1979-05-27 07:32:00Z"),
"ldt1" => datetime!("1979-05-27T07:32:00"), "ldt1" => datetime!("1979-05-27T07:32:00"),
"ldt2" => datetime!("1979-05-27T00:32:00.999999"), "ldt2" => datetime!("1979-05-27T00:32:00.999999"),
"ld1" => datetime!("1979-05-27"), "ld1" => datetime!("1979-05-27"),
@ -211,6 +253,7 @@ fn test_datetime() {
"odt1" => datetime!("1979-05-27T07:32:00Z"), "odt1" => datetime!("1979-05-27T07:32:00Z"),
"odt2" => datetime!("1979-05-27T00:32:00-07:00"), "odt2" => datetime!("1979-05-27T00:32:00-07:00"),
"odt3" => datetime!("1979-05-27T00:32:00.999999-07:00"), "odt3" => datetime!("1979-05-27T00:32:00.999999-07:00"),
"odt4" => datetime!("1979-05-27 07:32:00Z"),
"ldt1" => datetime!("1979-05-27T07:32:00"), "ldt1" => datetime!("1979-05-27T07:32:00"),
"ldt2" => datetime!("1979-05-27T00:32:00.999999"), "ldt2" => datetime!("1979-05-27T00:32:00.999999"),
"ld1" => datetime!("1979-05-27"), "ld1" => datetime!("1979-05-27"),
@ -222,6 +265,7 @@ fn test_datetime() {
datetime!("1979-05-27T07:32:00Z"), datetime!("1979-05-27T07:32:00Z"),
datetime!("1979-05-27T00:32:00-07:00"), datetime!("1979-05-27T00:32:00-07:00"),
datetime!("1979-05-27T00:32:00.999999-07:00"), datetime!("1979-05-27T00:32:00.999999-07:00"),
datetime!("1979-05-27 07:32:00Z"),
datetime!("1979-05-27T07:32:00"), datetime!("1979-05-27T07:32:00"),
datetime!("1979-05-27T00:32:00.999999"), datetime!("1979-05-27T00:32:00.999999"),
datetime!("1979-05-27"), datetime!("1979-05-27"),
@ -284,3 +328,38 @@ fn test_empty() {
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
#[test]
fn test_dotted_keys() {
let actual = toml! {
a.b = 123
a.c = 1979-05-27T07:32:00Z
[table]
a.b.c = 1
a . b . d = 2
in = { type.name = "cat", type.color = "blue" }
};
let expected = table! {
"a" => table! {
"b" => 123,
"c" => datetime!("1979-05-27T07:32:00Z"),
},
"table" => table! {
"a" => table! {
"b" => table! {
"c" => 1,
"d" => 2,
},
},
"in" => table! {
"type" => table! {
"name" => "cat",
"color" => "blue",
},
},
},
};
assert_eq!(actual, expected);
}