From b82b6566f1a0f5f0cbd9c0f5fca6b8702d85ebe8 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Sun, 21 May 2023 23:15:08 -0400 Subject: [PATCH] refactor(cosmic-config-derive): add support for types with generic parameters --- Cargo.toml | 1 + cosmic-config-derive/src/lib.rs | 20 ++++++++++++++++++-- cosmic-config/Cargo.toml | 2 +- cosmic-config/src/lib.rs | 6 +++--- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0879d0f9..acccb0ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ cosmic-panel-config = {git = "https://github.com/pop-os/cosmic-panel", optional sctk = { package = "smithay-client-toolkit", git = "https://github.com/Smithay/client-toolkit", optional = true, rev = "389a4f2" } slotmap = "1.0.6" fraction = "0.13.0" +cosmic-config = { path = "cosmic-config" } [target.'cfg(unix)'.dependencies] freedesktop-icons = "0.2.2" diff --git a/cosmic-config-derive/src/lib.rs b/cosmic-config-derive/src/lib.rs index 5f9c25e0..36370d75 100644 --- a/cosmic-config-derive/src/lib.rs +++ b/cosmic-config-derive/src/lib.rs @@ -1,6 +1,6 @@ use proc_macro::TokenStream; use quote::quote; -use syn; +use syn::{self, parse_quote}; #[proc_macro_derive(CosmicConfigEntry)] pub fn cosmic_config_entry_derive(input: TokenStream) -> TokenStream { @@ -14,6 +14,7 @@ pub fn cosmic_config_entry_derive(input: TokenStream) -> TokenStream { fn impl_cosmic_config_entry_macro(ast: &syn::DeriveInput) -> TokenStream { let name = &ast.ident; + let generics = &ast.generics; // Get the fields of the struct let fields = match ast.data { @@ -42,8 +43,23 @@ fn impl_cosmic_config_entry_macro(ast: &syn::DeriveInput) -> TokenStream { } }); + // Get the existing where clause or create a new one if it doesn't exist + let mut where_clause = ast + .generics + .where_clause + .clone() + .unwrap_or_else(|| parse_quote!(where)); + + // Add your additional constraints to the where clause + // Here, we add the constraint 'T: Debug' to all generic parameters + for param in ast.generics.params.iter() { + where_clause + .predicates + .push(parse_quote!(#param: ::std::default::Default + ::serde::Serialize + ::serde::de::DeserializeOwned)); + } + let gen = quote! { - impl CosmicConfigEntry for #name { + impl #generics CosmicConfigEntry for #name #generics #where_clause { fn write_entry(&self, config: &Config) -> Result<(), cosmic_config::Error> { #(#write_each_config_field)* Ok(()) diff --git a/cosmic-config/Cargo.toml b/cosmic-config/Cargo.toml index 5bf94df1..7393fafc 100644 --- a/cosmic-config/Cargo.toml +++ b/cosmic-config/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" [features] default = ["macro", "subscription"] macro = ["cosmic-config-derive"] -subscription = ["iced", "iced_futures"] +subscription = ["iced_futures"] [dependencies] atomicwrites = "0.4.0" diff --git a/cosmic-config/src/lib.rs b/cosmic-config/src/lib.rs index 57dfef95..cfcea1e8 100644 --- a/cosmic-config/src/lib.rs +++ b/cosmic-config/src/lib.rs @@ -1,7 +1,7 @@ #[cfg(feature = "subscription")] -use iced::subscription; -#[cfg(feature = "subscription")] use iced_futures::futures::channel::mpsc; +#[cfg(feature = "subscription")] +use iced_futures::subscription; use notify::{RecommendedWatcher, Watcher}; use serde::{de::DeserializeOwned, Serialize}; use std::{ @@ -277,7 +277,7 @@ pub enum ConfigUpdate { pub trait CosmicConfigEntry where - Self: Sized + Default, + Self: Sized, { fn write_entry(&self, config: &Config) -> Result<(), crate::Error>; fn get_entry(config: &Config) -> Result, Self)>;