armour-derive 0.28.0

DDL and serialization for key-value storage
Documentation
use syn::{Attribute, Expr, ExprLit, Lit, MetaNameValue, Path};

/// `#[idx = N]` on fields (same semantics as rapira-derive).
pub fn extract_idx_attr(attrs: &[Attribute]) -> Option<u32> {
    attrs.iter().find_map(|attr| {
        if !attr.path().is_ident("idx") {
            return None;
        }
        let nv = attr.meta.require_name_value().ok()?;
        match &nv.value {
            Expr::Lit(ExprLit {
                lit: Lit::Int(i), ..
            }) => Some(i.base10_parse::<u32>().expect("idx must be integer")),
            _ => panic!("#[idx] value must be an integer literal"),
        }
    })
}

/// `#[rapira(skip)]` on fields.
pub fn skip_attr(attrs: &[Attribute]) -> bool {
    attrs.iter().any(|attr| {
        if !attr.path().is_ident("rapira") {
            return false;
        }
        match attr.parse_args::<Path>() {
            Ok(path) => path.is_ident("skip"),
            _ => false,
        }
    })
}

/// `#[rapira(with = path)]` on fields.
pub fn has_rapira_with_attr(attrs: &[Attribute]) -> bool {
    attrs.iter().any(|attr| {
        if !attr.path().is_ident("rapira") {
            return false;
        }
        let Ok(nv) = attr.parse_args::<MetaNameValue>() else {
            return false;
        };
        nv.path.is_ident("with")
    })
}