Skip to content

Commit d368685

Browse files
committed
Merge branch 'release/v1.1.0'
2 parents feee914 + 2861963 commit d368685

File tree

4 files changed

+58
-53
lines changed

4 files changed

+58
-53
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: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,74 +5,69 @@
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
}
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+
}
3634

37-
#[cfg(test)]
38-
mod tests {
39-
use super::*;
35+
#[cfg(test)]
36+
mod tests {
37+
use super::*;
4038

41-
use rstest::rstest;
39+
use rstest::rstest;
4240

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

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

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

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

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'))
7572
}
7673
}
77-
78-
pub use crate::leftpad_rs::*;

0 commit comments

Comments
 (0)