#[allow(unused_imports)]
use crate::prelude::*;
use beet_core::prelude::*;
use syn::Ident;
use syn::ItemFn;
#[derive(Debug, Clone, PartialEq, Eq, Component)]
pub struct RouteFileMethod {
pub path: RoutePath,
pub method: HttpMethod,
pub item: Unspan<ItemFn>,
}
impl AsRef<RouteFileMethod> for RouteFileMethod {
fn as_ref(&self) -> &RouteFileMethod { self }
}
impl RouteFileMethod {
pub fn new(path: impl Into<RoutePath>, method: HttpMethod) -> Self {
let path = path.into();
let method_name = method.to_string_lowercase();
let method_ident = quote::format_ident!("{method_name}");
Self {
path,
method,
item: Unspan::new(&syn::parse_quote!(
fn #method_ident() {}
)),
}
}
pub fn new_with(
path: impl Into<RoutePath>,
method: HttpMethod,
item: &ItemFn,
) -> Self {
Self {
path: path.into(),
method,
item: Unspan::new(item),
}
}
pub fn from_path(
local_path: impl AsRef<std::path::Path>,
method: HttpMethod,
) -> Self {
let route = RoutePath::from_file_path(local_path).unwrap();
Self::new(route, method)
}
pub fn returns_result(&self) -> bool {
if let syn::ReturnType::Type(_, ty) = &self.item.sig.output {
if let syn::Type::Path(syn::TypePath { path, .. }) = &**ty {
if let Some(seg) = path.segments.last() {
return seg.ident == "Result";
}
}
}
false
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub enum RouteFileMethodMeta {
Method,
File,
#[default]
Collection,
}
impl RouteFileMethodMeta {
pub fn ident(&self, mod_ident: &Ident, method_name: &str) -> syn::Path {
match self {
RouteFileMethodMeta::Method => {
let meta_ident = quote::format_ident!("meta_{}", method_name);
syn::parse_quote!(#mod_ident::#meta_ident)
}
RouteFileMethodMeta::File => syn::parse_quote!(#mod_ident::meta),
RouteFileMethodMeta::Collection => syn::parse_quote!(Self::meta),
}
}
}
#[cfg(test)]
mod test {
use crate::prelude::*;
use beet_core::prelude::*;
#[test]
fn returns_result() {
RouteFileMethod::new_with(
"",
HttpMethod::Get,
&syn::parse_quote!(
fn get() {}
),
)
.returns_result()
.xpect_false();
RouteFileMethod::new_with(
"",
HttpMethod::Get,
&syn::parse_quote!(
fn get() -> Result<(), ()> {}
),
)
.returns_result()
.xpect_true();
}
}