Skip to content

Commit 8cf1eff

Browse files
committed
new: Support generics.
1 parent 6ed5739 commit 8cf1eff

4 files changed

Lines changed: 34 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
#### 🚀 Updates
6+
7+
- Added simple generics support to `#[derive(Schematic)]`.
8+
39
## 0.19.4
410

511
#### 🐞 Fixes

crates/macros/src/common/macros.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use darling::ast::NestedMeta;
44
use darling::{FromDeriveInput, FromMeta};
55
use proc_macro2::{Ident, TokenStream};
66
use quote::{ToTokens, quote};
7-
use syn::{Attribute, Data, DeriveInput, ExprPath, Fields};
7+
use syn::{Attribute, Data, DeriveInput, ExprPath, Fields, Generics};
88

99
// #[serde()]
1010
#[derive(FromDeriveInput, Default)]
@@ -76,6 +76,7 @@ pub struct Macro<'l> {
7676
pub casing_format: String,
7777
pub name: &'l Ident,
7878
pub type_of: Container<'l>,
79+
pub generics: &'l Generics,
7980
}
8081

8182
impl<'l> Macro<'l> {
@@ -193,6 +194,7 @@ impl<'l> Macro<'l> {
193194
name: &input.ident,
194195
type_of: config_type,
195196
casing_format,
197+
generics: &input.generics,
196198
}
197199
}
198200

crates/macros/src/schematic/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ impl ToTokens for SchematicMacro<'_> {
1313
let schema_name = cfg.get_name();
1414
let schema_impl = cfg.type_of.generate_schema(&cfg.attrs);
1515
let instrument = instrument_quote();
16+
let (impl_generics, ty_generics, where_clause) = cfg.generics.split_for_impl();
1617

1718
tokens.extend(quote! {
1819
#[automatically_derived]
19-
impl schematic::Schematic for #name {
20+
impl #impl_generics schematic::Schematic for #name #ty_generics #where_clause {
2021
fn schema_name() -> Option<String> {
2122
Some(#schema_name.into())
2223
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![allow(dead_code, deprecated)]
2+
3+
use schematic::Schematic;
4+
use std::collections::HashMap;
5+
6+
#[derive(Schematic)]
7+
struct Basic<'a> {
8+
pub name: &'a str,
9+
}
10+
11+
#[derive(Schematic)]
12+
struct Multiple<'a, 'b> {
13+
pub name: &'a str,
14+
pub age: &'b u32,
15+
}
16+
17+
#[derive(Schematic)]
18+
struct Collections<'a, 'b> {
19+
pub map: HashMap<&'a String, &'a bool>,
20+
pub map_alt: &'a HashMap<String, bool>,
21+
pub list: Vec<&'b u32>,
22+
pub list_alt: &'b [u32],
23+
}

0 commit comments

Comments
 (0)