Compare commits
No commits in common. "f5935b673ba58ef137279f78b07fb6d54d7845e5" and "7364eec25f7dc603cfe75a0358fa41af2d34bf4c" have entirely different histories.
f5935b673b
...
7364eec25f
@ -4,11 +4,9 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
description = "macro for creating configurations using miniconf"
|
||||
license = "MIT OR Apache-2.0"
|
||||
repository = "https://git.mkaenner.de/max/macroconf"
|
||||
readme = "README.md"
|
||||
repository = "https://git.mkaenner.de/max/macroconf.git"
|
||||
categories = ["config", "embedded", "data-structures"]
|
||||
keywords = ["miniconf", "macro", "config"]
|
||||
authors = ["Max Känner <max.kaenner@gmail.com>"]
|
||||
|
||||
[lints.rust]
|
||||
unsafe_code = "forbid"
|
||||
|
47
src/lib.rs
47
src/lib.rs
@ -1,11 +1,8 @@
|
||||
//! This crate creates `miniconf::Tree` implementations fields in a struct. These carry some extra
|
||||
//! extra information about the field.
|
||||
|
||||
use std::iter::once;
|
||||
|
||||
use convert_case::{Case, Casing};
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::{Span as Span2, TokenStream as TokenStream2, TokenTree as TokenTree2};
|
||||
use proc_macro2::{Span as Span2, TokenStream as TokenStream2};
|
||||
use quote::{format_ident, quote, ToTokens};
|
||||
use syn::{
|
||||
parse_macro_input, parse_quote, Attribute, DataStruct, DeriveInput, Expr, ExprLit, Field,
|
||||
@ -15,20 +12,6 @@ use syn::{
|
||||
/// Creates structs for the values to extend them with extra metadata.
|
||||
///
|
||||
/// supported metadata is `min`, `max` and `default`. Doc comments are parsed as `description`
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// use macroconf::config;
|
||||
///
|
||||
/// #[config]
|
||||
/// struct Config {
|
||||
/// /// This will be parsed as description
|
||||
/// #[min] // This will use i32::MIN for the minimum
|
||||
/// #[max = 50] // The value 50 is used for the maximum
|
||||
/// #[default = 42] // A `Default` implementation will be generated returning 42
|
||||
/// field1: i32,
|
||||
/// }
|
||||
/// ```
|
||||
#[proc_macro_attribute]
|
||||
pub fn config(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
let mut input = parse_macro_input!(item as DeriveInput);
|
||||
@ -54,32 +37,6 @@ pub fn config(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(attr) = input
|
||||
.attrs
|
||||
.iter_mut()
|
||||
.find(|attr| attr.path().is_ident("derive"))
|
||||
{
|
||||
if let Ok(meta) = attr.meta.require_list() {
|
||||
let derives_tree = meta
|
||||
.tokens
|
||||
.clone()
|
||||
.into_iter()
|
||||
.filter_map(|token| match token {
|
||||
TokenTree2::Ident(ident) if ident == Ident::new("Tree", ident.span()) => {
|
||||
Some(ident)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.count()
|
||||
== 1;
|
||||
if !derives_tree {
|
||||
input.attrs.push(parse_quote!(#[derive(::miniconf::Tree)]));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
input.attrs.push(parse_quote!(#[derive(::miniconf::Tree)]));
|
||||
}
|
||||
|
||||
quote! {
|
||||
#input
|
||||
#(#new_types)*
|
||||
@ -382,7 +339,7 @@ fn generate_serde(ident: &Ident, ty: &Type, checked_new: bool) -> TokenStream2 {
|
||||
let conversion = if checked_new {
|
||||
quote! {
|
||||
Self::new(value).ok_or_else(|| {
|
||||
<D::Error as ::serde::de::Error>::custom("checking value bounds")
|
||||
D::Error::custom("checking value bounds")
|
||||
})
|
||||
}
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user