From 32ad1d384ac710b33c89ad234397c35b02ebd9e5 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Wed, 29 May 2024 09:31:45 +0800 Subject: [PATCH 1/6] fix #47 --- Cargo.toml | 2 +- duration-str.png | Bin 0 -> 3402 bytes src/error.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++-- src/lib.rs | 1 + src/parser.rs | 2 +- src/unit.rs | 2 +- 6 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 duration-str.png diff --git a/Cargo.toml b/Cargo.toml index d71bace..34c2cc9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ readme = "README.md" categories = ["parsing", "date-and-time"] repository = "https://github.com/baoyachi/duration-str" license = "Apache-2.0" -exclude = ["./playground"] +exclude = ["duration-str.png", "./playground"] [features] default = ["chrono", "serde", "time"] diff --git a/duration-str.png b/duration-str.png new file mode 100644 index 0000000000000000000000000000000000000000..832cf50f34a57c9b379a25b39890482d71c8f213 GIT binary patch literal 3402 zcmbuC`9IX(9>-_1hZ(Zf7{iQxElXtkn8;X)>X)xj9d8LLd-s6QZFNGxq&w zv$HbKYKNLUW`G7-851BiAJMDKB7}*dfep#!XYPf784YACM_JBHhmxdyt^8->v$n6E zHPl;_Fuo|Qa3*|cKKJcW#}( zgS#y3>}}DU3Nirj9b*H6!))b17Ay(tMP3!g3vX?x$qne767W3tW(14@T@MmltoV_kQXa8QGG9-Ukv0d=c zh!yah_PAj)0XEzUKinx3^W)40Kig8?iS1t9$imcUyD3#^ETT9>%vMp1X;kt!gfBC@d|EH6 zT*6@yhGS5lp&ZZ`Qap`$SvS@kB27@dnxxCYJ``PW&}7=|ywPKIDJPR43jhTJJ)P-x z@5Hbd0!0;nef0&pws7@g$>tVhgv9j;9@?d)gX|g6 zAoDZZrZ5St4its7T}?w60wkuY9FgaU2RE6@ro^5P37u!Ea?_W1EUc3s3B8o&Vgkg( ztPl#xwXs)eu>|oBj`;tk)l!5g9H%^jXv1?k0ZcO+0Ho&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#`yxLA)L78X5EqeL4Jf80sdpG{kLxPiefenS=;k#vjF;HYc>ha4*K56!VN;Q* z$Th2=2{d$SgiUz2_FsBiWdt10Y8S_r^or>p?UdM-*SMzNq&jx_*~x}4@$tw-c{UQq zUl{j8wGT8AT*#8&yy5f(6p8`WhV~24B)+H~=-qw5B|3i z11FO)(qd1(oOn5=_*lwSQd|Su$z`E2t~>5z6jFN;7~dY+3ttscSevb?_tI;-uQ+bW zM^1uJ+A7tsc7m)EL22vmX|JapBb)xEG#=n9Yv?;QLbxJ!Ux)kC&6h~ITTKr4ADP5) z#cyI1lxM4Mp9`k1x-9k(V5diukmQUVbtP_S!l+khrAJ{F%B?BSLYbV~FM$=g(70Xs z6eNihJkMat6QJ4!Tr(pXTvU}~TitJPim)~*Y#VSDNWk9zbLcDG;MSEsE#S|#+KF=8 z-x_EhEI88Z&;Dkn;oF?n4O{%X<~*twX$`NC{BTRNcsC8Ly*$ox0=andFyQ2?@m>Ea z69Za7zM_nbt@>qdsO;T1fCp6|!G7>L3e9>hoQ*nI3u!(I6PemsWeuM1A*gv!>RJ)S z1-8S7^(-ZoQu5n=r)&_bl*c+Z9g-&m71*h5n-CH6-vLrp1x>dHJ`lR?p^Gw3&-S?6 zzntx(yNIJTrjq;3?+N=hv`k^J zvN*w`UQ?+ZHRf66fZ}MEZBQLM@r97cy(Dm(brsQFTPAIn3?j*f6AXqh6I=CkbnDWY z*ovn^7~^VT-!_v;$bIXM5e%ctyAOSh^ZP=-(VxDEjd;}hq5wrBfJ0GpfH61aMAa4_q( zEwG>yW`90y3wx#KT9{lx2?R>F%G)O}=LhUSH^G}p#0{w)Z;{N=fnBwZ?8uCYOPR_s yFa)C0T%kA}hA=d{yUbMxfj&^-=c@RhpC7?{9ZdXq!gz8Z%w=M9(XfW#8uf33 String; +} + +impl RawDebug for A +where + A: AsRef, +{ + fn raw(&self) -> String { + format!("{}", self.as_ref()) + } +} + #[derive(Error, Debug, PartialEq)] pub enum DError { #[error("{0}")] @@ -11,9 +24,11 @@ pub enum DError { OverflowError, } +const PARTIAL_INPUT_MAX_LEN: usize = 11; + #[derive(Debug, PartialEq, Eq)] pub struct PError { - pub partial_input: I, + partial_input: I, kind: ErrorKind, cause: String, } @@ -31,6 +46,22 @@ impl PError { self.cause = cause.as_ref().to_string(); self } + + pub fn partial_input(&self) -> String + where + I: RawDebug, + { + let raw = self.partial_input.raw(); + if let Some(offset) = raw + .char_indices() + .enumerate() + .find_map(|(pos, (offset, _))| (PARTIAL_INPUT_MAX_LEN <= pos).then_some(offset)) + { + format!("{}...", raw.split_at(offset).0) + } else { + raw + } + } } impl ParserError for PError { @@ -66,3 +97,35 @@ where Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_partial_input() { + let error = PError::new("1234567890abcde", ErrorKind::Complete); + let partial_input = error.partial_input(); + assert_eq!(partial_input, "1234567890a..."); + + let error = PError::new("你好,龙骧虎步龙行龘龘龘", ErrorKind::Complete); + let partial_input = error.partial_input(); + assert_eq!(partial_input, "你好,龙骧虎步龙行龘龘..."); + + let error = PError::new("hello,你好", ErrorKind::Complete); + let partial_input = error.partial_input(); + assert_eq!(partial_input, "hello,你好"); + + let error = PError::new("1mins", ErrorKind::Complete); + let partial_input = error.partial_input(); + assert_eq!(partial_input, "1mins"); + + let error = PError::new("MILLISECONDhah", ErrorKind::Complete); + let partial_input = error.partial_input(); + assert_eq!(partial_input, "MILLISECOND..."); + + let error = PError::new("MILLISECOND", ErrorKind::Complete); + let partial_input = error.partial_input(); + assert_eq!(partial_input, "MILLISECOND"); + } +} diff --git a/src/lib.rs b/src/lib.rs index 5447bf0..c115a6e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![doc(html_logo_url = "https://raw.githubusercontent.com/baoyachi/duration-rs/master/duration-str.png")] //! Parse string to `Duration` . //! //! The String value unit support for one of:["y","mon","w","d","h","m","s", "ms", "µs", "ns"] diff --git a/src/parser.rs b/src/parser.rs index b75b6d1..d189291 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -18,7 +18,7 @@ fn opt_cond_unit<'a>(input: &mut &'a str) -> PResult> .parse_next(input) .map_err(|err: ErrMode>| { err.map(|x| { - let partial_input = x.partial_input; + let partial_input = x.partial_input(); x.append_cause(CondUnit::expect_err(partial_input)) }) }); diff --git a/src/unit.rs b/src/unit.rs index 302a3ae..a6678f9 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -110,7 +110,7 @@ pub(crate) fn unit_abbr1<'a>(input: &mut &'a str) -> PResult>| { err.map(|x| { - let partial_input = x.partial_input; + let partial_input = x.partial_input(); x.append_cause(TimeUnit::expect_err(partial_input)) }) })?; From 141c2ed8b5f0733f63e8045fdb5f0b05e012151e Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 3 Jun 2024 14:14:37 +0800 Subject: [PATCH 2/6] update cargo dep --- Cargo.toml | 6 +++--- src/parser.rs | 2 +- src/unit.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 34c2cc9..c7c3761 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,10 +16,10 @@ default = ["chrono", "serde", "time"] [dependencies] thiserror = "1.0.37" -chrono = { version = "0.4.38", optional = true } -time = { version = "0.3.17", optional = true } +chrono = { version = "0.4.38", optional = true, default-features = false, features = ["now"] } +time = { version = "0.3.17", optional = true, default-features = false } -serde = { version = "1.0.147", features = ["derive"], optional = true } +serde = { version = "1.0.147", features = ["derive"], optional = true, default-features = false } rust_decimal = { version = "1.29.1", default-features = false } winnow = "0.6.8" diff --git a/src/parser.rs b/src/parser.rs index d189291..5763468 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -13,7 +13,6 @@ fn cond_unit1<'a>(input: &mut &'a str) -> PResult> { } fn opt_cond_unit<'a>(input: &mut &'a str) -> PResult> { - let multispace = multispace0::<_, PError<_>>; let result = cond_unit1 .parse_next(input) .map_err(|err: ErrMode>| { @@ -23,6 +22,7 @@ fn opt_cond_unit<'a>(input: &mut &'a str) -> PResult> }) }); if result.is_err() { + let multispace = multispace0::<_, PError<_>>; if (multispace, eof).parse_next(input).is_ok() { // The input result is empty except for spaces. Give `TimeUnit` default value return Ok(CondUnit::Plus); diff --git a/src/unit.rs b/src/unit.rs index a6678f9..7f453b2 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -121,9 +121,9 @@ pub(crate) fn unit_abbr1<'a>(input: &mut &'a str) -> PResult(input: &mut &'a str) -> PResult> { - let multispace = multispace0::<_, PError<_>>; let result = unit_abbr1(input); if result.is_err() { + let multispace = multispace0::<_, PError<_>>; if (multispace, eof).parse_next(input).is_ok() { // The input result is empty except for spaces. Give `TimeUnit` default value return Ok(TimeUnit::default()); From 8d01482e1d33cc783ac889f12574458eebd4f3a5 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Wed, 5 Jun 2024 00:25:01 +0800 Subject: [PATCH 3/6] cargo fmt --- src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index c115a6e..ed4f2be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,6 @@ -#![doc(html_logo_url = "https://raw.githubusercontent.com/baoyachi/duration-rs/master/duration-str.png")] +#![doc( + html_logo_url = "https://raw.githubusercontent.com/baoyachi/duration-rs/master/duration-str.png" +)] //! Parse string to `Duration` . //! //! The String value unit support for one of:["y","mon","w","d","h","m","s", "ms", "µs", "ns"] From 39da40dfe9826ec3cb5fac7f49d259670e62bb38 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Wed, 5 Jun 2024 00:27:13 +0800 Subject: [PATCH 4/6] fix cargo clippy --- Cargo.toml | 2 +- src/error.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c7c3761..d95514e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ thiserror = "1.0.37" chrono = { version = "0.4.38", optional = true, default-features = false, features = ["now"] } time = { version = "0.3.17", optional = true, default-features = false } -serde = { version = "1.0.147", features = ["derive"], optional = true, default-features = false } +serde = { version = "1.0.147", features = ["derive"], optional = true} rust_decimal = { version = "1.29.1", default-features = false } winnow = "0.6.8" diff --git a/src/error.rs b/src/error.rs index 9360263..69279bf 100644 --- a/src/error.rs +++ b/src/error.rs @@ -12,7 +12,7 @@ where A: AsRef, { fn raw(&self) -> String { - format!("{}", self.as_ref()) + self.as_ref().to_string() } } From 8b70d858ef1bf0126b26a6677f728fb370c0398d Mon Sep 17 00:00:00 2001 From: baoyachi Date: Wed, 5 Jun 2024 00:41:40 +0800 Subject: [PATCH 5/6] fix github ci --- .github/workflows/check.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index cf359f3..6371944 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -126,6 +126,7 @@ jobs: - name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v4 + if: ${{github.ref == 'refs/heads/main' }} with: personal_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./playground/site/dist From 23277a915e55b1bbe8b90bde69112c1a9a696f9c Mon Sep 17 00:00:00 2001 From: baoyachi Date: Wed, 5 Jun 2024 00:48:58 +0800 Subject: [PATCH 6/6] fix github ci --- .github/workflows/check.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 6371944..02c250d 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -126,7 +126,7 @@ jobs: - name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v4 - if: ${{github.ref == 'refs/heads/main' }} + if: ${{github.ref == 'refs/heads/master' }} with: personal_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./playground/site/dist