Skip to content

Commit

Permalink
Fix MSRV
Browse files Browse the repository at this point in the history
  • Loading branch information
CathalMullan committed Aug 17, 2024
1 parent 73398c3 commit d58c525
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 43 deletions.
168 changes: 135 additions & 33 deletions src/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,138 @@ pub trait Constraint {
fn check(segment: &str) -> bool;
}

macro_rules! impl_constraint_fromstr {
($type:ty, $name:expr) => {
impl Constraint for $type {
const NAME: &'static str = $name;

fn check(segment: &str) -> bool {
<$type as std::str::FromStr>::from_str(segment).is_ok()
}
}
};
}

impl_constraint_fromstr!(u8, "u8");
impl_constraint_fromstr!(u16, "u16");
impl_constraint_fromstr!(u32, "u32");
impl_constraint_fromstr!(u64, "u64");
impl_constraint_fromstr!(u128, "u128");
impl_constraint_fromstr!(usize, "usize");

impl_constraint_fromstr!(i8, "i8");
impl_constraint_fromstr!(i16, "i16");
impl_constraint_fromstr!(i32, "i32");
impl_constraint_fromstr!(i64, "i64");
impl_constraint_fromstr!(i128, "i128");
impl_constraint_fromstr!(isize, "isize");

impl_constraint_fromstr!(f32, "f32");
impl_constraint_fromstr!(f64, "f64");

impl_constraint_fromstr!(bool, "bool");

impl_constraint_fromstr!(Ipv4Addr, "ipv4");
impl_constraint_fromstr!(Ipv6Addr, "ipv6");
impl Constraint for u8 {
const NAME: &'static str = "u8";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for u16 {
const NAME: &'static str = "u16";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for u32 {
const NAME: &'static str = "u32";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for u64 {
const NAME: &'static str = "u64";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for u128 {
const NAME: &'static str = "u128";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for usize {
const NAME: &'static str = "usize";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for i8 {
const NAME: &'static str = "i8";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for i16 {
const NAME: &'static str = "i16";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for i32 {
const NAME: &'static str = "i32";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for i64 {
const NAME: &'static str = "i64";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for i128 {
const NAME: &'static str = "i128";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for isize {
const NAME: &'static str = "isize";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for f32 {
const NAME: &'static str = "f32";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for f64 {
const NAME: &'static str = "f64";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for bool {
const NAME: &'static str = "bool";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for Ipv4Addr {
const NAME: &'static str = "ipv4";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}

impl Constraint for Ipv6Addr {
const NAME: &'static str = "ipv6";

fn check(segment: &str) -> bool {
segment.parse::<Self>().is_ok()
}
}
16 changes: 6 additions & 10 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,15 @@ impl<T> Router<T> {
router.constraint::<u64>().unwrap();
router.constraint::<u128>().unwrap();
router.constraint::<usize>().unwrap();

router.constraint::<i8>().unwrap();
router.constraint::<i16>().unwrap();
router.constraint::<i32>().unwrap();
router.constraint::<i64>().unwrap();
router.constraint::<i128>().unwrap();
router.constraint::<isize>().unwrap();

router.constraint::<f32>().unwrap();
router.constraint::<f64>().unwrap();

router.constraint::<bool>().unwrap();

router.constraint::<Ipv4Addr>().unwrap();
router.constraint::<Ipv6Addr>().unwrap();

Expand Down Expand Up @@ -144,15 +140,15 @@ impl<T> Router<T> {

pub fn search<'a>(&'a self, path: &str) -> Result<Option<Match<'a, T>>, SearchError> {
let path_bytes = if self.percent_encoding {
&percent_decode(path.as_bytes())?
percent_decode(path.as_bytes())?
} else {
path.as_bytes()
path.as_bytes().to_vec()
};

let mut parameters = vec![];
let Some(node) = self
.root
.search(path_bytes, &mut parameters, &self.constraints)
.search(&path_bytes, &mut parameters, &self.constraints)
else {
return Ok(None);
};
Expand All @@ -165,14 +161,14 @@ impl<T> Router<T> {
.into_iter()
.map(|raw| {
let value = if self.percent_encoding {
&percent_decode(raw.value)?
percent_decode(raw.value)?
} else {
raw.value
raw.value.to_vec()
};

Ok(Parameter {
key: std::str::from_utf8(raw.key)?.to_string(),
value: std::str::from_utf8(value)?.to_string(),
value: std::str::from_utf8(&value)?.to_string(),
})
})
.collect::<Result<Vec<_>, SearchError>>()?;
Expand Down

0 comments on commit d58c525

Please sign in to comment.