Skip to content

Commit

Permalink
Use .strip_prefix in place of .starts_with
Browse files Browse the repository at this point in the history
The route! macro gets expanded into library consumer code, which is then
evaluated by Clippy, as such we need to fix these lints so downstream
users don't need to add exceptions.

Fix the same lint elsewhere in this codebase for consistency.
  • Loading branch information
bradfier committed Sep 15, 2021
1 parent b49b26c commit 0c8c1da
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/input/priority_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ impl<'a> Iterator for PriorityHeaderIter<'a> {

for p in params {
let trimmed_p = p.trim_start();
if trimmed_p.starts_with("q=") {
if let Ok(val) = FromStr::from_str(trimmed_p[2..].trim()) {
if let Some(stripped) = trimmed_p.strip_prefix("q=") {
if let Ok(val) = FromStr::from_str(stripped.trim()) {
value = val;
break;
}
Expand Down
24 changes: 10 additions & 14 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ macro_rules! router {
let url_params = (|| {
let mut url_params = RouilleUrlParams::new();
for (actual, desired) in request_url.iter().zip(url_pattern.iter()) {
if desired.starts_with("{") && desired.ends_with("}") {
let key = &desired[1..desired.len()-1];
if let Some(key) = desired.strip_prefix("{").and_then(|d| d.strip_suffix("}")) {
$crate::router!(__insert_param $request_url_str, url_params, key, actual ; $($param: $param_type)*)
} else if actual != desired {
return None
Expand Down Expand Up @@ -276,9 +275,7 @@ macro_rules! router {
};

(__check_pattern $url:ident $value:block /{$p:ident} $($rest:tt)*) => (
if !$url.starts_with('/') {
None
} else {
if let Some(url) = $url.strip_prefix('/') {
let url = &$url[1..];
let pat_end = url.find('/').unwrap_or(url.len());
let rest_url = &url[pat_end..];
Expand All @@ -288,13 +285,13 @@ macro_rules! router {
} else {
None
}
} else {
None
}
);

(__check_pattern $url:ident $value:block /{$p:ident: $t:ty} $($rest:tt)*) => (
if !$url.starts_with('/') {
None
} else {
if let Some(url) = $url.strip_prefix('/') {
let url = &$url[1..];
let pat_end = url.find('/').unwrap_or(url.len());
let rest_url = &url[pat_end..];
Expand All @@ -306,14 +303,15 @@ macro_rules! router {
} else {
None
}
} else {
None
}
);

(__check_pattern $url:ident $value:block /$p:ident $($rest:tt)*) => (
{
let required = concat!("/", stringify!($p));
if $url.starts_with(required) {
let rest_url = &$url[required.len()..];
if let Some(rest_url) = $url.strip_prefix(required) {
$crate::router!(__check_pattern rest_url $value $($rest)*)
} else {
None
Expand All @@ -323,8 +321,7 @@ macro_rules! router {

(__check_pattern $url:ident $value:block - $($rest:tt)*) => (
{
if $url.starts_with('-') {
let rest_url = &$url[1..];
if let Some(rest_url) = $url.strip_prefix('-') {
$crate::router!(__check_pattern rest_url $value $($rest)*)
} else {
None
Expand All @@ -343,8 +340,7 @@ macro_rules! router {
(__check_pattern $url:ident $value:block $p:ident $($rest:tt)*) => (
{
let required = stringify!($p);
if $url.starts_with(required) {
let rest_url = &$url[required.len()..];
if let Some(rest_url) = $url.strip_prefix(required) {
$crate::router!(__check_pattern rest_url $value $($rest)*)
} else {
None
Expand Down

0 comments on commit 0c8c1da

Please sign in to comment.