Skip to content

Commit 2f93c1d

Browse files
committed
Add pad_with() as an alias to pad_char(). Fix README.
1 parent feee914 commit 2f93c1d

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ fn main() {
2323

2424
println!("{}", pad(s, 5));
2525
println!("{:?}", pad_char(s, 5, 'b'));
26+
27+
// You can use pad_with() too now.
28+
//println!("{:?}", pad_with(s, 5, 'b'));
2629
}
2730
```
2831
## crates.io
@@ -31,7 +34,7 @@ to your `Cargo.toml`:
3134

3235
``` toml
3336
[dependencies]
34-
leftpad-rs = "1.0.1"
37+
leftpad-rs = "1.1.0"
3538
```
3639
then you can use it in your own crates.
3740

src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ pub mod leftpad_rs {
3434
Ok(format!("{}{}", f, s))
3535
}
3636

37+
/// Useful alias
38+
pub fn pad_with(s: &str, n: usize, c: char) -> Result<String, &str> {
39+
pad_char(s, n, c)
40+
}
41+
3742
#[cfg(test)]
3843
mod tests {
3944
use super::*;
@@ -72,6 +77,26 @@ pub mod leftpad_rs {
7277
fn test_nopad_char() {
7378
assert_ne!(Ok("foobar".to_string()), pad_char("foo", 6, 'X'))
7479
}
80+
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+
}
89+
90+
#[test]
91+
fn test_pad_with_0() {
92+
assert_eq!(Err("invalid size"), pad_with("foo", 0, 'X'))
93+
}
94+
95+
#[test]
96+
fn test_nopad_with() {
97+
assert_ne!(Ok("foobar".to_string()), pad_with("foo", 6, 'X'))
98+
}
99+
75100
}
76101
}
77102

0 commit comments

Comments
 (0)