replace use of str::split_once, str::rsplit_once, and concat! inside doc comments, to enable 1.51 compat

This commit is contained in:
David McGillicuddy 2022-01-06 19:11:40 +00:00
parent c3ce33df05
commit 7f1f131d44
5 changed files with 59 additions and 12 deletions

View File

@ -99,8 +99,7 @@ impl<T: str::FromStr> str::FromStr for QualityItem<T> {
let mut raw_item = q_item_str;
let mut quality = Quality::MAX;
let parts = q_item_str
.rsplit_once(';')
let parts = str_rsplit_once(q_item_str, ';')
.map(|(item, q_attr)| (item.trim(), q_attr.trim()));
if let Some((val, q_attr)) = parts {
@ -141,6 +140,14 @@ impl<T: str::FromStr> str::FromStr for QualityItem<T> {
}
}
// `str::rsplit_once` is stabilized in 1.52.0
fn str_rsplit_once(s: &str, delimiter: char) -> Option<(&str, &str)> {
let mut rsplit = s.rsplitn(2, delimiter);
let suffix = rsplit.next()?;
let prefix = rsplit.next()?;
Some((prefix, suffix))
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -117,8 +117,17 @@ where
}
macro_rules! error_helper {
// Workaround for 1.52.0 compat. It's not great but any use of `concat!` must be done prior
// to insertion in a doc comment.
($name:ident, $status:ident) => {
#[doc = concat!("Helper function that wraps any error and generates a `", stringify!($status), "` response.")]
error_helper!(
$name,
$status,
concat!("Helper function that wraps any error and generates a `", stringify!($status), "` response.")
);
};
($name:ident, $status:ident, $doc:expr) => {
#[doc = $doc]
#[allow(non_snake_case)]
pub fn $name<T>(err: T) -> Error
where

View File

@ -289,16 +289,27 @@ impl Guard for MethodGuard {
}
macro_rules! method_guard {
// Workaround for 1.52.0 compat. It's not great but any use of `concat!` must be done prior
// to insertion in a doc comment.
($method_fn:ident, $method_const:ident) => {
#[doc = concat!("Creates a guard that matches the `", stringify!($method_const), "` request method.")]
method_guard!(
$method_fn,
$method_const,
concat!("Creates a guard that matches the `", stringify!($method_const), "` request method."),
concat!("The route in this example will only respond to `", stringify!($method_const), "` requests."),
concat!(" .guard(guard::", stringify!($method_fn), "())")
);
};
($method_fn:ident, $method_const:ident, $doc1:expr, $doc2:expr, $doc3:expr) => {
#[doc = $doc1]
///
/// # Examples
#[doc = concat!("The route in this example will only respond to `", stringify!($method_const), "` requests.")]
#[doc = $doc2]
/// ```
/// use actix_web::{guard, web, HttpResponse};
///
/// web::route()
#[doc = concat!(" .guard(guard::", stringify!($method_fn), "())")]
#[doc = $doc3]
/// .to(|| HttpResponse::Ok());
/// ```
#[allow(non_snake_case)]

View File

@ -217,7 +217,7 @@ impl FromStr for Range {
type Err = ParseError;
fn from_str(s: &str) -> Result<Range, ParseError> {
let (unit, val) = s.split_once('=').ok_or(ParseError::Header)?;
let (unit, val) = str_split_once(s, '=').ok_or(ParseError::Header)?;
match (unit, val) {
("bytes", ranges) => {
@ -242,7 +242,7 @@ impl FromStr for ByteRangeSpec {
type Err = ParseError;
fn from_str(s: &str) -> Result<ByteRangeSpec, ParseError> {
let (start, end) = s.split_once('-').ok_or(ParseError::Header)?;
let (start, end) = str_split_once(s, '-').ok_or(ParseError::Header)?;
match (start, end) {
("", end) => end
@ -295,6 +295,14 @@ fn from_comma_delimited<T: FromStr>(s: &str) -> Vec<T> {
.collect()
}
// `str::split_once` is stabilized in 1.52.0
fn str_split_once(str: &str, delimiter: char) -> Option<(&str, &str)> {
let mut splitn = str.splitn(2, delimiter);
let prefix = splitn.next()?;
let suffix = splitn.next()?;
Some((prefix, suffix))
}
#[cfg(test)]
mod tests {
use actix_http::{test::TestRequest, Request};

View File

@ -83,25 +83,37 @@ pub fn route() -> Route {
Route::new()
}
macro_rules! method_route {
// Workaround for 1.52.0 compat. It's not great but any use of `concat!` must be done prior
// to insertion in a doc comment.
($method_fn:ident, $method_const:ident) => {
#[doc = concat!(" Creates a new route with `", stringify!($method_const), "` method guard.")]
method_route!(
$method_fn,
$method_const,
concat!(" Creates a new route with `", stringify!($method_const), "` method guard."),
concat!(" In this example, one `", stringify!($method_const), " /{project_id}` route is set up:"),
concat!(" .route(web::", stringify!($method_fn), "().to(|| HttpResponse::Ok()))")
);
};
($method_fn:ident, $method_const:ident, $doc1:expr, $doc2:expr, $doc3:expr) => {
#[doc = $doc1]
///
/// # Examples
#[doc = concat!(" In this example, one `", stringify!($method_const), " /{project_id}` route is set up:")]
#[doc = $doc2]
/// ```
/// use actix_web::{web, App, HttpResponse};
///
/// let app = App::new().service(
/// web::resource("/{project_id}")
#[doc = concat!(" .route(web::", stringify!($method_fn), "().to(|| HttpResponse::Ok()))")]
#[doc = $doc3]
///
/// );
/// ```
pub fn $method_fn() -> Route {
method(Method::$method_const)
}
};
}
}
method_route!(get, GET);