Debian Codegraph @

src/utils/helper.rs

raw symbols SCIP
1 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/;
2
3 #[cfg(not(feature = "std"))]
4 use num_traitsrust-analyzer cargo num-traits 0.2.19 crate/::floatrust-analyzer cargo num-traits 0.2.19 float/::Floatrust-analyzer cargo num-traits 0.2.19 float/Float#;
5
6 // Strip prefix ignore case.
7 pub fn strip_prefixrust-analyzer cargo csscolorparser 0.8.3 utils/helper/strip_prefix().<'a>(s: &'a str, prefix: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'a str> {
8 if prefix.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().s.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() {
9 return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
10 }
11 let s_start = &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#prefix.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().()]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
12 if s_start.eq_ignore_ascii_caserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]eq_ignore_ascii_case().(prefix) {
13 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().prefix.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().()..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)
14 } else {
15 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
16 }
17 }
18
19 pub(craterust-analyzer cargo csscolorparser 0.8.3 crate/) struct AlphaFmtrust-analyzer cargo csscolorparser 0.8.3 utils/helper/AlphaFmt#(pub f32);
20
21 impl fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for AlphaFmtrust-analyzer cargo csscolorparser 0.8.3 utils/helper/AlphaFmt# {
22 fn fmtrust-analyzer cargo csscolorparser 0.8.3 utils/helper/impl#[AlphaFmt][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
23 let t = (self.0rust-analyzer cargo csscolorparser 0.8.3 utils/helper/AlphaFmt#0..clamprust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]clamp().(0.0, 1.0) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().100.0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().0.5) as u8;
24 if t rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]lt().100 {
25 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, " / {t}%")
26 } else {
27 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
28 }
29 }
30 }
31
32 pub(craterust-analyzer cargo csscolorparser 0.8.3 crate/) struct FloatFmtrust-analyzer cargo csscolorparser 0.8.3 utils/helper/FloatFmt#(pub f32);
33
34 impl fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for FloatFmtrust-analyzer cargo csscolorparser 0.8.3 utils/helper/FloatFmt# {
35 fn fmtrust-analyzer cargo csscolorparser 0.8.3 utils/helper/impl#[FloatFmt][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
36 if self.0rust-analyzer cargo csscolorparser 0.8.3 utils/helper/FloatFmt#0..is_nanrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]is_nan().() {
37 return writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "none");
38 }
39
40 // Round to 3 decimal places first to clear floating point noise
41 let rounded = (self.0rust-analyzer cargo csscolorparser 0.8.3 utils/helper/FloatFmt#0. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().1000.0).roundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]round().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().1000.0;
42
43 // Get the 3-digit fractional part as an integer
44 let fract = (rounded.absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]abs().().fractrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]fract().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().1000.0).roundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]round().() as u16;
45
46 // Determine precision based on the integer fractional part
47 let precision = if fract rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]eq().0 {
48 0
49 } else if fract rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`Rem<Self>`]rem().10 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]ne().0 {
50 3
51 } else if fract rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`Rem<Self>`]rem().100 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]ne().0 {
52 2
53 } else {
54 1
55 };
56
57 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:.*}", precision, rounded)
58 }
59 }
60
61 #[cfg(test)]
62 mod trust-analyzer cargo csscolorparser 0.8.3 utils/helper/t/ {
63 use superrust-analyzer cargo csscolorparser 0.8.3 utils/helper/::*;
64
65 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
66 fn strip_prefix_rust-analyzer cargo csscolorparser 0.8.3 utils/helper/t/strip_prefix_().() {
67 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(strip_prefixrust-analyzer cargo csscolorparser 0.8.3 utils/helper/strip_prefix().("rgb(77)", "rgb"), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("(77)"));
68 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(strip_prefixrust-analyzer cargo csscolorparser 0.8.3 utils/helper/strip_prefix().("RGB(0,0)", "rgb"), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("(0,0)"));
69 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(strip_prefixrust-analyzer cargo csscolorparser 0.8.3 utils/helper/strip_prefix().("Hsv()", "HSV"), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("()"));
70
71 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(strip_prefixrust-analyzer cargo csscolorparser 0.8.3 utils/helper/strip_prefix().("", "rgb"), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
72 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(strip_prefixrust-analyzer cargo csscolorparser 0.8.3 utils/helper/strip_prefix().("10", "rgb"), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
73 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(strip_prefixrust-analyzer cargo csscolorparser 0.8.3 utils/helper/strip_prefix().("hsv(0,0)", "hsva"), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
74 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(strip_prefixrust-analyzer cargo csscolorparser 0.8.3 utils/helper/strip_prefix().("hsv", "hsva"), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
75 }
76 }