You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+74-13Lines changed: 74 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Multi-line String AutoIndent
1
+
# Multi-line String Auto-Indent
2
2
3
3
[![made-with-rust][rust-logo]][rust-src-page]
4
4
[![crates.io][crates-badge]][crates-page]
@@ -29,6 +29,10 @@ cargo add string-auto-indent
29
29
30
30
## Usage
31
31
32
+
## Example 1: Basic Indentation
33
+
34
+
This example removes unnecessary leading spaces while preserving the relative indentation of nested lines.
35
+
32
36
```rust
33
37
usestring_auto_indent::{auto_indent, LineEnding};
34
38
@@ -38,29 +42,36 @@ let text = r#"
38
42
Level 1
39
43
Level 2
40
44
Level 3
41
-
"#;
45
+
"#;
42
46
43
-
// For cross-platform testing
44
-
letline_ending=LineEnding::detect(text);
47
+
// Expected output after applying auto indentation
48
+
letexpected=r#"
49
+
String Auto Indent
50
+
51
+
Level 1
52
+
Level 2
53
+
Level 3
54
+
"#;
45
55
46
-
//With auto-indent
56
+
//Verify that `auto_indent` correctly normalizes indentation
47
57
assert_eq!(
48
58
auto_indent(text),
49
-
// For cross-platform testing: Restore platform-specific line endings
50
-
line_ending.restore("String Auto Indent\n\nLevel 1\n Level 2\n Level 3\n")
59
+
expected,
60
+
"The auto_indent function should normalize leading whitespace."
51
61
);
52
62
53
-
// Without auto-indent
54
-
assert_eq!(
63
+
// Ensure the original text is not identical to the expected output
64
+
// This confirms that `auto_indent` actually modifies the string.
65
+
assert_ne!(
55
66
text,
56
-
// For cross-platform testing: Restore platform-specific line endings
57
-
line_ending.restore("\n String Auto Indent\n\n Level 1\n Level 2\n Level 3\n"),
67
+
expected,
68
+
"The original text should *not* be identical to the expected output before normalization."
58
69
);
59
70
```
60
71
61
72
### Example Output
62
73
63
-
**With `auto-indent` enabled.**
74
+
#### With `auto-indent`
64
75
65
76
```text
66
77
String Auto Indent
@@ -70,7 +81,7 @@ Level 1
70
81
Level 3
71
82
```
72
83
73
-
**With `auto-intent` disabled.**
84
+
#### Without `auto-intent`
74
85
75
86
```text
76
87
String Auto Indent
@@ -80,6 +91,54 @@ Level 1
80
91
Level 3
81
92
```
82
93
94
+
## Example 2: Mixed Indentation
95
+
96
+
This example demonstrates how `auto_indent` normalizes inconsistent indentation while preserving the relative structure of nested content.
97
+
98
+
```rust
99
+
usestring_auto_indent::{auto_indent, LineEnding};
100
+
101
+
lettext=r#"
102
+
String Auto Indent
103
+
104
+
1. Point 1
105
+
a. Sub point a
106
+
b. Sub point b
107
+
2. Point 2
108
+
a. Sub point a
109
+
b. Sub piont b
110
+
1b. Sub piont 1b
111
+
"#;
112
+
113
+
// Expected output after applying auto indentation
114
+
letexpected=r#"
115
+
String Auto Indent
116
+
117
+
1. Point 1
118
+
a. Sub point a
119
+
b. Sub point b
120
+
2. Point 2
121
+
a. Sub point a
122
+
b. Sub piont b
123
+
1b. Sub piont 1b
124
+
"#;
125
+
126
+
// Verify that `auto_indent` correctly normalizes indentation
127
+
assert_eq!(
128
+
auto_indent(text),
129
+
expected,
130
+
"The auto_indent function should normalize leading whitespace."
131
+
);
132
+
133
+
// Ensure the original text is not identical to the expected output
134
+
// This confirms that `auto_indent` actually modifies the string.
135
+
assert_ne!(
136
+
text,
137
+
expected,
138
+
"The original text should *not* be identical to the expected output before normalization."
139
+
);
140
+
```
141
+
83
142
## How It Works
84
143
85
144
1. Detects the platform’s line endings (`\n`, `\r\n`, `\r`) and normalizes input for processing.
@@ -93,6 +152,8 @@ Level 1
93
152
- Formatting log messages or CLI output while ensuring alignment.
94
153
- Cleaning up documentation strings or multi-line literals in indented Rust code.
95
154
- Processing structured text while ensuring consistent indentation.
155
+
- Declaring multi-line variables in code where the indentation should match the codebase for readability, but the actual string content should not retain unnecessary leading spaces.
156
+
- Ensuring consistent formatting in generated strings for use in templates, serialization, or output rendering.
96
157
97
158
## License
98
159
Licensed under **MIT**. See [`LICENSE`][license-page] for details.
0 commit comments