Skip to content

Commit

Permalink
Change to version=X, implement for serde helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Dec 12, 2022
1 parent 581c44a commit beac001
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 101 deletions.
28 changes: 18 additions & 10 deletions tests/compile-fail/invalid_format_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@ fn main() {
let _ = format_description!("[day sign:mandatory");
let _ = format_description!("[day padding:invalid]");

let _ = format_description!(v2, r"\a");
let _ = format_description!(v2, r"\");
let _ = format_description!(version);
let _ = format_description!(version "");
let _ = format_description!(version =);
let _ = format_description!(version = 0);
let _ = format_description!(version = 1);
let _ = format_description!(version = 3);
let _ = format_description!(version = two);

let _ = format_description!(v2, "[year [month]]");
let _ = format_description!(v2, "[optional[]]");
let _ = format_description!(v2, "[first[]]");
let _ = format_description!(v2, "[optional []");
let _ = format_description!(v2, "[first []");
let _ = format_description!(v2, "[optional [");
let _ = format_description!(v2, "[optional [[year");
let _ = format_description!(v2, "[optional ");
let _ = format_description!(version = 2, r"\a");
let _ = format_description!(version = 2, r"\");

let _ = format_description!(version = 2, "[year [month]]");
let _ = format_description!(version = 2, "[optional[]]");
let _ = format_description!(version = 2, "[first[]]");
let _ = format_description!(version = 2, "[optional []");
let _ = format_description!(version = 2, "[first []");
let _ = format_description!(version = 2, "[optional [");
let _ = format_description!(version = 2, "[optional [[year");
let _ = format_description!(version = 2, "[optional ");
}
110 changes: 79 additions & 31 deletions tests/compile-fail/invalid_format_description.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ error: unexpected token: x
9 | let _ = format_description!("" x);
| ^

error: expected `v1` or `v2`
error: expected string literal
--> ../tests/compile-fail/invalid_format_description.rs:10:33
|
10 | let _ = format_description!(x);
Expand Down Expand Up @@ -108,62 +108,110 @@ error: invalid modifier value
22 | let _ = format_description!("[day padding:invalid]");
| ^^^^^^^^^^^^^^^^^^^^^^^

error: expected `=`
--> ../tests/compile-fail/invalid_format_description.rs:24:13
|
24 | let _ = format_description!(version);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `format_description` (in Nightly builds, run with -Z macro-backtrace for more info)

error: expected `=`
--> ../tests/compile-fail/invalid_format_description.rs:25:41
|
25 | let _ = format_description!(version "");
| ^^

error: expected 1 or 2
--> ../tests/compile-fail/invalid_format_description.rs:26:13
|
26 | let _ = format_description!(version =);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `format_description` (in Nightly builds, run with -Z macro-backtrace for more info)

error: invalid format description version
--> ../tests/compile-fail/invalid_format_description.rs:27:43
|
27 | let _ = format_description!(version = 0);
| ^

error: unexpected end of input
--> ../tests/compile-fail/invalid_format_description.rs:28:13
|
28 | let _ = format_description!(version = 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `format_description` (in Nightly builds, run with -Z macro-backtrace for more info)

error: invalid format description version
--> ../tests/compile-fail/invalid_format_description.rs:29:43
|
29 | let _ = format_description!(version = 3);
| ^

error: expected 1 or 2
--> ../tests/compile-fail/invalid_format_description.rs:30:43
|
30 | let _ = format_description!(version = two);
| ^^^

error: invalid escape sequence
--> ../tests/compile-fail/invalid_format_description.rs:24:37
--> ../tests/compile-fail/invalid_format_description.rs:32:46
|
24 | let _ = format_description!(v2, r"\a");
| ^^^^^
32 | let _ = format_description!(version = 2, r"\a");
| ^^^^^

error: unexpected end of input
--> ../tests/compile-fail/invalid_format_description.rs:25:37
--> ../tests/compile-fail/invalid_format_description.rs:33:46
|
25 | let _ = format_description!(v2, r"\");
| ^^^^
33 | let _ = format_description!(version = 2, r"\");
| ^^^^

error: modifier must be of the form `key:value`
--> ../tests/compile-fail/invalid_format_description.rs:27:37
--> ../tests/compile-fail/invalid_format_description.rs:35:46
|
27 | let _ = format_description!(v2, "[year [month]]");
| ^^^^^^^^^^^^^^^^
35 | let _ = format_description!(version = 2, "[year [month]]");
| ^^^^^^^^^^^^^^^^

error: expected whitespace after `optional`
--> ../tests/compile-fail/invalid_format_description.rs:28:37
--> ../tests/compile-fail/invalid_format_description.rs:36:46
|
28 | let _ = format_description!(v2, "[optional[]]");
| ^^^^^^^^^^^^^^
36 | let _ = format_description!(version = 2, "[optional[]]");
| ^^^^^^^^^^^^^^

error: expected whitespace after `first`
--> ../tests/compile-fail/invalid_format_description.rs:29:37
--> ../tests/compile-fail/invalid_format_description.rs:37:46
|
29 | let _ = format_description!(v2, "[first[]]");
| ^^^^^^^^^^^
37 | let _ = format_description!(version = 2, "[first[]]");
| ^^^^^^^^^^^

error: unclosed bracket
--> ../tests/compile-fail/invalid_format_description.rs:30:37
--> ../tests/compile-fail/invalid_format_description.rs:38:46
|
30 | let _ = format_description!(v2, "[optional []");
| ^^^^^^^^^^^^^^
38 | let _ = format_description!(version = 2, "[optional []");
| ^^^^^^^^^^^^^^

error: unclosed bracket
--> ../tests/compile-fail/invalid_format_description.rs:31:37
--> ../tests/compile-fail/invalid_format_description.rs:39:46
|
31 | let _ = format_description!(v2, "[first []");
| ^^^^^^^^^^^
39 | let _ = format_description!(version = 2, "[first []");
| ^^^^^^^^^^^

error: unclosed bracket
--> ../tests/compile-fail/invalid_format_description.rs:32:37
--> ../tests/compile-fail/invalid_format_description.rs:40:46
|
32 | let _ = format_description!(v2, "[optional [");
| ^^^^^^^^^^^^^
40 | let _ = format_description!(version = 2, "[optional [");
| ^^^^^^^^^^^^^

error: unclosed bracket
--> ../tests/compile-fail/invalid_format_description.rs:33:37
--> ../tests/compile-fail/invalid_format_description.rs:41:46
|
33 | let _ = format_description!(v2, "[optional [[year");
| ^^^^^^^^^^^^^^^^^^
41 | let _ = format_description!(version = 2, "[optional [[year");
| ^^^^^^^^^^^^^^^^^^

error: expected opening bracket
--> ../tests/compile-fail/invalid_format_description.rs:34:37
--> ../tests/compile-fail/invalid_format_description.rs:42:46
|
34 | let _ = format_description!(v2, "[optional ");
| ^^^^^^^^^^^^
42 | let _ = format_description!(version = 2, "[optional ");
| ^^^^^^^^^^^^
60 changes: 33 additions & 27 deletions tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,36 @@ fn nontrivial_string() {

#[test]
fn format_description_version() {
assert_eq!(format_description!(v1, "[["), &[FormatItem::Literal(b"[")]);
assert_eq!(
format_description!(v1, r"\\"),
format_description!(version = 1, "[["),
&[FormatItem::Literal(b"[")]
);
assert_eq!(
format_description!(version = 1, r"\\"),
&[FormatItem::Literal(br"\\")]
);
assert_eq!(
format_description!(v2, r"\\"),
format_description!(version = 2, r"\\"),
&[FormatItem::Literal(br"\")]
);
}

#[test]
fn nested_v1() {
assert_eq!(
format_description!(v1, "[optional [[[]]"),
format_description!(version = 1, "[optional [[[]]"),
&[FormatItem::Optional(&FormatItem::Literal(b"["))]
);
assert_eq!(
format_description!(v1, "[optional [ [[ ]]"),
format_description!(version = 1, "[optional [ [[ ]]"),
&[FormatItem::Optional(&FormatItem::Compound(&[
FormatItem::Literal(b" "),
FormatItem::Literal(b"["),
FormatItem::Literal(b" "),
]))]
);
assert_eq!(
format_description!(v1, "[first [a][[[]]"),
format_description!(version = 1, "[first [a][[[]]"),
&[FormatItem::First(&[
FormatItem::Literal(b"a"),
FormatItem::Literal(b"[")
Expand All @@ -65,24 +68,24 @@ fn nested_v1() {
#[test]
fn optional() {
assert_eq!(
format_description!(v2, "[optional [:[year]]]"),
format_description!(version = 2, "[optional [:[year]]]"),
&[FormatItem::Optional(&FormatItem::Compound(&[
FormatItem::Literal(b":"),
FormatItem::Component(Component::Year(Default::default()))
]))]
);
assert_eq!(
format_description!(v2, "[optional [[year]]]"),
format_description!(version = 2, "[optional [[year]]]"),
&[FormatItem::Optional(&FormatItem::Component(
Component::Year(Default::default())
))]
);
assert_eq!(
format_description!(v2, r"[optional [\[]]"),
format_description!(version = 2, r"[optional [\[]]"),
&[FormatItem::Optional(&FormatItem::Literal(b"["))]
);
assert_eq!(
format_description!(v2, r"[optional [ \[ ]]"),
format_description!(version = 2, r"[optional [ \[ ]]"),
&[FormatItem::Optional(&FormatItem::Compound(&[
FormatItem::Literal(b" "),
FormatItem::Literal(b"["),
Expand All @@ -94,39 +97,42 @@ fn optional() {
#[test]
fn first() {
assert_eq!(
format_description!(v2, "[first [a]]"),
format_description!(version = 2, "[first [a]]"),
&[FormatItem::First(&[FormatItem::Literal(b"a")])]
);
assert_eq!(
format_description!(v2, "[first [a] [b]]"),
format_description!(version = 2, "[first [a] [b]]"),
&[FormatItem::First(&[
FormatItem::Literal(b"a"),
FormatItem::Literal(b"b"),
])]
);
assert_eq!(
format_description!(v2, "[first [a][b]]"),
format_description!(version = 2, "[first [a][b]]"),
&[FormatItem::First(&[
FormatItem::Literal(b"a"),
FormatItem::Literal(b"b"),
])]
);
assert_eq!(
format_description!(v2, r"[first [a][\[]]"),
format_description!(version = 2, r"[first [a][\[]]"),
&[FormatItem::First(&[
FormatItem::Literal(b"a"),
FormatItem::Literal(b"["),
])]
);
assert_eq!(
format_description!(v2, r"[first [a][\[\[]]"),
format_description!(version = 2, r"[first [a][\[\[]]"),
&[FormatItem::First(&[
FormatItem::Literal(b"a"),
FormatItem::Compound(&[FormatItem::Literal(b"["), FormatItem::Literal(b"["),])
])]
);
assert_eq!(
format_description!(v2, "[first [[period case:upper]] [[period case:lower]] ]"),
format_description!(
version = 2,
"[first [[period case:upper]] [[period case:lower]] ]"
),
&[FormatItem::First(&[
FormatItem::Component(Component::Period(modifier!(Period {
is_uppercase: true,
Expand All @@ -143,47 +149,47 @@ fn first() {
#[test]
fn backslash_escape() {
assert_eq!(
format_description!(v2, r"[optional [\]]]"),
format_description!(version = 2, r"[optional [\]]]"),
&[FormatItem::Optional(&FormatItem::Literal(b"]"))]
);
assert_eq!(
format_description!(v2, r"[optional [\[]]"),
format_description!(version = 2, r"[optional [\[]]"),
&[FormatItem::Optional(&FormatItem::Literal(b"["))]
);
assert_eq!(
format_description!(v2, r"[optional [\\]]"),
format_description!(version = 2, r"[optional [\\]]"),
&[FormatItem::Optional(&FormatItem::Literal(br"\"))]
);
assert_eq!(
format_description!(v2, r"\\"),
format_description!(version = 2, r"\\"),
&[FormatItem::Literal(br"\")]
);
assert_eq!(
format_description!(v2, r"\["),
format_description!(version = 2, r"\["),
&[FormatItem::Literal(br"[")]
);
assert_eq!(
format_description!(v2, r"\]"),
format_description!(version = 2, r"\]"),
&[FormatItem::Literal(br"]")]
);
assert_eq!(
format_description!(v2, r"foo\\"),
format_description!(version = 2, r"foo\\"),
&[FormatItem::Literal(b"foo"), FormatItem::Literal(br"\"),]
);
assert_eq!(
format_description!(v2, r"\\"),
format_description!(version = 2, r"\\"),
&[FormatItem::Literal(br"\")]
);
assert_eq!(
format_description!(v2, r"\["),
format_description!(version = 2, r"\["),
&[FormatItem::Literal(br"[")]
);
assert_eq!(
format_description!(v2, r"\]"),
format_description!(version = 2, r"\]"),
&[FormatItem::Literal(br"]")]
);
assert_eq!(
format_description!(v2, r"foo\\"),
format_description!(version = 2, r"foo\\"),
&[FormatItem::Literal(b"foo"), FormatItem::Literal(br"\"),]
);
}
Expand Down
Loading

0 comments on commit beac001

Please sign in to comment.