diff options
author | Irene Knapp <ireneista@gmail.com> | 2020-12-29 01:25:30 -0800 |
---|---|---|
committer | Irene Knapp <ireneista@gmail.com> | 2020-12-29 01:25:30 -0800 |
commit | 3378c0c3704e1b85f23459fc1273186ec9ecbd8a (patch) | |
tree | f5a5f57c48ddfe43a1a3371e7e96dc7aa07af445 /src | |
parent | db31e770be89cc3e693ffef470878267efeff406 (diff) |
add a FromStr instance for GenericPathComponent, and a new type PathError
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/path.rs | 10 | ||||
-rw-r--r-- | src/path/error.rs | 16 | ||||
-rw-r--r-- | src/path/parser.lalrpop | 2 |
4 files changed, 28 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs index 3b7d26d..a8d8f50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use std::collections::HashSet; use std::io; use std::io::prelude::*; use std::os::unix::fs::PermissionsExt; +use std::str::FromStr; #[macro_use] extern crate lalrpop_util; diff --git a/src/path.rs b/src/path.rs index 2e599bf..c566b9c 100644 --- a/src/path.rs +++ b/src/path.rs @@ -175,6 +175,16 @@ impl std::str::FromStr for DirectoryName { } +impl std::str::FromStr for GenericPathComponent { + type Err = PathError; + + fn from_str(input: &str) -> std::result::Result<Self, Self::Err> { + parser::PathComponentParser::new().parse(input).map_err( + |e| PathError::Parse(e.to_string())) + } +} + + impl AbsoluteDirectoryPath { pub fn to_sys_path(&self) -> std::path::PathBuf { let mut result = std::path::PathBuf::new(); diff --git a/src/path/error.rs b/src/path/error.rs index 1162a3b..0ee5423 100644 --- a/src/path/error.rs +++ b/src/path/error.rs @@ -8,11 +8,18 @@ pub enum DirectoryNameError { ContainsSlash(String), } +#[derive(Clone,Debug,Eq,Hash,Ord,PartialEq,PartialOrd)] +pub enum PathError { + Parse(String), +} + impl std::error::Error for FileNameError { } impl std::error::Error for DirectoryNameError { } +impl std::error::Error for PathError { } + impl std::fmt::Display for FileNameError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -33,3 +40,12 @@ impl std::fmt::Display for DirectoryNameError { } } +impl std::fmt::Display for PathError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + PathError::Parse(s) => + f.write_fmt(format_args!("Syntax error in path: {}", s)), + } + } +} + diff --git a/src/path/parser.lalrpop b/src/path/parser.lalrpop index 9705fc3..c41b3fd 100644 --- a/src/path/parser.lalrpop +++ b/src/path/parser.lalrpop @@ -70,7 +70,7 @@ PathNoColons2: Vec<GenericPathComponent> = { } } -PathComponent: GenericPathComponent = { +pub PathComponent: GenericPathComponent = { DOT => GenericPathComponent::CurrentDirectory, DOT_DOT => GenericPathComponent::ParentDirectory, <PATH_COMPONENT_NO_COLONS> => |