is-macro-0.3.0/.cargo_vcs_info.json0000644000000001360000000000100125460ustar { "git": { "sha1": "cac0aaa89cf5d1fe07d32c86cc6c2b6b4b1434bd" }, "path_in_vcs": "" }is-macro-0.3.0/.gitignore000064400000000000000000000002371046102023000133300ustar 00000000000000 target/ **/*.bk core *.log Cargo.lock .vscode/* !.vscode/settings.json /.idea *.iml .DS_Store node_modules/ # Flamegraph *.html *.svg package-lock.jsonis-macro-0.3.0/Cargo.toml0000644000000021210000000000100105400ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "is-macro" version = "0.3.0" authors = ["강동윤 "] description = "Easily create methods for to use yout custom enum like Option / Result" documentation = "https://docs.rs/is-macro" license = "MIT" repository = "https://github.com/kdy1/is-macro" [lib] proc-macro = true [dependencies.Inflector] version = "0.11.4" default-features = false [dependencies.pmutil] version = "0.6.0" [dependencies.proc-macro2] version = "1" [dependencies.quote] version = "1" [dependencies.syn] version = "2" features = [ "fold", "full", "derive", "extra-traits", "fold", ] is-macro-0.3.0/Cargo.toml.orig000064400000000000000000000011771046102023000142330ustar 00000000000000[package] name = "is-macro" description = "Easily create methods for to use yout custom enum like Option / Result" version = "0.3.0" documentation = "https://docs.rs/is-macro" repository = "https://github.com/kdy1/is-macro" authors = ["강동윤 "] edition = "2018" license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] proc-macro = true [dependencies] pmutil = "0.6.0" proc-macro2 = "1" quote = "1" syn = { version = "2", features = ["fold", "full", "derive", "extra-traits", "fold"] } Inflector = { version = "0.11.4", default-features = false } is-macro-0.3.0/src/lib.rs000064400000000000000000000362701046102023000132510ustar 00000000000000extern crate proc_macro; use inflector::Inflector; use pmutil::{smart_quote, Quote, ToTokensExt}; use proc_macro2::Span; use quote::quote; use syn::parse::Parse; use syn::punctuated::{Pair, Punctuated}; use syn::spanned::Spanned; use syn::{ parse, parse2, Data, DataEnum, DeriveInput, Expr, ExprLit, Field, Fields, Generics, Ident, ImplItem, ItemImpl, Lit, Meta, MetaNameValue, Path, Token, Type, TypePath, TypeReference, TypeTuple, WhereClause, }; /// A proc macro to generate methods like is_variant / expect_variant. /// /// /// # Example /// /// ```rust /// /// use is_macro::Is; /// #[derive(Debug, Is)] /// pub enum Enum { /// A, /// B(T), /// C(Option), /// } /// /// // Rust's type inference cannot handle this. /// assert!(Enum::<()>::A.is_a()); /// /// assert_eq!(Enum::B(String::from("foo")).b(), Some(String::from("foo"))); /// /// assert_eq!(Enum::B(String::from("foo")).expect_b(), String::from("foo")); /// ``` /// /// # Renaming /// /// ```rust /// /// use is_macro::Is; /// #[derive(Debug, Is)] /// pub enum Enum { /// #[is(name = "video_mp4")] /// VideoMp4, /// } /// /// assert!(Enum::VideoMp4.is_video_mp4()); /// ``` #[proc_macro_derive(Is, attributes(is))] pub fn is(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let input: DeriveInput = syn::parse(input).expect("failed to parse derive input"); let generics: Generics = input.generics.clone(); let items = match input.data { Data::Enum(e) => expand(e), _ => panic!("`Is` can be applied only on enums"), }; ItemImpl { attrs: vec![], defaultness: None, unsafety: None, impl_token: Default::default(), generics: Default::default(), trait_: None, self_ty: Box::new(Type::Path(TypePath { qself: None, path: Path::from(input.ident), })), brace_token: Default::default(), items, } .with_generics(generics) .dump() .into() } #[derive(Debug)] struct Input { name: String, } impl Parse for Input { fn parse(input: parse::ParseStream) -> syn::Result { let _: Ident = input.parse()?; let _: Token![=] = input.parse()?; let name = input.parse::()?; Ok(Input { name: match name.lit { Lit::Str(s) => s.value(), _ => panic!("is(name = ...) expects a string literal"), }, }) } } fn expand(input: DataEnum) -> Vec { let mut items = vec![]; for v in &input.variants { let attrs = v .attrs .iter() .filter(|attr| attr.path().is_ident("is")) .collect::>(); if attrs.len() >= 2 { panic!("derive(Is) expects no attribute or one attribute") } let i = match attrs.into_iter().next() { None => Input { name: { v.ident.to_string().to_snake_case() // }, }, Some(attr) => { // let mut input = Input { name: Default::default(), }; let mut apply = |v: &MetaNameValue| { assert!( v.path.is_ident("name"), "Currently, is() only supports `is(name = 'foo')`" ); input.name = match &v.value { Expr::Lit(ExprLit { lit: Lit::Str(s), .. }) => s.value(), _ => unimplemented!( "is(): name must be a string literal but {:?} is provided", v.value ), }; }; match &attr.meta { Meta::NameValue(v) => { // apply(v) } Meta::List(l) => { // Handle is(name = "foo") input = parse2(l.tokens.clone()).expect("failed to parse input"); } _ => unimplemented!("is({:?})", attr.meta), } input } }; let name = &*i.name; { let name_of_is = Ident::new(&format!("is_{name}"), v.ident.span()); let docs_of_is = format!( "Returns `true` if `self` is of variant [`{variant}`].\n\n\ [`{variant}`]: #variant.{variant}", variant = v.ident, ); items.extend( Quote::new_call_site() .quote_with(smart_quote!( Vars { docs_of_is, name_of_is, Variant: &v.ident }, { impl Type { #[doc = docs_of_is] #[inline] pub const fn name_of_is(&self) -> bool { match *self { Self::Variant { .. } => true, _ => false, } } } } )) .parse::() .items, ); } { let name_of_cast = Ident::new(&format!("as_{name}"), v.ident.span()); let name_of_cast_mut = Ident::new(&format!("as_mut_{name}"), v.ident.span()); let name_of_expect = Ident::new(&format!("expect_{name}"), v.ident.span()); let name_of_take = Ident::new(name, v.ident.span()); let docs_of_cast = format!( "Returns `Some` if `self` is a reference of variant [`{variant}`], and `None` otherwise.\n\n\ [`{variant}`]: #variant.{variant}", variant = v.ident, ); let docs_of_cast_mut = format!( "Returns `Some` if `self` is a mutable reference of variant [`{variant}`], and `None` otherwise.\n\n\ [`{variant}`]: #variant.{variant}", variant = v.ident, ); let docs_of_expect = format!( "Unwraps the value, yielding the content of [`{variant}`].\n\n\ # Panics\n\n\ Panics if the value is not [`{variant}`], with a panic message including \ the content of `self`.\n\n\ [`{variant}`]: #variant.{variant}", variant = v.ident, ); let docs_of_take = format!( "Returns `Some` if `self` is of variant [`{variant}`], and `None` otherwise.\n\n\ [`{variant}`]: #variant.{variant}", variant = v.ident, ); if let Fields::Unnamed(fields) = &v.fields { let types = fields.unnamed.iter().map(|f| f.ty.clone()); let cast_ty = types_to_type(types.clone().map(|ty| add_ref(false, ty))); let cast_ty_mut = types_to_type(types.clone().map(|ty| add_ref(true, ty))); let ty = types_to_type(types); let mut fields: Punctuated = fields .unnamed .clone() .into_pairs() .enumerate() .map(|(i, pair)| { let handle = |f: Field| { // Ident::new(&format!("v{i}"), f.span()) }; match pair { Pair::Punctuated(v, p) => Pair::Punctuated(handle(v), p), Pair::End(v) => Pair::End(handle(v)), } }) .collect(); // Make sure that we don't have any trailing punctuation // This ensure that if we have a single unnamed field, // we will produce a value of the form `(v)`, // not a single-element tuple `(v,)` if let Some(mut pair) = fields.pop() { if let Pair::Punctuated(v, _) = pair { pair = Pair::End(v); } fields.extend(std::iter::once(pair)); } items.extend( Quote::new_call_site() .quote_with(smart_quote!( Vars { docs_of_cast, docs_of_cast_mut, docs_of_expect, docs_of_take, name_of_cast, name_of_cast_mut, name_of_expect, name_of_take, Variant: &v.ident, Type: &ty, CastType: &cast_ty, CastTypeMut: &cast_ty_mut, v: &fields, }, { impl Type { #[doc = docs_of_cast] #[inline] pub fn name_of_cast(&self) -> Option { match self { Self::Variant(v) => Some((v)), _ => None, } } #[doc = docs_of_cast_mut] #[inline] pub fn name_of_cast_mut(&mut self) -> Option { match self { Self::Variant(v) => Some((v)), _ => None, } } #[doc = docs_of_expect] #[inline] pub fn name_of_expect(self) -> Type where Self: ::std::fmt::Debug, { match self { Self::Variant(v) => (v), _ => panic!("called expect on {:?}", self), } } #[doc = docs_of_take] #[inline] pub fn name_of_take(self) -> Option { match self { Self::Variant(v) => Some((v)), _ => None, } } } } )) .parse::() .items, ); } } } items } fn types_to_type(types: impl Iterator) -> Type { let mut types: Punctuated<_, _> = types.collect(); if types.len() == 1 { types.pop().expect("len is 1").into_value() } else { TypeTuple { paren_token: Default::default(), elems: types, } .into() } } fn add_ref(mutable: bool, ty: Type) -> Type { Type::Reference(TypeReference { and_token: Default::default(), lifetime: None, mutability: if mutable { Some(Default::default()) } else { None }, elem: Box::new(ty), }) } /// Extension trait for `ItemImpl` (impl block). trait ItemImplExt { /// Instead of /// /// ```rust,ignore /// let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); /// /// let item: Item = Quote::new(def_site::()) /// .quote_with(smart_quote!( /// Vars { /// Type: type_name, /// impl_generics, /// ty_generics, /// where_clause, /// }, /// { /// impl impl_generics ::swc_common::AstNode for Type ty_generics /// where_clause {} /// } /// )).parse(); /// ``` /// /// You can use this like /// /// ```rust,ignore // let item = Quote::new(def_site::()) /// .quote_with(smart_quote!(Vars { Type: type_name }, { /// impl ::swc_common::AstNode for Type {} /// })) /// .parse::() /// .with_generics(input.generics); /// ``` fn with_generics(self, generics: Generics) -> Self; } impl ItemImplExt for ItemImpl { fn with_generics(mut self, mut generics: Generics) -> Self { // TODO: Check conflicting name let need_new_punct = !generics.params.empty_or_trailing(); if need_new_punct { generics .params .push_punct(syn::token::Comma(Span::call_site())); } // Respan if let Some(t) = generics.lt_token { self.generics.lt_token = Some(t) } if let Some(t) = generics.gt_token { self.generics.gt_token = Some(t) } let ty = self.self_ty; // Handle generics defined on struct, enum, or union. let mut item: ItemImpl = { let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); let item = if let Some((ref polarity, ref path, ref for_token)) = self.trait_ { quote! { impl #impl_generics #polarity #path #for_token #ty #ty_generics #where_clause {} } } else { quote! { impl #impl_generics #ty #ty_generics #where_clause {} } }; parse(item.dump().into()) .unwrap_or_else(|err| panic!("with_generics failed: {}\n{}", err, item.dump())) }; // Handle generics added by proc-macro. item.generics .params .extend(self.generics.params.into_pairs()); match self.generics.where_clause { Some(WhereClause { ref mut predicates, .. }) => predicates.extend( generics .where_clause .into_iter() .flat_map(|wc| wc.predicates.into_pairs()), ), ref mut opt @ None => *opt = generics.where_clause, } ItemImpl { attrs: self.attrs, defaultness: self.defaultness, unsafety: self.unsafety, impl_token: self.impl_token, brace_token: self.brace_token, items: self.items, ..item } } } is-macro-0.3.0/tests/arg_count.rs000064400000000000000000000002161046102023000150260ustar 00000000000000use is_macro::Is; #[derive(Debug, Is)] pub enum Enum { A(), B(usize, usize), C(String), D(&'static str, &'static mut u32), } is-macro-0.3.0/tests/as_mut.rs000064400000000000000000000004771046102023000143460ustar 00000000000000use is_macro::Is; #[derive(Debug, PartialEq, Is)] pub enum Enum { A(u32), B(Vec), } #[test] fn test() { let mut e = Enum::A(0); *e.as_mut_a().unwrap() += 1; assert_eq!(e, Enum::A(1)); let mut e = Enum::B(vec![]); e.as_mut_b().unwrap().push(1); assert_eq!(e, Enum::B(vec![1])); } is-macro-0.3.0/tests/generic.rs000064400000000000000000000001401046102023000144550ustar 00000000000000use is_macro::Is; #[derive(Debug, Is)] pub enum Enum { A, B(T), C(Option), } is-macro-0.3.0/tests/reference.rs000064400000000000000000000001571046102023000150070ustar 00000000000000use is_macro::Is; #[derive(Debug, Is)] pub enum Enum { A, B(&'static str), C(&'static mut u32), } is-macro-0.3.0/tests/rename.rs000064400000000000000000000002501046102023000143120ustar 00000000000000use is_macro::Is; #[derive(Debug, Is)] pub enum Enum { #[is(name = "video_mp4")] VideoMp4, } #[test] fn test() { assert!(Enum::VideoMp4.is_video_mp4()); } is-macro-0.3.0/tests/trailing_punct.rs000064400000000000000000000001111046102023000160610ustar 00000000000000use is_macro::Is; #[derive(Debug, Is)] pub enum Enum { B(usize,), } is-macro-0.3.0/tests/usage.rs000064400000000000000000000001351046102023000141510ustar 00000000000000use is_macro::Is; #[derive(Debug, Is)] pub enum Enum { A, B(String), C(bool), }