From 24450571defcf9b207c733e28214dc164b0480d7 Mon Sep 17 00:00:00 2001 From: SpecificProtagonist Date: Sat, 22 Jul 2023 11:20:31 +0200 Subject: [PATCH] derive Updateable (#167) --- Cargo.lock | 9 +++++++ Cargo.toml | 1 + chorus-macros/Cargo.lock | 46 +++++++++++++++++++++++++++++++++++ chorus-macros/Cargo.toml | 11 +++++++++ chorus-macros/src/lib.rs | 18 ++++++++++++++ src/types/entities/channel.rs | 9 ++----- 6 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 chorus-macros/Cargo.lock create mode 100644 chorus-macros/Cargo.toml create mode 100644 chorus-macros/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index c90b667..d193cf4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -189,6 +189,7 @@ dependencies = [ "async-trait", "base64 0.21.2", "bitflags 2.3.3", + "chorus-macros", "chrono", "custom_error", "futures-util", @@ -215,6 +216,14 @@ dependencies = [ "url", ] +[[package]] +name = "chorus-macros" +version = "0.1.0" +dependencies = [ + "quote", + "syn 2.0.26", +] + [[package]] name = "chrono" version = "0.4.26" diff --git a/Cargo.toml b/Cargo.toml index 4126146..8dbb172 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ thiserror = "1.0.43" jsonwebtoken = "8.3.0" log = "0.4.19" async-trait = "0.1.71" +chorus-macros = {path = "chorus-macros"} [dev-dependencies] tokio = {version = "1.29.1", features = ["full"]} diff --git a/chorus-macros/Cargo.lock b/chorus-macros/Cargo.lock new file mode 100644 index 0000000..4aa96d3 --- /dev/null +++ b/chorus-macros/Cargo.lock @@ -0,0 +1,46 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "chorus-macros" +version = "0.1.0" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fe8a65d69dd0808184ebb5f836ab526bb259db23c657efa38711b1072ee47f0" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b60f673f44a8255b9c8c657daf66a596d435f2da81a555b06dc644d080ba45e0" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" diff --git a/chorus-macros/Cargo.toml b/chorus-macros/Cargo.toml new file mode 100644 index 0000000..cffffe1 --- /dev/null +++ b/chorus-macros/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "chorus-macros" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] +quote = "1" +syn = "2" diff --git a/chorus-macros/src/lib.rs b/chorus-macros/src/lib.rs new file mode 100644 index 0000000..c8d5e87 --- /dev/null +++ b/chorus-macros/src/lib.rs @@ -0,0 +1,18 @@ +use proc_macro::TokenStream; +use quote::quote; + +#[proc_macro_derive(Updateable)] +pub fn updateable_macro_derive(input: TokenStream) -> TokenStream { + let ast: syn::DeriveInput = syn::parse(input).unwrap(); + + let name = &ast.ident; + // No need for macro hygiene, we're only using this in chorus + quote! { + impl Updateable for #name { + fn id(&self) -> Snowflake { + self.id + } + } + } + .into() +} diff --git a/src/types/entities/channel.rs b/src/types/entities/channel.rs index aac57b6..154b83c 100644 --- a/src/types/entities/channel.rs +++ b/src/types/entities/channel.rs @@ -1,3 +1,4 @@ +use chorus_macros::Updateable; use chrono::Utc; use serde::{Deserialize, Serialize}; use serde_aux::prelude::deserialize_string_from_number; @@ -9,7 +10,7 @@ use crate::types::{ utils::Snowflake, }; -#[derive(Default, Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] +#[derive(Default, Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Updateable)] #[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))] pub struct Channel { pub application_id: Option, @@ -66,12 +67,6 @@ pub struct Channel { pub video_quality_mode: Option, } -impl Updateable for Channel { - fn id(&self) -> Snowflake { - self.id - } -} - #[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)] pub struct Tag { pub id: Snowflake,