-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlib.rs
More file actions
30 lines (28 loc) · 704 Bytes
/
lib.rs
File metadata and controls
30 lines (28 loc) · 704 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
extern crate derive_as_path;
pub use derive_as_path::*;
use std::str::FromStr;
pub trait AsPath {
fn as_path(self) -> String;
}
impl<T: ToString> AsPath for T {
fn as_path(self) -> String {
format!("/{}", self.to_string())
}
}
pub trait ParsePath: AsPath + Sized {
fn parse_path(route: &str) -> Result<Self, ParseError>;
}
#[derive(Debug)]
pub enum ParseError {
FromStr,
NoMatch,
By(String, Box<ParseError>),
RemainingSegments,
}
impl<T: FromStr + ToString + AsPath> ParsePath for T {
fn parse_path(path: &str) -> Result<Self, ParseError> {
path.trim_start_matches("/")
.parse::<T>()
.map_err(|_| ParseError::FromStr)
}
}