2022-07-31 15:44:45 +02:00
|
|
|
use std::{path::PathBuf, str::FromStr};
|
|
|
|
|
2022-08-10 10:13:34 +02:00
|
|
|
use crate::Error;
|
2022-07-31 15:44:45 +02:00
|
|
|
|
2022-07-31 21:12:19 +02:00
|
|
|
/// A specialized `FromStr` trait that returns [`AtError`] errors
|
2022-07-31 15:44:45 +02:00
|
|
|
pub trait Parse: Sized {
|
2022-07-31 21:12:19 +02:00
|
|
|
/// Parse `Self` from `string`.
|
2022-08-10 10:13:34 +02:00
|
|
|
fn parse(string: &str) -> Result<Self, Error>;
|
2022-07-31 15:44:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for bool {
|
2022-08-10 10:13:34 +02:00
|
|
|
fn parse(string: &str) -> Result<Self, Error> {
|
|
|
|
Self::from_str(string).map_err(Error::from)
|
2022-07-31 15:44:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_parse_for_int_type {
|
|
|
|
($($int_type:ty),+ $(,)?) => {
|
|
|
|
$(
|
|
|
|
impl Parse for $int_type {
|
2022-08-10 10:13:34 +02:00
|
|
|
fn parse(string: &str) -> Result<Self, Error> {
|
|
|
|
Self::from_str(string).map_err(Error::from)
|
2022-07-31 15:44:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl_parse_for_int_type![i8, i16, i32, i64, i128, u8, u16, u32, u64, u128];
|
|
|
|
|
|
|
|
impl Parse for String {
|
2022-08-10 10:13:34 +02:00
|
|
|
fn parse(string: &str) -> Result<Self, Error> {
|
2022-07-31 15:44:45 +02:00
|
|
|
Ok(string.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for PathBuf {
|
2022-08-10 10:13:34 +02:00
|
|
|
fn parse(string: &str) -> Result<Self, Error> {
|
2022-07-31 15:44:45 +02:00
|
|
|
Ok(PathBuf::from(string))
|
|
|
|
}
|
|
|
|
}
|