diff options
| author | Fuwn <[email protected]> | 2023-04-27 05:52:31 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-04-27 05:52:31 +0000 |
| commit | 2afebbc1f8fa218bb3eae271391b707d14778fc7 (patch) | |
| tree | ce226b86a871110dc86484a2fd9a01db4d501eae /rossweisse/src/lib.rs | |
| parent | docs(readme): add information about examples (diff) | |
| download | windmark-2afebbc1f8fa218bb3eae271391b707d14778fc7.tar.xz windmark-2afebbc1f8fa218bb3eae271391b707d14778fc7.zip | |
feat(rossweisse): initial implementation of struct router framework
Diffstat (limited to 'rossweisse/src/lib.rs')
| -rw-r--r-- | rossweisse/src/lib.rs | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/rossweisse/src/lib.rs b/rossweisse/src/lib.rs new file mode 100644 index 0000000..315a0de --- /dev/null +++ b/rossweisse/src/lib.rs @@ -0,0 +1,91 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2023 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +#![deny( + clippy::all, + clippy::nursery, + clippy::pedantic, + future_incompatible, + nonstandard_style, + rust_2018_idioms, + unsafe_code, + unused, + warnings +)] +#![recursion_limit = "128"] + +mod implementations; + +use proc_macro::TokenStream; +use syn::Item; + +/// Marks a `struct` as a router or marks an `impl` block as a router +/// implementation +/// +/// # Examples +/// +/// ```rust +/// #[rossweisse::router] +/// struct Router { +/// _phantom: (), +/// } +/// +/// #[rossweisse::router] +/// impl Router { +/// #[route] +/// pub fn index( +/// _context: windmark::context::RouteContext, +/// ) -> windmark::Response { +/// windmark::Response::success("Hello, World!") +/// } +/// } +/// ``` +#[proc_macro_attribute] +pub fn router(arguments: TokenStream, item: TokenStream) -> TokenStream { + let output = match syn::parse::<Item>(item.clone()) { + Ok(Item::Struct(item)) => implementations::fields(arguments, item), + Ok(Item::Impl(item)) => implementations::methods(arguments, item), + _ => panic!("`#[rossweisse::router]` can only be used on `struct`s"), + }; + + output.into() +} + +/// Marks a method of a router implementation as a route to mount +/// +/// # Examples +/// +/// ```rust +/// #[rossweisse::router] +/// impl Router { +/// #[route] +/// pub fn index( +/// _context: windmark::context::RouteContext, +/// ) -> windmark::Response { +/// windmark::Response::success("Hello, World!") +/// } +/// } +/// ``` +#[proc_macro_attribute] +pub fn route(arguments: TokenStream, item: TokenStream) -> TokenStream { + let output = match syn::parse::<Item>(item.clone()) { + Ok(Item::Fn(item)) => implementations::route(arguments, item), + _ => panic!("`#[rossweisse::route]` can only be used on `fn`s"), + }; + + output.into() +} |