Debian Codegraph @

src/handlers/diff_stat.rs

raw symbols SCIP
1 use lazy_staticrust-analyzer cargo lazy_static 1.4.0 crate/::lazy_staticrust-analyzer cargo lazy_static 1.4.0 lazy_static!;
2 use regexrust-analyzer cargo regex 1.12.3 crate/::Regexrust-analyzer cargo regex 1.12.3 regex/string/Regex#;
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#;
4
5 use craterust-analyzer cargo git-delta 0.19.2 crate/::configrust-analyzer cargo git-delta 0.19.2 config/::Configrust-analyzer cargo git-delta 0.19.2 config/Config#;
6 use craterust-analyzer cargo git-delta 0.19.2 crate/::deltarust-analyzer cargo git-delta 0.19.2 delta/::{Staterust-analyzer cargo git-delta 0.19.2 delta/State#, StateMachinerust-analyzer cargo git-delta 0.19.2 delta/StateMachine#};
7 use craterust-analyzer cargo git-delta 0.19.2 crate/::featuresrust-analyzer cargo git-delta 0.19.2 features/;
8 use craterust-analyzer cargo git-delta 0.19.2 crate/::utilsrust-analyzer cargo git-delta 0.19.2 utils/;
9
10 impl StateMachinerust-analyzer cargo git-delta 0.19.2 delta/StateMachine#<'_> {
11 #[inline]
12 fn test_diff_stat_linerust-analyzer cargo git-delta 0.19.2 handlers/diff_stat/impl#[`StateMachine<'_>`]test_diff_stat_line().(&self) -> bool {
13 (self.staterust-analyzer cargo git-delta 0.19.2 delta/StateMachine#state. rust-analyzer cargo git-delta 0.19.2 delta/impl#[State][`PartialEq<Self>`]eq().==rust-analyzer cargo git-delta 0.19.2 delta/impl#[State][`PartialEq<Self>`]eq(). rust-analyzer cargo git-delta 0.19.2 delta/impl#[State][`PartialEq<Self>`]eq().Staterust-analyzer cargo git-delta 0.19.2 delta/State#::CommitMetarust-analyzer cargo git-delta 0.19.2 delta/State#CommitMeta# || self.staterust-analyzer cargo git-delta 0.19.2 delta/StateMachine#state. rust-analyzer cargo git-delta 0.19.2 delta/impl#[State][`PartialEq<Self>`]eq().==rust-analyzer cargo git-delta 0.19.2 delta/impl#[State][`PartialEq<Self>`]eq(). rust-analyzer cargo git-delta 0.19.2 delta/impl#[State][`PartialEq<Self>`]eq().Staterust-analyzer cargo git-delta 0.19.2 delta/State#::Unknownrust-analyzer cargo git-delta 0.19.2 delta/State#Unknown#)
14 && self.linerust-analyzer cargo git-delta 0.19.2 delta/StateMachine#line..starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(' ')
15 }
16
17 pub fn handle_diff_stat_linerust-analyzer cargo git-delta 0.19.2 handlers/diff_stat/impl#[`StateMachine<'_>`]handle_diff_stat_line().(&mut self) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<bool> {
18 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.test_diff_stat_linerust-analyzer cargo git-delta 0.19.2 handlers/diff_stat/impl#[`StateMachine<'_>`]test_diff_stat_line().() {
19 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(false);
20 }
21 let mut handled_line = false;
22 if self.configrust-analyzer cargo git-delta 0.19.2 delta/StateMachine#config..relative_pathsrust-analyzer cargo git-delta 0.19.2 config/Config#relative_paths. {
23 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cwd) = self.configrust-analyzer cargo git-delta 0.19.2 delta/StateMachine#config..cwd_relative_to_repo_rootrust-analyzer cargo git-delta 0.19.2 config/Config#cwd_relative_to_repo_root..as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().() {
24 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(replacement_line) =
25 relativize_path_in_diff_stat_linerust-analyzer cargo git-delta 0.19.2 handlers/diff_stat/relativize_path_in_diff_stat_line().(&self.raw_linerust-analyzer cargo git-delta 0.19.2 delta/StateMachine#raw_line., cwd, self.configrust-analyzer cargo git-delta 0.19.2 delta/StateMachine#config.)
26 {
27 self.painterrust-analyzer cargo git-delta 0.19.2 delta/StateMachine#painter..emitrust-analyzer cargo git-delta 0.19.2 paint/impl#[`Painter<'p>`]emit().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
28 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(self.painterrust-analyzer cargo git-delta 0.19.2 delta/StateMachine#painter..writerrust-analyzer cargo git-delta 0.19.2 paint/Painter#writer., "{replacement_line}")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
29 handled_line = true
30 }
31 }
32 }
33 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(handled_line)
34 }
35 }
36
37 // A regex to capture the path, and the content from the pipe onwards, in lines
38 // like these:
39 // " src/delta.rs | 14 ++++++++++----"
40 // " src/config.rs | 2 ++"
41 lazy_staticrust-analyzer cargo lazy_static 1.4.0 lazy_static!! {
42 static ref DIFF_STAT_LINE_REGEXrust-analyzer cargo git-delta 0.19.2 handlers/diff_stat/DIFF_STAT_LINE_REGEX#: Regexrust-analyzer cargo regex 1.12.3 regex/string/Regex# =
43 Regexrust-analyzer cargo regex 1.12.3 regex/string/Regex#::newrust-analyzer cargo regex 1.12.3 regex/string/impl#[Regex]new().(r" ([^\| ][^\|]+[^\| ]) +(\| +[0-9]+ .+)").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
44 }
45
46 pub fn relativize_path_in_diff_stat_linerust-analyzer cargo git-delta 0.19.2 handlers/diff_stat/relativize_path_in_diff_stat_line().(
47 line: &str,
48 cwd_relative_to_repo_root: &str,
49 config: &Configrust-analyzer cargo git-delta 0.19.2 config/Config#,
50 ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
51 let caps = DIFF_STAT_LINE_REGEXrust-analyzer cargo git-delta 0.19.2 handlers/diff_stat/DIFF_STAT_LINE_REGEX..capturesrust-analyzer cargo regex 1.12.3 regex/string/impl#[Regex]captures().(line)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
52 let path_relative_to_repo_root = caps.getrust-analyzer cargo regex 1.12.3 regex/string/impl#[`Captures<'h>`]get().(1).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().as_strrust-analyzer cargo regex 1.12.3 regex/string/impl#[`Match<'h>`]as_str().();
53
54 let relative_path =
55 pathdiffrust-analyzer cargo pathdiff 0.2.1 crate/::diff_pathsrust-analyzer cargo pathdiff 0.2.1 diff_paths().(path_relative_to_repo_root, cwd_relative_to_repo_root)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
56 let relative_path = relative_path.to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_str().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
57 let formatted_path = match (
58 config.hyperlinksrust-analyzer cargo git-delta 0.19.2 config/Config#hyperlinks.,
59 utilsrust-analyzer cargo git-delta 0.19.2 utils/::pathrust-analyzer cargo git-delta 0.19.2 utils/path/::absolute_pathrust-analyzer cargo git-delta 0.19.2 utils/path/absolute_path().(path_relative_to_repo_root, config),
60 ) {
61 (true, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(absolute_path)) => featuresrust-analyzer cargo git-delta 0.19.2 features/::hyperlinksrust-analyzer cargo git-delta 0.19.2 features/hyperlinks/::format_osc8_file_hyperlinkrust-analyzer cargo git-delta 0.19.2 features/hyperlinks/format_osc8_file_hyperlink().(
62 absolute_path,
63 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
64 relative_path,
65 config,
66 ),
67 _ => Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`Cow<'a, str>`][`From<&'a str>`]from().(relative_path),
68 };
69 let suffix = caps.getrust-analyzer cargo regex 1.12.3 regex/string/impl#[`Captures<'h>`]get().(2).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().as_strrust-analyzer cargo regex 1.12.3 regex/string/impl#[`Match<'h>`]as_str().();
70 let pad_width = config
71 .diff_stat_align_widthrust-analyzer cargo git-delta 0.19.2 config/Config#diff_stat_align_width.
72 .saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(relative_path.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().());
73 let padding = " ".repeatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]repeat().(pad_width);
74 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(" {formatted_path}{padding}{suffix}"))
75 }
76
77 #[cfg(test)]
78 mod testsrust-analyzer cargo git-delta 0.19.2 handlers/diff_stat/tests/ {
79 use superrust-analyzer cargo git-delta 0.19.2 handlers/diff_stat/::*;
80
81 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
82 fn test_diff_stat_line_regex_1rust-analyzer cargo git-delta 0.19.2 handlers/diff_stat/tests/test_diff_stat_line_regex_1().() {
83 let caps = DIFF_STAT_LINE_REGEXrust-analyzer cargo git-delta 0.19.2 handlers/diff_stat/DIFF_STAT_LINE_REGEX..capturesrust-analyzer cargo regex 1.12.3 regex/string/impl#[Regex]captures().(" src/delta.rs | 14 ++++++++++----");
84 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(caps.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().());
85 let caps = caps.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
86 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(caps.getrust-analyzer cargo regex 1.12.3 regex/string/impl#[`Captures<'h>`]get().(1).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().as_strrust-analyzer cargo regex 1.12.3 regex/string/impl#[`Match<'h>`]as_str().(), "src/delta.rs");
87 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(caps.getrust-analyzer cargo regex 1.12.3 regex/string/impl#[`Captures<'h>`]get().(2).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().as_strrust-analyzer cargo regex 1.12.3 regex/string/impl#[`Match<'h>`]as_str().(), "| 14 ++++++++++----");
88 }
89
90 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
91 fn test_diff_stat_line_regex_2rust-analyzer cargo git-delta 0.19.2 handlers/diff_stat/tests/test_diff_stat_line_regex_2().() {
92 let caps = DIFF_STAT_LINE_REGEXrust-analyzer cargo git-delta 0.19.2 handlers/diff_stat/DIFF_STAT_LINE_REGEX..capturesrust-analyzer cargo regex 1.12.3 regex/string/impl#[Regex]captures().(" src/config.rs | 2 ++");
93 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(caps.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().());
94 let caps = caps.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
95 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(caps.getrust-analyzer cargo regex 1.12.3 regex/string/impl#[`Captures<'h>`]get().(1).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().as_strrust-analyzer cargo regex 1.12.3 regex/string/impl#[`Match<'h>`]as_str().(), "src/config.rs");
96 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(caps.getrust-analyzer cargo regex 1.12.3 regex/string/impl#[`Captures<'h>`]get().(2).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().as_strrust-analyzer cargo regex 1.12.3 regex/string/impl#[`Match<'h>`]as_str().(), "| 2 ++");
97 }
98
99 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
100 fn test_relative_pathrust-analyzer cargo git-delta 0.19.2 handlers/diff_stat/tests/test_relative_path().() {
101 for (path, cwd_relative_to_repo_root, expected) in &[
102 ("file.rs", "", "file.rs"),
103 ("file.rs", "a/", "../file.rs"),
104 ("a/file.rs", "a/", "file.rs"),
105 ("a/b/file.rs", "a", "b/file.rs"),
106 ("c/d/file.rs", "a/b/", "../../c/d/file.rs"),
107 ] {
108 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
109 pathdiffrust-analyzer cargo pathdiff 0.2.1 crate/::diff_pathsrust-analyzer cargo pathdiff 0.2.1 diff_paths().(path, cwd_relative_to_repo_root),
110 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(expected.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
111 )
112 }
113 }
114 }