derive Updateable (#167)

This commit is contained in:
SpecificProtagonist 2023-07-22 11:20:31 +02:00 committed by GitHub
parent ccf44a5375
commit 9ebe72a7dc
6 changed files with 87 additions and 7 deletions

9
Cargo.lock generated
View File

@ -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"

View File

@ -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"]}

46
chorus-macros/Cargo.lock generated Normal file
View File

@ -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"

11
chorus-macros/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "chorus-macros"
version = "0.1.0"
edition = "2021"
[lib]
proc-macro = true
[dependencies]
quote = "1"
syn = "2"

18
chorus-macros/src/lib.rs Normal file
View File

@ -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()
}

View File

@ -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<Snowflake>,
@ -66,12 +67,6 @@ pub struct Channel {
pub video_quality_mode: Option<i32>,
}
impl Updateable for Channel {
fn id(&self) -> Snowflake {
self.id
}
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub struct Tag {
pub id: Snowflake,