mirror of
https://github.com/kristoferssolo/filecaster.git
synced 2026-02-04 05:52:03 +00:00
refactor(workspace): make a workspace
This commit is contained in:
22
filecaster-derive/Cargo.toml
Normal file
22
filecaster-derive/Cargo.toml
Normal file
@@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "filecaster-derive"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[features]
|
||||
default = []
|
||||
merge = ["dep:merge"]
|
||||
|
||||
[dependencies]
|
||||
proc-macro2 = "1.0"
|
||||
quote = "1.0"
|
||||
proc-macro-error2 = "2.0"
|
||||
syn = { version = "2.0", features = ["extra-traits", "parsing"] }
|
||||
serde.workspace = true
|
||||
merge = { workspace = true, optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
claims.workspace = true
|
||||
358
filecaster-derive/src/from_file.rs
Normal file
358
filecaster-derive/src/from_file.rs
Normal file
@@ -0,0 +1,358 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::{format_ident, quote};
|
||||
use syn::{
|
||||
Attribute, Data, DeriveInput, Error, Expr, Field, Fields, FieldsNamed, GenericParam, Generics,
|
||||
Lit, Meta, MetaList, Result, Type, TypePath, WhereClause, WherePredicate, parse_quote, parse2,
|
||||
};
|
||||
|
||||
const WITH_MERGE: bool = cfg!(feature = "merge");
|
||||
|
||||
/// Entry point: generate the shadow struct + [`FromFile`] impls.
|
||||
pub fn impl_from_file(input: &DeriveInput) -> Result<TokenStream> {
|
||||
let name = &input.ident;
|
||||
let vis = &input.vis;
|
||||
let generics = add_trait_bounds(input.generics.clone());
|
||||
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
|
||||
|
||||
let file_ident = format_ident!("{name}File");
|
||||
|
||||
let fields = extract_named_fields(input)?;
|
||||
let (field_assignments, file_fields, default_bounds) = process_fields(fields)?;
|
||||
|
||||
let where_clause = build_where_clause(where_clause.cloned(), default_bounds)?;
|
||||
let derive_clause = build_derive_clause();
|
||||
|
||||
Ok(quote! {
|
||||
#derive_clause
|
||||
#vis struct #file_ident #ty_generics #where_clause {
|
||||
#(#file_fields),*
|
||||
}
|
||||
|
||||
impl #impl_generics filecaster::FromFile for #name #ty_generics #where_clause {
|
||||
type Shadow = #file_ident #ty_generics;
|
||||
|
||||
fn from_file(file: Option<Self::Shadow>) -> Self {
|
||||
let file = file.unwrap_or_default();
|
||||
Self {
|
||||
#(#field_assignments),*
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl #impl_generics From<Option<#file_ident #ty_generics>> for #name #ty_generics #where_clause {
|
||||
fn from(value: Option<#file_ident #ty_generics>) -> Self {
|
||||
<Self as filecaster::FromFile>::from_file(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl #impl_generics From<#file_ident #ty_generics> for #name #ty_generics #where_clause {
|
||||
fn from(value: #file_ident #ty_generics) -> Self {
|
||||
<Self as filecaster::FromFile>::from_file(Some(value))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Ensure we only work on named-field structs
|
||||
fn extract_named_fields(input: &DeriveInput) -> Result<&FieldsNamed> {
|
||||
match &input.data {
|
||||
Data::Struct(ds) => match &ds.fields {
|
||||
Fields::Named(fields) => Ok(fields),
|
||||
_ => Err(Error::new_spanned(
|
||||
&input.ident,
|
||||
"FromFile can only be derived for structs with named fields",
|
||||
)),
|
||||
},
|
||||
_ => Err(Error::new_spanned(
|
||||
&input.ident,
|
||||
"FromFile can only be derived for structs",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Nested-struct detection
|
||||
fn is_from_file_struct(ty: &Type) -> bool {
|
||||
if let Type::Path(TypePath { qself: None, path }) = ty {
|
||||
return path.segments.len() == 1;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Extract the last identifier from a [`TypePath`].
|
||||
fn last_path_ident(ty: &Type) -> Result<String> {
|
||||
if let Type::Path(TypePath { qself: None, path }) = ty {
|
||||
return Ok(path.segments.last().unwrap().ident.to_string());
|
||||
}
|
||||
|
||||
Err(Error::new_spanned(ty, "expected a plain struct name"))
|
||||
}
|
||||
|
||||
/// Build the shadow field + assignment for one original field
|
||||
fn build_file_field(field: &Field) -> Result<(TokenStream, TokenStream, Option<TokenStream>)> {
|
||||
let ident = field
|
||||
.ident
|
||||
.as_ref()
|
||||
.ok_or_else(|| Error::new_spanned(field, "Expected named fields"))?;
|
||||
let ty = &field.ty;
|
||||
|
||||
let field_attrs = if WITH_MERGE {
|
||||
quote! { #[merge(strategy = merge::option::overwrite_none)] }
|
||||
} else {
|
||||
quote! {}
|
||||
};
|
||||
|
||||
// Nested struct -> delegate to its own `FromFile` impl
|
||||
let field_decl = quote! {
|
||||
#field_attrs
|
||||
pub #ident: Option<#ty>
|
||||
};
|
||||
let assign = quote! {
|
||||
#ident: <#ty as filecaster::FromFile>::from_file(file.#ident)
|
||||
};
|
||||
|
||||
let default_bound = Some(quote! { #ty: Default });
|
||||
|
||||
Ok((field_decl, assign, default_bound))
|
||||
}
|
||||
|
||||
/// Process all fields
|
||||
fn process_fields(
|
||||
fields: &FieldsNamed,
|
||||
) -> Result<(Vec<TokenStream>, Vec<TokenStream>, Vec<TokenStream>)> {
|
||||
fields.named.iter().try_fold(
|
||||
(Vec::new(), Vec::new(), Vec::new()),
|
||||
|(mut assignments, mut file_fields, mut defaults), field| {
|
||||
let (file_field, assignment, default_value) = build_file_field(field)?;
|
||||
file_fields.push(file_field);
|
||||
assignments.push(assignment);
|
||||
if let Some(value) = default_value {
|
||||
defaults.push(value);
|
||||
}
|
||||
Ok((assignments, file_fields, defaults))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Where-clause helpers
|
||||
fn build_where_clause(
|
||||
where_clause: Option<WhereClause>,
|
||||
default_bounds: Vec<TokenStream>,
|
||||
) -> Result<Option<WhereClause>> {
|
||||
if default_bounds.is_empty() {
|
||||
return Ok(where_clause);
|
||||
}
|
||||
|
||||
let mut where_clause = where_clause;
|
||||
if let Some(wc) = &mut where_clause {
|
||||
for bound in default_bounds {
|
||||
let predicate = parse2::<WherePredicate>(bound.clone())
|
||||
.map_err(|_| Error::new_spanned(&bound, "Failed to parse where predicate"))?;
|
||||
wc.predicates.push(predicate);
|
||||
}
|
||||
} else {
|
||||
where_clause = Some(parse_quote!(where #(#default_bounds),*));
|
||||
}
|
||||
Ok(where_clause)
|
||||
}
|
||||
|
||||
/// Derive clause for the shadow struct
|
||||
fn build_derive_clause() -> TokenStream {
|
||||
if WITH_MERGE {
|
||||
return quote! {
|
||||
#[derive(Debug, Clone, Default, serde::Deserialize, serde::Serialize, merge::Merge)]
|
||||
};
|
||||
}
|
||||
|
||||
quote! {
|
||||
#[derive(Debug, Clone, Default, serde::Deserialize, serde::Serialize)]
|
||||
}
|
||||
}
|
||||
|
||||
/// Add Default bound to every generic parameter
|
||||
fn add_trait_bounds(mut generics: Generics) -> Generics {
|
||||
for param in &mut generics.params {
|
||||
if let GenericParam::Type(type_param) = param {
|
||||
type_param.bounds.push(parse_quote!(Default));
|
||||
}
|
||||
}
|
||||
generics
|
||||
}
|
||||
|
||||
/// Attribute parsing: `#[from_file(default = ...)]`
|
||||
fn parse_from_file_default_attr(attrs: &[Attribute]) -> Result<Option<Expr>> {
|
||||
for attr in attrs {
|
||||
if !attr.path().is_ident("from_file") {
|
||||
continue; // Not a #[from_file] attribute, skip it
|
||||
}
|
||||
|
||||
// Parse the content inside the parentheses of #[from_file(...)]
|
||||
return match &attr.meta {
|
||||
Meta::List(meta_list) => parse_default(meta_list),
|
||||
_ => Err(Error::new_spanned(
|
||||
attr,
|
||||
"Expected #[from_file(default = \"literal\")] or similar",
|
||||
)),
|
||||
};
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn parse_default(list: &MetaList) -> Result<Option<Expr>> {
|
||||
let mut default_expr = None;
|
||||
list.parse_nested_meta(|meta| {
|
||||
if meta.path.is_ident("default") {
|
||||
let value = meta.value()?;
|
||||
let expr = value.parse::<Expr>()?;
|
||||
|
||||
if let Expr::Lit(expr_lit) = &expr {
|
||||
if let Lit::Str(lit_str) = &expr_lit.lit {
|
||||
default_expr = Some(parse_quote! {
|
||||
#lit_str.to_string()
|
||||
});
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
default_expr = Some(expr);
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(default_expr)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use claims::{assert_err, assert_none};
|
||||
use quote::ToTokens;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn extract_named_fields_success() {
|
||||
let input: DeriveInput = parse_quote! {
|
||||
struct S { x: i32, y: String }
|
||||
};
|
||||
let fields = extract_named_fields(&input).unwrap();
|
||||
let names = fields
|
||||
.named
|
||||
.iter()
|
||||
.map(|f| f.ident.as_ref().unwrap().to_string())
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(names, vec!["x", "y"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_named_fields_err_on_enum() {
|
||||
let input: DeriveInput = parse_quote! {
|
||||
enum E { A, B }
|
||||
};
|
||||
assert_err!(extract_named_fields(&input));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_named_fields_err_on_tuple_struct() {
|
||||
let input: DeriveInput = parse_quote! {
|
||||
struct T(i32, String);
|
||||
};
|
||||
assert_err!(extract_named_fields(&input));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_default_attrs_none() {
|
||||
let attrs: Vec<Attribute> = vec![parse_quote!(#[foo])];
|
||||
assert_none!(parse_from_file_default_attr(&attrs).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn process_fields_mixed() {
|
||||
let fields: FieldsNamed = parse_quote! {
|
||||
{
|
||||
#[from_file(default = 1)]
|
||||
a: u32,
|
||||
b: String,
|
||||
}
|
||||
};
|
||||
let (assign, file_fields, bounds) = process_fields(&fields).unwrap();
|
||||
// two fields
|
||||
assert_eq!(assign.len(), 2);
|
||||
assert_eq!(file_fields.len(), 2);
|
||||
// a uses unwrap_or_else
|
||||
assert!(
|
||||
assign[0]
|
||||
.to_string()
|
||||
.contains("a : file . a . unwrap_or_else")
|
||||
);
|
||||
// b uses unwrap_or_default
|
||||
assert!(
|
||||
assign[1]
|
||||
.to_string()
|
||||
.contains("b : file . b . unwrap_or_default")
|
||||
);
|
||||
// default-bound should only mention String
|
||||
assert_eq!(bounds.len(), 1);
|
||||
assert!(bounds[0].to_string().contains("String : Default"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_where_clause_to_new() {
|
||||
let bounds = vec![quote! { A: Default }, quote! { B: Default }];
|
||||
let wc = build_where_clause(None, bounds).unwrap().unwrap();
|
||||
let s = wc.to_token_stream().to_string();
|
||||
assert!(s.contains("where A : Default , B : Default"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_where_clause_append_existing() {
|
||||
let orig: WhereClause = parse_quote!(where X: Clone);
|
||||
let bounds = vec![quote! { Y: Default }];
|
||||
let wc = build_where_clause(Some(orig.clone()), bounds)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let preds: Vec<_> = wc
|
||||
.predicates
|
||||
.iter()
|
||||
.map(|p| p.to_token_stream().to_string())
|
||||
.collect();
|
||||
assert!(preds.contains(&"X : Clone".to_string()));
|
||||
assert!(preds.contains(&"Y : Default".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_where_clause_no_bounds_keeps_original() {
|
||||
let orig: WhereClause = parse_quote!(where Z: Eq);
|
||||
let wc = build_where_clause(Some(orig.clone()), vec![])
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let preds: Vec<_> = wc
|
||||
.predicates
|
||||
.iter()
|
||||
.map(|p| p.to_token_stream().to_string())
|
||||
.collect();
|
||||
assert_eq!(preds, vec!["Z : Eq".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_derive_clause_defaults() {
|
||||
let derive_ts = build_derive_clause();
|
||||
let s = derive_ts.to_string();
|
||||
if WITH_MERGE {
|
||||
assert!(s.contains(
|
||||
"derive (Debug , Clone , Default , serde :: Deserialize , serde :: Serialize , merge :: Merge)"
|
||||
));
|
||||
} else {
|
||||
assert!(s.contains(
|
||||
"derive (Debug , Clone , Default , serde :: Deserialize , serde :: Serialize)"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_trait_bouds_appends_default() {
|
||||
let gens: Generics = parse_quote!(<T, U>);
|
||||
let new = add_trait_bounds(gens);
|
||||
let s = new.to_token_stream().to_string();
|
||||
assert!(s.contains("T : Default"));
|
||||
assert!(s.contains("U : Default"));
|
||||
}
|
||||
}
|
||||
97
filecaster-derive/src/lib.rs
Normal file
97
filecaster-derive/src/lib.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
//! # filecaster
|
||||
//!
|
||||
//! `filecaster` is a small `proc-macro` crate that provides a derive‐macro
|
||||
//! `#[derive(FromFile)]` to make it trivial to load partial configurations
|
||||
//! from files, merge them with defaults, and get a fully‐populated struct.
|
||||
//!
|
||||
//! ## What it does
|
||||
//!
|
||||
//! For any struct with named fields, `#[derive(FromFile)]` generates:
|
||||
//!
|
||||
//! 1. A companion `<YourStruct>NameFile` struct in which each field is wrapped
|
||||
//! in `Option<...>`.
|
||||
//! 2. A constructor `YourStruct::from_file(file: Option<YourStructFile>) -> YourStruct`
|
||||
//! that takes your partially‐filled file struct, fills in `None` fields
|
||||
//! with either:
|
||||
//! - an expression you supply via `#[from_file(default = ...)]`, or
|
||||
//! - `Default::default()` (requires `T: Default`)
|
||||
//! 3. An implementation of `From<Option<YourStructFile>> for YourStruct`.
|
||||
//!
|
||||
//! Because each field in the file‐struct is optional, you can deserialize
|
||||
//! e.g. JSON, YAML or TOML into it via Serde, then call `.from_file(...)`
|
||||
//! to get your final struct.
|
||||
//!
|
||||
//! ## Optional per‐field defaults
|
||||
//!
|
||||
//! Use a `#[from_file(default = <expr>)]` attribute on any field to override
|
||||
//! the fallback value. You may supply any expression valid in that struct’s
|
||||
//! context. If you omit it, the macro will require `T: Default` and call
|
||||
//! `unwrap_or_default()`.
|
||||
//!
|
||||
//! Example:
|
||||
//!
|
||||
//! ```rust
|
||||
//! use filecaster::FromFile;
|
||||
//!
|
||||
//! #[derive(Debug, Clone, FromFile)]
|
||||
//! struct AppConfig {
|
||||
//! /// If the user does not specify a host, use `"127.0.0.1"`.
|
||||
//! #[from_file(default = "127.0.0.1")]
|
||||
//! host: String,
|
||||
//!
|
||||
//! /// Number of worker threads; defaults to `4`.
|
||||
//! #[from_file(default = 4)]
|
||||
//! workers: usize,
|
||||
//!
|
||||
//! /// If not set, use `false`.
|
||||
//! auto_reload: bool, // requires `bool: Default`
|
||||
//! }
|
||||
//!
|
||||
//! let file_content = r#"
|
||||
//! {
|
||||
//! "host": "localhost"
|
||||
//! }
|
||||
//! "#;
|
||||
//!
|
||||
//! let config_from_file = serde_json::from_str::<AppConfigFile>(file_content).unwrap();
|
||||
//! // After deserializing the partial config from disk (e.g. with Serde):
|
||||
//! let cfg = AppConfig::from_file(Some(config_from_file));
|
||||
//! println!("{cfg:#?}");
|
||||
//! ```
|
||||
//!
|
||||
//! ## Feature flags
|
||||
//!
|
||||
//! - `merge`
|
||||
//! If you enable the `merge` feature, the generated `<Name>File` struct will
|
||||
//! also derive `merge::Merge`, and you can layer multiple partial files
|
||||
//! together before calling `.from_file(...)`. Any field‐level merge strategy
|
||||
//! annotations (`#[merge(...)]`) are applied automatically.
|
||||
//!
|
||||
//! ## Limitations
|
||||
//!
|
||||
//! - Only works on structs with _named_ fields (no tuple‐structs or enums).
|
||||
//! - All fields without a `#[from_file(default = ...)]` must implement `Default`.
|
||||
//!
|
||||
//! ## License
|
||||
//!
|
||||
//! MIT OR Apache-2.0
|
||||
|
||||
mod from_file;
|
||||
|
||||
pub(crate) use from_file::impl_from_file;
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro_error2::proc_macro_error;
|
||||
use syn::{DeriveInput, parse_macro_input};
|
||||
|
||||
/// Implements the `FromFile` derive macro.
|
||||
///
|
||||
/// This macro processes the `#[from_file]` attribute on structs to generate
|
||||
/// code for loading data from files.
|
||||
#[proc_macro_error]
|
||||
#[proc_macro_derive(FromFile, attributes(from_file))]
|
||||
pub fn derive_from_file(input: TokenStream) -> TokenStream {
|
||||
let inp = parse_macro_input!(input as DeriveInput);
|
||||
impl_from_file(&inp)
|
||||
.unwrap_or_else(|e| e.to_compile_error())
|
||||
.into()
|
||||
}
|
||||
Reference in New Issue
Block a user