Skip to content

Commit 0fa3643

Browse files
Fix error when no arguments are parsed
When cooking the span, it used to give us a (0,1) span even though the byte slice of the input was empty. Now it does (0,0). Closes #1
1 parent 122693a commit 0fa3643

File tree

7 files changed

+91
-0
lines changed

7 files changed

+91
-0
lines changed

src/deserialize.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,17 @@ impl<'input, F: Format<SpanType = Raw, Input<'input> = [&'input str]>> ToCooked<
253253
{
254254
#[inline]
255255
fn to_cooked(self, _format: &F, input: &'input [&'input str]) -> Span<Cooked> {
256+
trace!(
257+
"cooking span, self.start = {}, input.len = {}",
258+
self.start,
259+
input.len()
260+
);
261+
262+
if self.start == 0 && input.is_empty() {
263+
// empty input
264+
return Span::<Cooked>::new(0, 0);
265+
}
266+
256267
if self.start >= input.len() {
257268
// start points past the end of the args;
258269
// use byte offset = total length of whole input minus 1, len = 1

src/deserialize/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,10 @@ impl core::fmt::Display for DeserError<'_> {
302302

303303
let mut span_start = self.span.start();
304304
let mut span_end = self.span.end();
305+
trace!("span range = {span_start}..{span_end}");
306+
305307
use alloc::borrow::Cow;
308+
use log::trace;
306309
let mut input_str: Cow<'_, str> = Cow::Borrowed(orig_input_str);
307310

308311
// --- Context-sensitive truncation logic ---

tests/sequence.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,39 @@ fn test_simplest_value_singleton_list_positional() {
3030
assert_eq!(args_single.strings, vec!["joe", "le", "rigolo"]);
3131
}
3232

33+
#[test]
34+
fn test_noargs_single_positional() {
35+
#[derive(Facet, Debug, PartialEq)]
36+
struct Args {
37+
#[facet(positional)]
38+
strings: String,
39+
}
40+
let err = facet_args::from_slice::<Args>(&[]).unwrap_err();
41+
insta::assert_snapshot!(err);
42+
}
43+
44+
#[test]
45+
fn test_noargs_vec_positional_default() {
46+
#[derive(Facet, Debug, PartialEq)]
47+
struct Args {
48+
#[facet(positional, default)]
49+
strings: Vec<String>,
50+
}
51+
let args = facet_args::from_slice::<Args>(&[]).unwrap();
52+
assert!(args.strings.is_empty());
53+
}
54+
55+
#[test]
56+
fn test_noargs_vec_positional_no_default() {
57+
#[derive(Facet, Debug, PartialEq)]
58+
struct Args {
59+
#[facet(positional)]
60+
strings: Vec<String>,
61+
}
62+
let err = facet_args::from_slice::<Args>(&[]).unwrap_err();
63+
insta::assert_snapshot!(err);
64+
}
65+
3366
#[test]
3467
#[ignore]
3568
fn test_value_singleton_list() {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: tests/sequence.rs
3+
expression: err
4+
---
5+
Error:
6+
╭─[ <unknown>:1:1 ]
7+
│
8+
1 │
9+
 │ │
10+
 │ ╰─ Field 'Args::strings' was not initialized
11+
───╯
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: tests/sequence.rs
3+
expression: err
4+
---
5+
Error:
6+
╭─[ <unknown>:1:1 ]
7+
│
8+
1 │
9+
 │ │
10+
 │ ╰─ Field 'Args::strings' was not initialized
11+
───╯
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: tests/sequence.rs
3+
expression: err
4+
---
5+
Error:
6+
╭─[ <unknown>:1:1 ]
7+
│
8+
1 │
9+
 │ │
10+
 │ ╰─ Field 'Args::strings' was not initialized
11+
───╯
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: tests/sequence.rs
3+
expression: err
4+
---
5+
Error:
6+
╭─[ <unknown>:1:1 ]
7+
│
8+
1 │
9+
 │ │
10+
 │ ╰─ Field 'Args::strings' was not initialized
11+
───╯

0 commit comments

Comments
 (0)