Skip to content

Commit 0ddbf17

Browse files
committed
impl FromStr for NonEmptyString
1 parent 207e479 commit 0ddbf17

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/rule/non_empty/non_empty_string.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
use crate::refined::Refined;
2+
use crate::result::Error;
23
use crate::rule::NonEmptyRule;
34
use std::ops::Add;
5+
use std::str::FromStr;
46

57
/// This is a predicate type representing a non-empty string
68
pub type NonEmptyString = Refined<NonEmptyStringRule>;
79

10+
impl FromStr for NonEmptyString {
11+
type Err = Error<String>;
12+
fn from_str(s: &str) -> Result<Self, Self::Err> {
13+
Refined::new(s.to_string())
14+
}
15+
}
16+
817
impl Add for NonEmptyString {
918
type Output = Self;
1019

@@ -19,6 +28,7 @@ pub type NonEmptyStringRule = NonEmptyRule<String>;
1928
#[cfg(test)]
2029
mod test {
2130
use crate::rule::{NonEmptyString, NonEmptyStringRule, Rule};
31+
use std::str::FromStr;
2232

2333
#[test]
2434
fn test_non_empty_string() {
@@ -35,4 +45,11 @@ mod test {
3545
assert_eq!(non_empty_string.into_value(), "HelloWorld");
3646
Ok(())
3747
}
48+
49+
#[test]
50+
fn test_from_str() -> anyhow::Result<()> {
51+
let non_empty_string = NonEmptyString::from_str("Hello")?;
52+
assert_eq!(non_empty_string.into_value(), "Hello");
53+
Ok(())
54+
}
3855
}

0 commit comments

Comments
 (0)