Vendor things

This commit is contained in:
John Doty 2024-03-08 11:03:01 -08:00
parent 5deceec006
commit 977e3c17e5
19434 changed files with 10682014 additions and 0 deletions

View file

@ -0,0 +1 @@
{"files":{"Cargo.toml":"8e4bc779a421137be988a47038007c7b09b7989b4f7012247e1226641a4399ef","src/lib.rs":"54412483faeb1255a098cfdaabaf706a894f0147a9f43cb8700fa34054325b37"},"package":"03ec5dc38ee19078d84a692b1c41181ff9f94331c76cee66ff0208c770b5e54f"}

View file

@ -0,0 +1,43 @@
# 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 = "from_variant"
version = "0.1.6"
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "Automatically derive From impls for enums"
documentation = "https://rustdoc.swc.rs/from_variant/"
license = "Apache-2.0"
repository = "https://github.com/swc-project/swc.git"
resolver = "1"
[lib]
bench = false
proc-macro = true
[dependencies.pmutil]
version = "0.6.1"
[dependencies.proc-macro2]
version = "1"
[dependencies.swc_macros_common]
version = "0.3.8"
[dependencies.syn]
version = "2"
features = [
"derive",
"fold",
"parsing",
"printing",
]

View file

@ -0,0 +1,77 @@
extern crate proc_macro;
use pmutil::{smart_quote, Quote};
use swc_macros_common::prelude::*;
use syn::*;
/// Derives [`From`] for all variants. This only supports an enum where every
/// variant has a single field.
#[proc_macro_derive(FromVariant)]
pub fn derive_from_variant(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse::<DeriveInput>(input).expect("failed to parse input as DeriveInput");
let item = derive(input)
.into_iter()
.fold(TokenStream::new(), |mut t, item| {
item.to_tokens(&mut t);
t
});
print("derive(FromVariant)", item)
}
fn derive(
DeriveInput {
generics,
data,
ident,
..
}: DeriveInput,
) -> Vec<ItemImpl> {
let variants = match data {
Data::Enum(DataEnum { variants, .. }) => variants,
_ => panic!("#[derive(FromVariant)] only works for an enum."),
};
let mut from_impls: Vec<ItemImpl> = vec![];
for v in variants {
let variant_name = v.ident;
match v.fields {
Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => {
if unnamed.len() != 1 {
panic!(
"#[derive(FromVariant)] requires all variants to be tuple with exactly \
one field"
)
}
let field = unnamed.into_iter().next().unwrap();
let from_impl = Quote::new(def_site::<Span>())
.quote_with(smart_quote!(
Vars {
VariantType: field.ty,
Variant: variant_name,
Type: &ident,
},
{
impl From<VariantType> for Type {
fn from(v: VariantType) -> Self {
Type::Variant(v)
}
}
}
))
.parse::<ItemImpl>()
.with_generics(generics.clone());
from_impls.push(from_impl);
}
_ => panic!(
"#[derive(FromVariant)] requires all variants to be tuple with exactly one field"
),
}
}
from_impls
}