Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2021 ilkecan <[email protected]>
2 //
3 // SPDX-License-Identifier: MPL-2.0
4
5 //! These functions are useful for inserting a multiline string into an already indented context in
6 //! another string.
7
8 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#;
9
10 /// Indents every line that is not empty by the given number of spaces, starting from the second
11 /// line.
12 ///
13 /// The first line of the string is not indented so that it can be placed after an introduction
14 /// sequence that has already begun the line.
15 ///
16 /// # Examples
17 /// ```rust
18 /// assert_eq!(format!(" items: {}", indent::indent_by(2, "[\n foo,\n bar,\n]\n")),
19 /// " items: [
20 /// foo,
21 /// bar,
22 /// ]
23 /// ")
24 /// ```
25 ///
26 /// For the version that also indents the first line, see [indent_all_by].
27 pub fn indent_byrust-analyzer cargo indent 0.1.1 indent_by().<'a, S>(number_of_spaces: usize, input: S) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#
28 where
29 S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>>,
30 {
31 indentrust-analyzer cargo indent 0.1.1 indent().(" ".repeatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]repeat().(number_of_spaces), input, false)
32 }
33
34 /// Indents every line that is not empty with the given prefix, starting from the second line.
35 ///
36 /// The first line of the string is not indented so that it can be placed after an introduction
37 /// sequence that has already begun the line.
38 ///
39 /// # Examples
40 /// ```rust
41 /// assert_eq!(format!("items:{}", indent::indent_with("- ", "\nfoo\nbar\n")),
42 /// "items:
43 /// - foo
44 /// - bar
45 /// ")
46 /// ```
47 ///
48 /// For the version that also indents the first line, see [indent_all_with].
49 pub fn indent_withrust-analyzer cargo indent 0.1.1 indent_with().<'a, S, T>(prefix: S, input: T) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#
50 where
51 S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>>,
52 T: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>>,
53 {
54 indentrust-analyzer cargo indent 0.1.1 indent().(prefix, input, false)
55 }
56
57 /// Indents every line that is not empty by the given number of spaces.
58 ///
59 /// # Examples
60 /// ```rust
61 /// assert_eq!(format!("items: [\n{}]\n", indent::indent_all_by(2, "foo,\nbar,\n")),
62 /// "items: [
63 /// foo,
64 /// bar,
65 /// ]
66 /// ")
67 /// ```
68 ///
69 /// For the version that doesn't indent the first line, see [indent_by].
70 pub fn indent_all_byrust-analyzer cargo indent 0.1.1 indent_all_by().<'a, S>(number_of_spaces: usize, input: S) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#
71 where
72 S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>>,
73 {
74 indentrust-analyzer cargo indent 0.1.1 indent().(" ".repeatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]repeat().(number_of_spaces), input, true)
75 }
76
77 /// Indents every line that is not empty with the given prefix.
78 ///
79 /// # Examples
80 /// ```rust
81 /// assert_eq!(format!("items:\n{}", indent::indent_all_with("- ", "foo\nbar\n")),
82 /// "items:
83 /// - foo
84 /// - bar
85 /// ")
86 /// ```
87 ///
88 /// For the version that also indents the first line, see [indent_with].
89 pub fn indent_all_withrust-analyzer cargo indent 0.1.1 indent_all_with().<'a, S, T>(prefix: S, input: T) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#
90 where
91 S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>>,
92 T: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>>,
93 {
94 indentrust-analyzer cargo indent 0.1.1 indent().(prefix, input, true)
95 }
96
97 fn indentrust-analyzer cargo indent 0.1.1 indent().<'a, S, T>(prefix: S, input: T, indent_all: bool) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#
98 where
99 S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>>,
100 T: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>>,
101 {
102 let prefix = prefix.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().();
103 let input = input.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().();
104 let length = input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().();
105 let mut output = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(length rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().length rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().2);
106
107 for (i, line) in input.linesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]lines().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() {
108 if i 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().0 {
109 output.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('\n');
110
111 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().line.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() {
112 output.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&prefix);
113 }
114 } else if indent_all && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().line.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() {
115 output.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&prefix);
116 }
117
118 output.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(line);
119 }
120
121 if input.ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]ends_with().('\n') {
122 output.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('\n');
123 }
124
125 output
126 }
127
128 #[cfg(test)]
129 mod testsrust-analyzer cargo indent 0.1.1 tests/ {
130 use superrust-analyzer cargo indent 0.1.1 crate/::*;
131
132 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
133 fn test_single_line_without_newlinerust-analyzer cargo indent 0.1.1 tests/test_single_line_without_newline().() {
134 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(indent_byrust-analyzer cargo indent 0.1.1 indent_by().(2, "foo"), "foo")
135 }
136
137 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
138 fn test_single_line_with_newlinerust-analyzer cargo indent 0.1.1 tests/test_single_line_with_newline().() {
139 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(indent_byrust-analyzer cargo indent 0.1.1 indent_by().(2, "foo\n"), "foo\n")
140 }
141
142 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
143 fn test_multiline_without_newlinerust-analyzer cargo indent 0.1.1 tests/test_multiline_without_newline().() {
144 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
145 indent_byrust-analyzer cargo indent 0.1.1 indent_by().(
146 2, "
147 foo
148 bar"
149 ),
150 "
151 foo
152 bar"
153 )
154 }
155
156 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
157 fn test_multiline_with_newlinerust-analyzer cargo indent 0.1.1 tests/test_multiline_with_newline().() {
158 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
159 indent_byrust-analyzer cargo indent 0.1.1 indent_by().(
160 2,
161 "
162 foo
163 bar
164 "
165 ),
166 "
167 foo
168 bar
169 "
170 )
171 }
172
173 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
174 fn test_empty_linerust-analyzer cargo indent 0.1.1 tests/test_empty_line().() {
175 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
176 indent_byrust-analyzer cargo indent 0.1.1 indent_by().(
177 2,
178 "
179 foo
180
181 bar
182 "
183 ),
184 "
185 foo
186
187 bar
188 "
189 )
190 }
191
192 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
193 fn test_indent_all_by_empty_linerust-analyzer cargo indent 0.1.1 tests/test_indent_all_by_empty_line().() {
194 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
195 indent_all_byrust-analyzer cargo indent 0.1.1 indent_all_by().(
196 2,
197 "
198 foo
199
200 bar"
201 ),
202 "
203 foo
204
205 bar"
206 )
207 }
208
209 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
210 fn test_indent_all_byrust-analyzer cargo indent 0.1.1 tests/test_indent_all_by().() {
211 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
212 indent_all_byrust-analyzer cargo indent 0.1.1 indent_all_by().(
213 2, "foo
214
215 bar"
216 ),
217 " foo
218
219 bar"
220 )
221 }
222
223 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
224 fn test_indent_withrust-analyzer cargo indent 0.1.1 tests/test_indent_with().() {
225 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
226 indent_withrust-analyzer cargo indent 0.1.1 indent_with().(
227 " ",
228 "
229 foo
230
231 bar
232 "
233 ),
234 "
235 foo
236
237 bar
238 "
239 )
240 }
241
242 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
243 fn test_indent_all_withrust-analyzer cargo indent 0.1.1 tests/test_indent_all_with().() {
244 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
245 indent_all_withrust-analyzer cargo indent 0.1.1 indent_all_with().(
246 " ", "foo
247
248 bar"
249 ),
250 " foo
251
252 bar"
253 )
254 }
255 }