diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2023-03-27 22:14:15 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2023-03-27 22:14:15 +0200 |
commit | fe9db878e8f7d8f4bff2a51bb49b66ff84013644 (patch) | |
tree | 60d040e1f61d5c00e86389578b15a536a212aa43 | |
parent | 93ee81f05e1cb90c79949d5c57b1d7e3c272dd02 (diff) | |
download | rust-zotapi-fe9db878e8f7d8f4bff2a51bb49b66ff84013644.tar.gz rust-zotapi-fe9db878e8f7d8f4bff2a51bb49b66ff84013644.tar.bz2 rust-zotapi-fe9db878e8f7d8f4bff2a51bb49b66ff84013644.zip |
Add ZotAPI trait and derive macro.
The idea is to try to generate more of the boilerplate code, but for now
we only do the `z()` method definition. There are also some we're not
quite able to replace yet (like XChanRequest) since it also has life
times.
It's a start anyways :)
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/abconfig.rs | 11 | ||||
-rw-r--r-- | src/abook.rs | 11 | ||||
-rw-r--r-- | src/bin/zot/main.rs | 1 | ||||
-rw-r--r-- | src/bin/zot/zot/abconfig.rs | 2 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/verify.rs | 11 | ||||
-rw-r--r-- | src/zotapi.rs | 5 | ||||
-rw-r--r-- | zotapi-derive/Cargo.toml | 13 | ||||
-rw-r--r-- | zotapi-derive/src/lib.rs | 20 |
10 files changed, 52 insertions, 25 deletions
@@ -29,6 +29,7 @@ serde_json = "1.0" serde_qs = "0.8" tokio = { version = "1.8", features = ["full"] } url = "2.2" +zotapi-derive = { version = "0.1.0", path = "zotapi-derive" } [dev-dependencies] base64 = "0.13" diff --git a/src/abconfig.rs b/src/abconfig.rs index ebb7e2b..8904ca3 100644 --- a/src/abconfig.rs +++ b/src/abconfig.rs @@ -14,13 +14,14 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. -use crate::{client::Client, error::Error}; +use crate::{client::Client, error::Error, ZotAPI}; use serde::Deserialize; +use zotapi_derive::ZotAPI; /// A struct for storing a key value pair with a category in /// relation to a contact. Typically used to store permissions /// a given contact has on a given channel. -#[derive(Debug, Default, Deserialize)] +#[derive(Debug, Default, Deserialize, ZotAPI)] pub struct ABConfig { /// Database ID for this entry pub id: u32, @@ -41,12 +42,6 @@ pub struct ABConfig { pub v: String, } -impl ABConfig { - pub fn z() -> ABConfigRequest { - ABConfigRequest::default() - } -} - #[derive(Default)] pub struct ABConfigRequest { abook_id: Option<u32>, diff --git a/src/abook.rs b/src/abook.rs index d7d38cd..c7ceeb1 100644 --- a/src/abook.rs +++ b/src/abook.rs @@ -14,11 +14,12 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. -use crate::{client::Client, error::Error, XChan}; +use crate::{client::Client, error::Error, XChan, ZotAPI}; use serde::Deserialize; use std::convert::TryFrom; +use zotapi_derive::ZotAPI; -#[derive(Debug, Default, Deserialize)] +#[derive(Debug, Default, Deserialize, ZotAPI)] pub struct Abook { #[serde(rename = "abook_id")] pub id: u32, @@ -105,12 +106,6 @@ pub struct Abook { } -impl Abook { - pub fn z() -> AbookRequest { - AbookRequest::default() - } -} - impl<'a> TryFrom<&'a str> for Abook { type Error = Error; diff --git a/src/bin/zot/main.rs b/src/bin/zot/main.rs index 70a7984..bf7b9b8 100644 --- a/src/bin/zot/main.rs +++ b/src/bin/zot/main.rs @@ -19,6 +19,7 @@ use clap::{clap_app, crate_authors, crate_version}; use dotenv::dotenv; use std::env; use std::str::FromStr; +use zotapi::ZotAPI; mod zot; diff --git a/src/bin/zot/zot/abconfig.rs b/src/bin/zot/zot/abconfig.rs index 14875c8..1d67a54 100644 --- a/src/bin/zot/zot/abconfig.rs +++ b/src/bin/zot/zot/abconfig.rs @@ -15,7 +15,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -use zotapi; +use zotapi::{self, ZotAPI}; pub async fn fetch(client: &zotapi::Client) { match zotapi::ABConfig::z().fetch(&client).await { @@ -24,6 +24,7 @@ mod item; mod network_stream; mod verify; mod xchan; +mod zotapi; pub use abconfig::ABConfig; pub use abook::Abook; @@ -35,6 +36,7 @@ pub use item::item; pub use network_stream::network_stream; pub use verify::Channel; pub use xchan::XChan; +pub use zotapi::ZotAPI; #[cfg(test)] mod tests { diff --git a/src/verify.rs b/src/verify.rs index 8edb2f1..57e250d 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -14,11 +14,12 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. -use crate::{client::Client, error::Error, XChan}; +use crate::{client::Client, error::Error, XChan, ZotAPI}; use serde::Deserialize; use std::convert::TryFrom; +use zotapi_derive::ZotAPI; -#[derive(Deserialize, Debug)] +#[derive(Deserialize, Debug, ZotAPI)] pub struct Channel { #[serde(alias = "channel_id")] pub id: u32, @@ -129,12 +130,6 @@ pub struct Channel { pub xchan: XChan, } -impl Channel { - pub fn z() -> ChannelRequest { - ChannelRequest::default() - } -} - impl<'a> TryFrom<&'a str> for Channel { type Error = Error; diff --git a/src/zotapi.rs b/src/zotapi.rs new file mode 100644 index 0000000..42d8cde --- /dev/null +++ b/src/zotapi.rs @@ -0,0 +1,5 @@ +pub trait ZotAPI<T: Default> { + fn z() -> T { + T::default() + } +} diff --git a/zotapi-derive/Cargo.toml b/zotapi-derive/Cargo.toml new file mode 100644 index 0000000..72ece81 --- /dev/null +++ b/zotapi-derive/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "zotapi-derive" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +proc-macro2 = "1.0" +quote = "1.0" +syn = { version = "1.0", features = ["full"] } +[lib] +proc-macro = true diff --git a/zotapi-derive/src/lib.rs b/zotapi-derive/src/lib.rs new file mode 100644 index 0000000..ff44c9c --- /dev/null +++ b/zotapi-derive/src/lib.rs @@ -0,0 +1,20 @@ +use proc_macro::{self, TokenStream}; +use quote::*; +use syn::DeriveInput; + +#[proc_macro_derive(ZotAPI)] +pub fn zotapi_derive(input: TokenStream) -> TokenStream { + let ast = syn::parse(input).unwrap(); + impl_zotapi_derive(&ast) +} + +fn impl_zotapi_derive(ast: &DeriveInput) -> TokenStream { + let name = &ast.ident; // The identifier of the struct we're called for + let out_name = format_ident!("{}Request", name); + + let gen = quote! { + impl ZotAPI<#out_name> for #name { + } + }; + gen.into() +} |