|
5 | 5 | //! |
6 | 6 | //! Examples: |
7 | 7 | //! ``` |
8 | | -//! use crate::leftpad_rs::*; |
| 8 | +//! use leftpad_rs::{pad,pad_char}; |
9 | 9 | //! |
10 | 10 | //! let r = pad("foo", 5); // -> " foo" |
11 | 11 | //! |
12 | 12 | //! let s = pad_char("foo", 6, 'X'); // -> "XXXfoo" |
13 | 13 | //! ``` |
14 | 14 |
|
15 | 15 | /// This module implements `pad` and `pad_char`. |
16 | | -pub mod leftpad_rs { |
17 | | - |
18 | | - /// Left-pads the string with spaces. |
19 | | - pub fn pad(s: &str, n: usize) -> String { |
20 | | - format!("{:>width$}", s, width = n) |
21 | | - } |
| 16 | +/// Left-pads the string with spaces. |
| 17 | +pub fn pad(s: &str, n: usize) -> String { |
| 18 | + format!("{:>width$}", s, width = n) |
| 19 | +} |
22 | 20 |
|
23 | | - /// Left-pads the string with the supplied character. |
24 | | - pub fn pad_char(s: &str, n: usize, c: char) -> Result<String, &str> { |
25 | | - let l = s.len(); |
| 21 | +/// Left-pads the string with the supplied character. |
| 22 | +pub fn pad_char(s: &str, n: usize, c: char) -> Result<String, &str> { |
| 23 | + let l = s.len(); |
26 | 24 |
|
27 | | - if n <= 0 { |
28 | | - return Err("invalid size"); |
29 | | - } |
30 | | - if n <= l { |
31 | | - return Ok(s.to_string()); |
32 | | - } |
33 | | - let f = c.to_string().repeat(n - l); |
34 | | - Ok(format!("{}{}", f, s)) |
| 25 | + if n == 0 { |
| 26 | + return Err("invalid size"); |
35 | 27 | } |
36 | | - |
37 | | - /// Useful alias |
38 | | - pub fn pad_with(s: &str, n: usize, c: char) -> Result<String, &str> { |
39 | | - pad_char(s, n, c) |
| 28 | + if n <= l { |
| 29 | + return Ok(s.to_string()); |
40 | 30 | } |
41 | | - |
42 | | - #[cfg(test)] |
43 | | - mod tests { |
44 | | - use super::*; |
| 31 | + let f = c.to_string().repeat(n - l); |
| 32 | + Ok(format!("{}{}", f, s)) |
| 33 | +} |
45 | 34 |
|
46 | | - use rstest::rstest; |
| 35 | +/// Useful alias |
| 36 | +pub fn pad_with(s: &str, n: usize, c: char) -> Result<String, &str> { |
| 37 | + pad_char(s, n, c) |
| 38 | +} |
47 | 39 |
|
48 | | - #[rstest] |
49 | | - #[case(2, "foo")] |
50 | | - #[case(3, "foo")] |
51 | | - #[case(4, " foo")] |
52 | | - #[case(5, " foo")] |
53 | | - fn test_pad(#[case] n: usize, #[case] want: &str) { |
54 | | - assert_eq!(want, pad("foo", n)); |
55 | | - } |
| 40 | +#[cfg(test)] |
| 41 | +mod tests { |
| 42 | + use super::*; |
56 | 43 |
|
57 | | - #[test] |
58 | | - fn test_nopad() { |
59 | | - assert_ne!(pad("foo", 6), "foobar") |
60 | | - } |
| 44 | + use rstest::rstest; |
61 | 45 |
|
62 | | - #[rstest] |
63 | | - #[case(2, "foo")] |
64 | | - #[case(3, "foo")] |
65 | | - #[case(4, "Xfoo")] |
66 | | - #[case(5, "XXfoo")] |
67 | | - fn test_pad_char(#[case] n: usize, #[case] want: &str) { |
68 | | - assert_eq!(Ok(want.to_string()), pad_char("foo", n, 'X')); |
69 | | - } |
| 46 | + #[rstest] |
| 47 | + #[case(2, "foo")] |
| 48 | + #[case(3, "foo")] |
| 49 | + #[case(4, " foo")] |
| 50 | + #[case(5, " foo")] |
| 51 | + fn test_pad(#[case] n: usize, #[case] want: &str) { |
| 52 | + assert_eq!(want, pad("foo", n)); |
| 53 | + } |
70 | 54 |
|
71 | | - #[test] |
72 | | - fn test_pad_char_0() { |
73 | | - assert_eq!(Err("invalid size"), pad_char("foo", 0, 'X')) |
74 | | - } |
| 55 | + #[test] |
| 56 | + fn test_nopad() { |
| 57 | + assert_ne!(pad("foo", 6), "foobar") |
| 58 | + } |
75 | 59 |
|
76 | | - #[test] |
77 | | - fn test_nopad_char() { |
78 | | - assert_ne!(Ok("foobar".to_string()), pad_char("foo", 6, 'X')) |
79 | | - } |
| 60 | + #[rstest] |
| 61 | + #[case(2, "foo")] |
| 62 | + #[case(3, "foo")] |
| 63 | + #[case(4, "Xfoo")] |
| 64 | + #[case(5, "XXfoo")] |
| 65 | + fn test_pad_char(#[case] n: usize, #[case] want: &str) { |
| 66 | + assert_eq!(Ok(want.to_string()), pad_char("foo", n, 'X')); |
| 67 | + } |
80 | 68 |
|
81 | | - #[rstest] |
82 | | - #[case(2, "foo")] |
83 | | - #[case(3, "foo")] |
84 | | - #[case(4, "Xfoo")] |
85 | | - #[case(5, "XXfoo")] |
86 | | - fn test_pad_with(#[case] n: usize, #[case] want: &str) { |
87 | | - assert_eq!(Ok(want.to_string()), pad_with("foo", n, 'X')); |
88 | | - } |
| 69 | + #[test] |
| 70 | + fn test_pad_char_0() { |
| 71 | + assert_eq!(Err("invalid size"), pad_char("foo", 0, 'X')) |
| 72 | + } |
89 | 73 |
|
90 | | - #[test] |
91 | | - fn test_pad_with_0() { |
92 | | - assert_eq!(Err("invalid size"), pad_with("foo", 0, 'X')) |
93 | | - } |
| 74 | + #[test] |
| 75 | + fn test_nopad_char() { |
| 76 | + assert_ne!(Ok("foobar".to_string()), pad_char("foo", 6, 'X')) |
| 77 | + } |
94 | 78 |
|
95 | | - #[test] |
96 | | - fn test_nopad_with() { |
97 | | - assert_ne!(Ok("foobar".to_string()), pad_with("foo", 6, 'X')) |
98 | | - } |
| 79 | + #[rstest] |
| 80 | + #[case(2, "foo")] |
| 81 | + #[case(3, "foo")] |
| 82 | + #[case(4, "Xfoo")] |
| 83 | + #[case(5, "XXfoo")] |
| 84 | + fn test_pad_with(#[case] n: usize, #[case] want: &str) { |
| 85 | + assert_eq!(Ok(want.to_string()), pad_with("foo", n, 'X')); |
| 86 | + } |
99 | 87 |
|
| 88 | + #[test] |
| 89 | + fn test_pad_with_0() { |
| 90 | + assert_eq!(Err("invalid size"), pad_with("foo", 0, 'X')) |
100 | 91 | } |
101 | | -} |
102 | 92 |
|
103 | | -pub use crate::leftpad_rs::*; |
| 93 | + #[test] |
| 94 | + fn test_nopad_with() { |
| 95 | + assert_ne!(Ok("foobar".to_string()), pad_with("foo", 6, 'X')) |
| 96 | + } |
| 97 | +} |
0 commit comments