typeshare-annotation-1.0.4/.cargo_vcs_info.json0000644000000001500000000000100152060ustar { "git": { "sha1": "5ded71b75b78862bd2f0e8341da729b75cdebe54" }, "path_in_vcs": "annotation" }typeshare-annotation-1.0.4/Cargo.toml0000644000000015030000000000100132070ustar # 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 = "2021" name = "typeshare-annotation" version = "1.0.4" description = "The annotation used to mark types for typeshare" license = "MIT OR Apache-2.0" repository = "https://github.com/1Password/typeshare" [lib] proc-macro = true [dependencies.quote] version = "1.0" [dependencies.syn] version = "2" features = [ "parsing", "proc-macro", ] typeshare-annotation-1.0.4/Cargo.toml.orig000064400000000000000000000005241046102023000166720ustar 00000000000000[package] name = "typeshare-annotation" version = "1.0.4" edition = "2021" license = "MIT OR Apache-2.0" description = "The annotation used to mark types for typeshare" repository = "https://github.com/1Password/typeshare" [lib] proc-macro = true [dependencies] syn = { version = "2", features = ["parsing", "proc-macro"] } quote = "1.0" typeshare-annotation-1.0.4/rust-toolchain.toml000064400000000000000000000001131046102023000176450ustar 00000000000000[toolchain] channel = "stable" components = ["clippy"] profile = "default" typeshare-annotation-1.0.4/src/lib.rs000064400000000000000000000051271046102023000157120ustar 00000000000000//! Defines the `#[typeshare]` attribute. extern crate proc_macro; use proc_macro::TokenStream; use quote::ToTokens; use syn::{parse, Attribute, Data, DeriveInput, Fields}; /// Marks a type as a type shared across the FFI boundary using typeshare. /// /// The `typeshare` program will process all the types with this attribute. It will ignore all types /// that do not have this attribute. /// /// # Example /// /// ```ignore /// use typeshare::typeshare; /// /// #[typeshare] /// pub struct Person { /// name: String, /// email: String, /// } /// ``` /// /// If the file above is `src/person.rs`, then `typeshare --lang=typescript src/person.rs` would /// generate the typescript bindings for the `Person` type. /// /// # Ignoring enum variants /// If you don't want to typeshare a certain enum variant, just put a `#[typeshare(skip)]` attribute on /// that variant. /// ```ignore /// use typeshare::typeshare; /// /// #[typeshare] /// pub enum SomeEnum { /// A, /// #[typeshare(skip)] /// B, // <- this variant will not be included in the typeshared file(s) /// C, /// } /// ``` #[proc_macro_attribute] pub fn typeshare(_attr: TokenStream, item: TokenStream) -> TokenStream { if let Ok(mut item) = parse::(item.clone()) { // We need to remove the #[typeshare] attribute from all data members so the compiler doesn't throw an error. strip_configuration_attribute(&mut item); TokenStream::from(item.to_token_stream()) } else { item } } fn strip_configuration_attribute(item: &mut DeriveInput) { fn remove_configuration_from_attributes(attributes: &mut Vec) { const CONFIG_ATTRIBUTE_NAME: &str = "typeshare"; attributes.retain(|x| x.path().to_token_stream().to_string() != CONFIG_ATTRIBUTE_NAME); } fn remove_configuration_from_fields(fields: &mut Fields) { for field in fields.iter_mut() { remove_configuration_from_attributes(&mut field.attrs); } } match item.data { Data::Enum(ref mut data_enum) => { for variant in data_enum.variants.iter_mut() { remove_configuration_from_attributes(&mut variant.attrs); remove_configuration_from_fields(&mut variant.fields); } } Data::Struct(ref mut data_struct) => { remove_configuration_from_fields(&mut data_struct.fields); } Data::Union(ref mut data_union) => { for field in data_union.fields.named.iter_mut() { remove_configuration_from_attributes(&mut field.attrs); } } }; }