api: fully qualify types in exported macros

and move const_regex macro into a separate module

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-11-21 14:00:25 +01:00
parent c38d18484d
commit a6ce1e432b
5 changed files with 46 additions and 40 deletions

View File

@ -0,0 +1,31 @@
/// Macro to generate a ConstRegexPattern
#[macro_export]
macro_rules! const_regex {
() => {};
($(#[$attr:meta])* pub ($($vis:tt)+) $name:ident = $regex:expr; $($rest:tt)*) => {
$crate::const_regex! { (pub ($($vis)+)) $(#[$attr])* $name = $regex; $($rest)* }
};
($(#[$attr:meta])* pub $name:ident = $regex:expr; $($rest:tt)*) => {
$crate::const_regex! { (pub) $(#[$attr])* $name = $regex; $($rest)* }
};
($(#[$attr:meta])* $name:ident = $regex:expr; $($rest:tt)*) => {
$crate::const_regex! { () $(#[$attr])* $name = $regex; $($rest)* }
};
(
($($pub:tt)*) $(#[$attr:meta])* $name:ident = $regex:expr;
$($rest:tt)*
) => {
$(#[$attr])* $($pub)* const $name: $crate::schema::ConstRegexPattern =
$crate::schema::ConstRegexPattern {
regex_string: $regex,
regex_obj: (|| -> &'static ::regex::Regex {
::lazy_static::lazy_static! {
static ref SCHEMA: ::regex::Regex = ::regex::Regex::new($regex).unwrap();
}
&SCHEMA
})
};
$crate::const_regex! { $($rest)* }
};
}

View File

@ -1,7 +1,9 @@
use std::fmt;
use failure::Fail;
use hyper::StatusCode;
#[doc(hidden)]
pub use hyper::StatusCode;
#[derive(Debug, Fail)]
pub struct HttpError {
@ -24,6 +26,9 @@ impl fmt::Display for HttpError {
#[macro_export]
macro_rules! http_err {
($status:ident, $msg:expr) => {{
Error::from(HttpError::new(StatusCode::$status, $msg))
::failure::Error::from($crate::error::HttpError::new(
$crate::error::StatusCode::$status,
$msg,
))
}};
}

View File

@ -8,6 +8,7 @@ use hyper::http::request::Parts;
use hyper::{Body, Response};
use serde_json::Value;
pub mod const_regex;
pub mod error;
pub mod router;
pub mod rpc_environment;

View File

@ -75,15 +75,15 @@ pub enum SubRoute {
#[macro_export]
macro_rules! list_subdirs_api_method {
($map:expr) => {
ApiMethod::new(
&ApiHandler::Sync( & |_, _, _| {
let index = serde_json::json!(
$map.iter().map(|s| serde_json::json!({ "subdir": s.0}))
.collect::<Vec<serde_json::Value>>()
$crate::ApiMethod::new(
&$crate::ApiHandler::Sync( & |_, _, _| {
let index = ::serde_json::json!(
$map.iter().map(|s| ::serde_json::json!({ "subdir": s.0}))
.collect::<Vec<::serde_json::Value>>()
);
Ok(index)
}),
&crate::api_schema::ObjectSchema::new("Directory index.", &[]).additional_properties(true)
&$crate::schema::ObjectSchema::new("Directory index.", &[]).additional_properties(true)
)
}
}

View File

@ -156,37 +156,6 @@ impl std::fmt::Debug for ConstRegexPattern {
}
}
/// Macro to generate a ConstRegexPattern
#[macro_export]
macro_rules! const_regex {
() => {};
($(#[$attr:meta])* pub ($($vis:tt)+) $name:ident = $regex:expr; $($rest:tt)*) => {
const_regex! { (pub ($($vis)+)) $(#[$attr])* $name = $regex; $($rest)* }
};
($(#[$attr:meta])* pub $name:ident = $regex:expr; $($rest:tt)*) => {
const_regex! { (pub) $(#[$attr])* $name = $regex; $($rest)* }
};
($(#[$attr:meta])* $name:ident = $regex:expr; $($rest:tt)*) => {
const_regex! { () $(#[$attr])* $name = $regex; $($rest)* }
};
(
($($pub:tt)*) $(#[$attr:meta])* $name:ident = $regex:expr;
$($rest:tt)*
) => {
$(#[$attr])* $($pub)* const $name: ConstRegexPattern = ConstRegexPattern {
regex_string: $regex,
regex_obj: (|| -> &'static regex::Regex {
lazy_static::lazy_static! {
static ref SCHEMA: regex::Regex = regex::Regex::new($regex).unwrap();
}
&SCHEMA
})
};
const_regex! { $($rest)* }
};
}
#[derive(Debug)]
pub struct StringSchema {
pub description: &'static str,
@ -740,7 +709,7 @@ fn test_query_string() {
}
// TEST regex pattern
const_regex! {
crate::const_regex! {
TEST_REGEX = "test";
TEST2_REGEX = "^test$";
}