Skip to content

Commit a5bc9b4

Browse files
committed
Merge branch 'main' of https://github.com/keltia/leftpad-rs into main
2 parents 2b01f96 + d368685 commit a5bc9b4

File tree

4 files changed

+78
-74
lines changed

4 files changed

+78
-74
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
[package]
22
name = "leftpad-rs"
3-
version = "1.0.1"
3+
version = "1.1.0"
44
edition = "2018"
55
authors = ["Ollivier Robert <roberto@keltia.net>"]
66
keywords = ["leftpad", "joke"]
77
repository = "https://github.com/keltia/leftpad-rs"
88
license = "MIT"
99
readme = "README.md"
1010
description = "Rust implementation of the Go Leftpad package."
11+
documentation = "https://docs.rs/leftpad-rs"
1112

1213
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1314

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Leftpad
22

33
[![CircleCI](https://circleci.com/gh/keltia/leftpad-rs/tree/main.svg?style=shield)](https://circleci.com/gh/keltia/leftpad-rs/tree/main)
4+
[![dependency status](https://deps.rs/repo/github/keltia/leftpad-rs/status.svg)](https://deps.rs/repo/github/keltia/leftpad-rs)
45
[![](https://img.shields.io/crates/v/leftpad-rs.svg)](https://crates.io/crates/leftpad-rs)
56
[![Docs](https://docs.rs/leftpad-rs/badge.svg)](https://docs.rs/leftpad-rs)
67

examples/small.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use leftpad_rs::{pad, pad_char};
2+
3+
fn main() {
4+
let s = "foo";
5+
6+
println!("{}", pad(s, 5));
7+
println!("{:?}", pad_char(s, 5, 'b'));
8+
}

src/lib.rs

Lines changed: 67 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,99 +5,93 @@
55
//!
66
//! Examples:
77
//! ```
8-
//! use crate::leftpad_rs::*;
8+
//! use leftpad_rs::{pad,pad_char};
99
//!
1010
//! let r = pad("foo", 5); // -> " foo"
1111
//!
1212
//! let s = pad_char("foo", 6, 'X'); // -> "XXXfoo"
1313
//! ```
1414
1515
/// 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+
}
2220

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();
2624

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");
3527
}
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());
4030
}
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+
}
4534

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+
}
4739

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::*;
5643

57-
#[test]
58-
fn test_nopad() {
59-
assert_ne!(pad("foo", 6), "foobar")
60-
}
44+
use rstest::rstest;
6145

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+
}
7054

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+
}
7559

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+
}
8068

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+
}
8973

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+
}
9478

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+
}
9987

88+
#[test]
89+
fn test_pad_with_0() {
90+
assert_eq!(Err("invalid size"), pad_with("foo", 0, 'X'))
10091
}
101-
}
10292

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

Comments
 (0)