Skip to content

Commit

Permalink
Allow empty string as an arg value (#2027)
Browse files Browse the repository at this point in the history
  • Loading branch information
bim9262 authored Mar 30, 2024
1 parent 0821c60 commit 06d1156
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/formatting/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use unicode_segmentation::UnicodeSegmentation;

use std::fmt::Debug;
use std::time::Duration;

Expand Down
19 changes: 13 additions & 6 deletions src/formatting/formatter/eng.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use crate::formatting::prefix::Prefix;
use crate::formatting::unit::Unit;

use std::borrow::Cow;

use super::*;

type PadWith = Cow<'static, str>;

const DEFAULT_NUMBER_WIDTH: usize = 2;
const DEFAULT_NUMBER_PAD_WITH: char = ' ';
const DEFAULT_NUMBER_PAD_WITH: PadWith = Cow::Borrowed(" ");

// TODO: split those defaults
pub const DEFAULT_NUMBER_FORMATTER: EngFormatter = EngFormatter {
Expand All @@ -29,7 +33,7 @@ pub struct EngFormatter {
prefix_has_space: bool,
prefix_hidden: bool,
prefix_forced: bool,
pad_with: char,
pad_with: PadWith,
}

impl EngFormatter {
Expand Down Expand Up @@ -91,10 +95,13 @@ impl EngFormatter {
.error("force_prefix must be true or false")?;
}
"pad_with" => {
pad_with = arg
.val
.parse()
.error("pad_with must be a single character")?;
if arg.val.graphemes(true).count() < 2 {
pad_with = Cow::Owned(arg.val.into());
} else {
return Err(Error::new(
"pad_with must be an empty string or a single character",
));
}
}
other => {
return Err(Error::new(format!("Unknown argument for 'eng': '{other}'")));
Expand Down
2 changes: 0 additions & 2 deletions src/formatting/formatter/str.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use unicode_segmentation::UnicodeSegmentation;

use std::iter::repeat;
use std::time::Instant;

Expand Down
17 changes: 15 additions & 2 deletions src/formatting/parse.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use nom::{
branch::alt,
bytes::complete::{escaped_transform, is_not, tag, take_while, take_while1},
bytes::complete::{escaped_transform, tag, take_while, take_while1},
character::complete::{anychar, char},
combinator::{cut, eof, map, not, opt},
multi::{many0, separated_list0},
Expand Down Expand Up @@ -86,7 +86,10 @@ fn alphanum1(i: &str) -> IResult<&str, &str, PError> {
fn arg1(i: &str) -> IResult<&str, &str, PError> {
alt((
take_while1(|x: char| x.is_alphanumeric() || x == '_' || x == '-' || x == '.' || x == '%'),
preceded(char('\''), cut(terminated(is_not("\'"), char('\'')))),
preceded(
char('\''),
cut(terminated(take_while(|x: char| x != '\''), char('\''))),
),
))(i)
}

Expand Down Expand Up @@ -222,6 +225,16 @@ mod tests {
}
))
);
assert_eq!(
parse_arg("key:'',"),
Ok((
",",
Arg {
key: "key",
val: ""
}
))
);
assert!(parse_arg("key:,").is_err());
}

Expand Down

0 comments on commit 06d1156

Please sign in to comment.