kago-macros 0.2.2

A macros for Kago.
Documentation
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Ident, Type};

pub fn impl_zero(struct_name: &Ident) -> TokenStream {
    quote! {
        impl num_traits::Zero for #struct_name {
            fn zero() -> Self {
                Self::new(0)
            }

            fn is_zero(&self) -> bool {
                self.0 == Self::zero().0
            }
        }
    }
}

pub fn impl_one(struct_name: &Ident) -> TokenStream {
    quote! {
        impl num_traits::One for #struct_name {
            fn one() -> Self {
                Self::new(1)
            }

            fn is_one(&self) -> bool {
                self.0 == Self::one().0
            }
        }
    }
}

pub fn impl_num(struct_name: &Ident, ty: &Type) -> TokenStream {
    quote! {
        impl num_traits::Num for #struct_name {
            type FromStrRadixErr = std::num::ParseIntError;

            fn from_str_radix(s: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
                let value = #ty::from_str_radix(s, radix)?;
                Ok(Self::new(value))
            }
        }
    }
}