Skip to content

Commit bfcc4fa

Browse files
committed
Merge pull request #39 from blaenk/from_str
provides a simple implementation of FromStr for Pattern
2 parents 79d3691 + bbfb356 commit bfcc4fa

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use std::fs;
3333
use std::io::prelude::*;
3434
use std::io;
3535
use std::path::{self, Path, PathBuf, Component};
36+
use std::str::FromStr;
3637

3738
use PatternToken::{Char, AnyChar, AnySequence, AnyRecursiveSequence, AnyWithin};
3839
use PatternToken::AnyExcept;
@@ -399,6 +400,14 @@ impl fmt::Display for Pattern {
399400
}
400401
}
401402

403+
impl FromStr for Pattern {
404+
type Err = PatternError;
405+
406+
fn from_str(s: &str) -> Result<Pattern, PatternError> {
407+
Pattern::new(s)
408+
}
409+
}
410+
402411
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
403412
enum PatternToken {
404413
Char(char),
@@ -913,6 +922,12 @@ mod test {
913922
use std::path::Path;
914923
use super::{glob, Pattern, MatchOptions};
915924

925+
#[test]
926+
fn test_pattern_from_str() {
927+
assert!("a*b".parse::<Pattern>().unwrap().matches("a_b"));
928+
assert!("a/**b".parse::<Pattern>().unwrap_err().pos == 4);
929+
}
930+
916931
#[test]
917932
fn test_wildcard_errors() {
918933
assert!(Pattern::new("a/**b").unwrap_err().pos == 4);

0 commit comments

Comments
 (0)