add doc comments and categorize crate
This commit is contained in:
parent
40a56c4f15
commit
90c32e1bd5
2 changed files with 28 additions and 0 deletions
14
Cargo.toml
14
Cargo.toml
|
@ -5,5 +5,19 @@ edition = "2021"
|
||||||
repository = "https://github.com/tudbut/readformat"
|
repository = "https://github.com/tudbut/readformat"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
description = "Very small format reader"
|
description = "Very small format reader"
|
||||||
|
keywords = [
|
||||||
|
"readf",
|
||||||
|
"format",
|
||||||
|
"parser",
|
||||||
|
"parse",
|
||||||
|
"reader",
|
||||||
|
"formatting",
|
||||||
|
]
|
||||||
|
categories = [
|
||||||
|
"parser-implementations",
|
||||||
|
"encoding",
|
||||||
|
"parsing",
|
||||||
|
"text-processing"
|
||||||
|
]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
14
src/lib.rs
14
src/lib.rs
|
@ -1,4 +1,12 @@
|
||||||
|
|
||||||
|
/// The inverse of format!().
|
||||||
|
/// The format argument is the format string, and the s argument is the string to match the format
|
||||||
|
/// against.
|
||||||
|
///
|
||||||
|
/// Examples:
|
||||||
|
/// `readf("Hello, {}!", "Hello, world!")` => `Some(vec!["world"])`
|
||||||
|
/// `readf("I hope {} are {}!", "I hope you are doing well!")` => `Some(vec!["you", "well"])`
|
||||||
|
/// `readf("Goodbye, {}!", "Hello, world!")` => `None`
|
||||||
pub fn readf(format: &str, mut s: &str) -> Option<Vec<String>> {
|
pub fn readf(format: &str, mut s: &str) -> Option<Vec<String>> {
|
||||||
if !format.contains("{}") {
|
if !format.contains("{}") {
|
||||||
return if format == s { Some(vec![]) } else { None };
|
return if format == s { Some(vec![]) } else { None };
|
||||||
|
@ -50,6 +58,12 @@ pub fn readf(format: &str, mut s: &str) -> Option<Vec<String>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convenience function for single-item format strings. Extra items are dropped.
|
||||||
|
///
|
||||||
|
/// Examples:
|
||||||
|
/// `readf1("Hello, {}!", "Hello, world!")` => `Some("world")`
|
||||||
|
/// `readf1("I hope {} are doing well!", "I hope you are doing well!")` => `Some("you")`
|
||||||
|
/// `readf1("Goodbye, {}!", "Hello, world!")` => `None`
|
||||||
pub fn readf1(format: &str, s: &str) -> Option<String> {
|
pub fn readf1(format: &str, s: &str) -> Option<String> {
|
||||||
let r = readf(format, s);
|
let r = readf(format, s);
|
||||||
r.map(|x| if x.len() == 0 { "".into() } else { x[0].clone() })
|
r.map(|x| if x.len() == 0 { "".into() } else { x[0].clone() })
|
||||||
|
|
Loading…
Add table
Reference in a new issue