|
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 | } |
| 28 | + if n <= l { |
| 29 | + return Ok(s.to_string()); |
| 30 | + } |
| 31 | + let f = c.to_string().repeat(n - l); |
| 32 | + Ok(format!("{}{}", f, s)) |
| 33 | +} |
36 | 34 |
|
37 | | - #[cfg(test)] |
38 | | - mod tests { |
39 | | - use super::*; |
| 35 | +#[cfg(test)] |
| 36 | +mod tests { |
| 37 | + use super::*; |
40 | 38 |
|
41 | | - use rstest::rstest; |
| 39 | + use rstest::rstest; |
42 | 40 |
|
43 | | - #[rstest] |
44 | | - #[case(2, "foo")] |
45 | | - #[case(3, "foo")] |
46 | | - #[case(4, " foo")] |
47 | | - #[case(5, " foo")] |
48 | | - fn test_pad(#[case] n: usize, #[case] want: &str) { |
49 | | - assert_eq!(want, pad("foo", n)); |
50 | | - } |
| 41 | + #[rstest] |
| 42 | + #[case(2, "foo")] |
| 43 | + #[case(3, "foo")] |
| 44 | + #[case(4, " foo")] |
| 45 | + #[case(5, " foo")] |
| 46 | + fn test_pad(#[case] n: usize, #[case] want: &str) { |
| 47 | + assert_eq!(want, pad("foo", n)); |
| 48 | + } |
51 | 49 |
|
52 | | - #[test] |
53 | | - fn test_nopad() { |
54 | | - assert_ne!(pad("foo", 6), "foobar") |
55 | | - } |
| 50 | + #[test] |
| 51 | + fn test_nopad() { |
| 52 | + assert_ne!(pad("foo", 6), "foobar") |
| 53 | + } |
56 | 54 |
|
57 | | - #[rstest] |
58 | | - #[case(2, "foo")] |
59 | | - #[case(3, "foo")] |
60 | | - #[case(4, "Xfoo")] |
61 | | - #[case(5, "XXfoo")] |
62 | | - fn test_pad_char(#[case] n: usize, #[case] want: &str) { |
63 | | - assert_eq!(Ok(want.to_string()), pad_char("foo", n, 'X')); |
64 | | - } |
| 55 | + #[rstest] |
| 56 | + #[case(2, "foo")] |
| 57 | + #[case(3, "foo")] |
| 58 | + #[case(4, "Xfoo")] |
| 59 | + #[case(5, "XXfoo")] |
| 60 | + fn test_pad_char(#[case] n: usize, #[case] want: &str) { |
| 61 | + assert_eq!(Ok(want.to_string()), pad_char("foo", n, 'X')); |
| 62 | + } |
65 | 63 |
|
66 | | - #[test] |
67 | | - fn test_pad_char_0() { |
68 | | - assert_eq!(Err("invalid size"), pad_char("foo", 0, 'X')) |
69 | | - } |
| 64 | + #[test] |
| 65 | + fn test_pad_char_0() { |
| 66 | + assert_eq!(Err("invalid size"), pad_char("foo", 0, 'X')) |
| 67 | + } |
70 | 68 |
|
71 | | - #[test] |
72 | | - fn test_nopad_char() { |
73 | | - assert_ne!(Ok("foobar".to_string()), pad_char("foo", 6, 'X')) |
74 | | - } |
| 69 | + #[test] |
| 70 | + fn test_nopad_char() { |
| 71 | + assert_ne!(Ok("foobar".to_string()), pad_char("foo", 6, 'X')) |
75 | 72 | } |
76 | 73 | } |
77 | | - |
78 | | -pub use crate::leftpad_rs::*; |
0 commit comments