trl_codegen 1.2.1

This library provides auto generation of some common methods based on Rust macros
Documentation
//! # field_attrs
//!
//! This module contains the `FieldAttrs` struct, which holds information about a field
//! for which a method is going to be generated.

use syn::{Token, punctuated::Punctuated};

use crate::{
    accessors::AccessorArg, fields::FieldParam, modifier::Modifier, new_from_args::NewFromArgs,
};

/// Information about field for which a method is going to be generated
#[derive(Debug)]
pub struct AccessorFieldAttrs {
    /// Method prefix
    pub prefix: String,
    /// Method name
    pub name: String,
    /// Method `self` modifier
    pub modifier: Modifier,
    pub option: bool,
}

impl AccessorFieldAttrs {
    pub fn from_values(
        prefix: String,
        name: String,
        modifier: Modifier,
        option: bool,
    ) -> AccessorFieldAttrs {
        AccessorFieldAttrs {
            prefix,
            name,
            modifier,
            option,
        }
    }
}

impl NewFromArgs<AccessorArg> for AccessorFieldAttrs {
    fn new(args: Punctuated<AccessorArg, Token![,]>) -> AccessorFieldAttrs {
        let mut prefix = String::new();
        let mut name = String::new();
        let mut modifier = Modifier::Ref;
        let mut option = false;

        for arg in args {
            match arg {
                AccessorArg::Prefix(p) => prefix = p,
                AccessorArg::Name(n) => name = n,
                AccessorArg::Modifier(m) => modifier = m,
                AccessorArg::FieldParam(FieldParam::Option) => option = true,
                _ => {}
            }
        }

        AccessorFieldAttrs {
            prefix,
            name,
            modifier,
            option,
        }
    }
}