use super::get_tuple_struct_inner_ident;
use proc_macro::TokenStream;
use quote::quote;
use syn::DeriveInput;
pub fn impl_clamp_new(ast: &DeriveInput) -> TokenStream {
let name = &ast.ident;
let inner = get_tuple_struct_inner_ident(ast);
let inner = inner.to_string();
let new_arg = if inner.starts_with("NonZero") {
quote! { self.0.get() }
} else {
quote! { self.0 }
};
let stream = quote! {
#[automatically_derived]
impl #name {
#[inline]
pub const fn clamp(&mut self) {
*self = Self::new(#new_arg);
}
#[inline]
pub const fn clamped(mut self) -> Self {
Self::clamp(&mut self);
self
}
}
};
stream.into()
}