dce-macro 1.8.0

The macros for the dce-router lib
Documentation
use std::ops::{Deref, DerefMut};

use dce_macro::api;
use dce_router::{api::{Handler, Methods, Suffix}, context::Request, protocol::{RoutableProtocol, RpMeta}};
use dce_util::result::{DceVoid, OK_VOID};


pub struct CliProtocol {
    meta: RpMeta<()>,
}

impl Deref for CliProtocol {
    type Target = RpMeta<()>;

    fn deref(&self) -> &Self::Target {
        &self.meta
    }
}

impl DerefMut for CliProtocol {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.meta
    }
}

impl RoutableProtocol for CliProtocol {
    type Req = ();

    fn path(&self) -> &str {
        todo!()
    }
}

#[api]
fn hello(_: Request<'_, CliProtocol>) -> DceVoid {
    OK_VOID
}

#[api(
    "ahello1",
    ext1 = "Abc",
    methods = Methods(255),
    suffixes = [".html"],
    omission = true,
    id = "abcd",
    responsive = false,
    redirect = "drunk",
    name = "AsyncHello",
    ext2 = "CBA",
)]
async fn ahello(_: Request<'_, CliProtocol>) -> DceVoid {
    OK_VOID
}

#[test]
fn test_api() {
    let api = hello_api();
    assert_eq!(api.path, "hello");
    assert_eq!((api.methods & Methods(255)).0, 0);
    assert_eq!(api.suffixes, vec![]);
    assert_eq!(api.omission, false);
    assert_eq!(api.id, None);
    assert_eq!(api.responsive, true);
    assert_eq!(api.redirect, None);
    assert_eq!(api.name, "hello");
    assert!(api.extras.is_empty());
    assert!(matches!(api.handler, Some(Handler::Sync(_))));

    let api = ahello_api();
    assert_eq!(api.path, "ahello1");
    assert_eq!((api.methods & Methods(128)).0, 128);
    assert_eq!(api.suffixes, vec![Suffix(".html")]);
    assert_eq!(api.omission, true);
    assert_eq!(api.id, Some("abcd"));
    assert_eq!(api.responsive, false);
    assert_eq!(api.redirect, Some("drunk"));
    assert_eq!(api.name, "AsyncHello");
    assert!(matches!(api.extra_by("ext1").and_then(|v| v.downcast_ref::<&str>()), Some(&"Abc")));
    assert!(matches!(api.extra_by("ext2").and_then(|v| v.downcast_ref::<&str>()), Some(&"CBA")));
    assert!(matches!(api.handler, Some(Handler::Async(_))));
}