make access description optional
This commit is contained in:
parent
25dbb0ebab
commit
e78e31ab4c
@ -42,10 +42,16 @@ pub fn handle_method(mut attribs: JSONObject, mut func: syn::ItemFn) -> Result<T
|
||||
let access_setter = match attribs.remove("access") {
|
||||
Some(access) => {
|
||||
let access = Access::try_from(access.into_object("access rules")?)?;
|
||||
let description: syn::LitStr = access.description.try_into()?;
|
||||
let description: Option<syn::LitStr> = access.description.try_into()?;
|
||||
let permission: syn::Expr = access.permission.try_into()?;
|
||||
quote_spanned! { access.span =>
|
||||
.access(#description, #permission)
|
||||
if let Some(description) = description {
|
||||
quote_spanned! { access.span =>
|
||||
.access(Some(#description), #permission)
|
||||
}
|
||||
} else {
|
||||
quote_spanned! { access.span =>
|
||||
.access(None, #permission)
|
||||
}
|
||||
}
|
||||
}
|
||||
None => TokenStream::new(),
|
||||
@ -533,7 +539,7 @@ impl<'a> DefaultParameters<'a> {
|
||||
|
||||
struct Access {
|
||||
span: Span,
|
||||
description: syn::LitStr,
|
||||
description: Option<syn::LitStr>,
|
||||
permission: syn::Expr,
|
||||
}
|
||||
|
||||
@ -549,10 +555,10 @@ impl TryFrom<JSONObject> for Access {
|
||||
type Error = syn::Error;
|
||||
|
||||
fn try_from(mut obj: JSONObject) -> Result<Self, syn::Error> {
|
||||
let description = obj
|
||||
.remove("description")
|
||||
.ok_or_else(|| format_err!(obj.span(), "missing description"))?
|
||||
.try_into()?;
|
||||
let description = match obj.remove("description") {
|
||||
Some(v) => Some(v.try_into()?),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let permission = obj
|
||||
.remove("permission")
|
||||
|
@ -195,10 +195,7 @@ macro_rules! list_subdirs_api_method {
|
||||
}),
|
||||
&$crate::api::schema::ObjectSchema::new("Directory index.", &[])
|
||||
.additional_properties(true)
|
||||
).access(
|
||||
"Any authenticated user may access the directory index.",
|
||||
&$crate::api::Permission::Anybody
|
||||
)
|
||||
).access(None, &$crate::api::Permission::Anybody)
|
||||
}
|
||||
}
|
||||
|
||||
@ -391,7 +388,7 @@ const DUMMY_HANDLER: ApiHandler = ApiHandler::Sync(&dummy_handler_fn);
|
||||
/// Access permission with description
|
||||
#[cfg_attr(feature = "test-harness", derive(Eq, PartialEq))]
|
||||
pub struct ApiAccess {
|
||||
pub description: &'static str,
|
||||
pub description: Option<&'static str>,
|
||||
pub permission: &'static Permission,
|
||||
}
|
||||
|
||||
@ -434,7 +431,7 @@ impl ApiMethod {
|
||||
protected: false,
|
||||
reload_timezone: false,
|
||||
access: ApiAccess {
|
||||
description: "Default access permissions (superuser only).",
|
||||
description: None,
|
||||
permission: &Permission::Superuser,
|
||||
},
|
||||
}
|
||||
@ -448,7 +445,7 @@ impl ApiMethod {
|
||||
protected: false,
|
||||
reload_timezone: false,
|
||||
access: ApiAccess {
|
||||
description: "Default access permissions (superuser only).",
|
||||
description: None,
|
||||
permission: &Permission::Superuser,
|
||||
},
|
||||
}
|
||||
@ -474,7 +471,7 @@ impl ApiMethod {
|
||||
|
||||
pub const fn access(
|
||||
mut self,
|
||||
description: &'static str,
|
||||
description: Option<&'static str>,
|
||||
permission: &'static Permission,
|
||||
) -> Self {
|
||||
self.access = ApiAccess {
|
||||
|
Loading…
x
Reference in New Issue
Block a user