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":"b7f47ddf60a5d6660db16e33901ffd8ddfeb955bdb5fc172c77c260697bcc956","src/config_types/bool_config.rs":"f7ce3ca87f6777cf580cd59689d6e107a2907234dc0217235639d3b47bc3e3d7","src/config_types/bool_or_data.rs":"94d7b9b08f62c37d09f11c6d22d64902f8fa142bccb55e84dea535585a61f7b5","src/config_types/mod.rs":"2e3f7b192e853db103607e6274ee60de30126e5eb15e1182d044ef0138ed8220","src/config_types/option.rs":"6531b89fa309f61eb8434876eb44d0cb2ec3a04dfb8a8b9e8fd4a74c4811600b","src/lib.rs":"7b41bf762b51c607d8cc3e90edd65a8bdc6d503361467e55ec7c83b8a267fa6c","src/macros.rs":"01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b","src/merge.rs":"dc7c6e9a3d424b83a207f85d8b4654889f8d83b800b9902f1703e8424a115b9c","tests/config_types.rs":"feb80fa2c082cdd3c69ee6577e3bb6ba58115dd3f32422790ef492e7867a25dc","tests/derive_merge.rs":"2bec19a29405053c9810a185bb79b122e4ab73ea32f4ca00a9a8df0639fa2207"},"package":"9ba1c7a40d38f9dd4e9a046975d3faf95af42937b34b2b963be4d8f01239584b"}

View file

@ -0,0 +1,37 @@
# 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 = "swc_config"
version = "0.1.7"
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "Types for configuring swc"
documentation = "https://rustdoc.swc.rs/swc_config/"
license = "Apache-2.0"
repository = "https://github.com/swc-project/swc.git"
resolver = "1"
[lib]
bench = false
[dependencies.indexmap]
version = "1.9.3"
[dependencies.serde]
version = "1"
features = ["derive"]
[dependencies.serde_json]
version = "1"
[dependencies.swc_config_macro]
version = "0.1.2"

View file

@ -0,0 +1,54 @@
use serde::{Deserialize, Serialize};
use crate::merge::Merge;
/// You can create this type like `true.into()` or `false.into()`.
#[derive(
Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
pub struct BoolConfig<const DEFAULT: bool>(#[serde(default)] Option<bool>);
impl<const DEFAULT: bool> BoolConfig<DEFAULT> {
/// Creates a new `BoolConfig` with the given value.
#[inline]
pub fn new(value: Option<bool>) -> Self {
Self(value)
}
/// Returns the value specified by the user or the default value.
#[inline]
pub fn into_bool(self) -> bool {
self.into()
}
}
impl<const DEFAULT: bool> From<BoolConfig<DEFAULT>> for bool {
#[inline]
fn from(v: BoolConfig<DEFAULT>) -> Self {
match v.0 {
Some(v) => v,
_ => DEFAULT,
}
}
}
impl<const DEFAULT: bool> From<Option<bool>> for BoolConfig<DEFAULT> {
#[inline]
fn from(v: Option<bool>) -> Self {
Self(v)
}
}
impl<const DEFAULT: bool> From<bool> for BoolConfig<DEFAULT> {
#[inline]
fn from(v: bool) -> Self {
Self(Some(v))
}
}
impl<const DEFAULT: bool> Merge for BoolConfig<DEFAULT> {
#[inline]
fn merge(&mut self, other: Self) {
self.0.merge(other.0);
}
}

View file

@ -0,0 +1,140 @@
use serde::{Deserialize, Serialize};
use crate::merge::Merge;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct BoolOrDataConfig<T>(#[serde(default)] Option<BoolOr<T>>);
impl<T> BoolOrDataConfig<T> {
pub fn from_bool(v: bool) -> Self {
Self(Some(BoolOr::Bool(v)))
}
pub fn from_obj(v: T) -> Self {
v.into()
}
pub fn as_ref(&self) -> BoolOrDataConfig<&T> {
match &self.0 {
Some(BoolOr::Data(v)) => BoolOrDataConfig::from_obj(v),
Some(BoolOr::Bool(b)) => BoolOrDataConfig::from_bool(*b),
None => BoolOrDataConfig::default(),
}
}
pub fn or<F>(self, default: F) -> Self
where
F: FnOnce() -> Self,
{
match self.0 {
Some(..) => self,
None => default(),
}
}
pub fn unwrap_as_option<F>(self, default: F) -> Option<T>
where
F: FnOnce(Option<bool>) -> Option<T>,
{
match self.0 {
Some(BoolOr::Data(v)) => Some(v),
Some(BoolOr::Bool(b)) => default(Some(b)),
None => default(None),
}
}
pub fn map<F, N>(self, op: F) -> BoolOrDataConfig<N>
where
F: FnOnce(T) -> N,
{
match self.0 {
Some(BoolOr::Data(v)) => BoolOrDataConfig::from_obj(op(v)),
Some(BoolOr::Bool(b)) => BoolOrDataConfig::from_bool(b),
None => BoolOrDataConfig::default(),
}
}
pub fn is_true(&self) -> bool {
matches!(self.0, Some(BoolOr::Bool(true)))
}
pub fn is_false(&self) -> bool {
matches!(self.0, Some(BoolOr::Bool(false)))
}
pub fn is_obj(&self) -> bool {
matches!(self.0, Some(BoolOr::Data(_)))
}
pub fn into_inner(self) -> Option<BoolOr<T>> {
self.0
}
}
impl<T> From<T> for BoolOrDataConfig<T> {
fn from(v: T) -> Self {
Self(Some(BoolOr::Data(v)))
}
}
impl<T> Default for BoolOrDataConfig<T> {
fn default() -> Self {
Self(Default::default())
}
}
impl<T> Merge for BoolOrDataConfig<T> {
#[inline]
fn merge(&mut self, other: Self) {
self.0.merge(other.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(untagged)]
pub enum BoolOr<T> {
Bool(bool),
Data(T),
}
impl<'de, T> Deserialize<'de> for BoolOr<T>
where
T: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum Deser<T> {
Bool(bool),
Obj(T),
EmptyObject(EmptyStruct),
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct EmptyStruct {}
use serde::__private::de;
let content = de::Content::deserialize(deserializer)?;
let deserializer = de::ContentRefDeserializer::<D::Error>::new(&content);
let res = Deser::deserialize(deserializer);
match res {
Ok(v) => Ok(match v {
Deser::Bool(v) => BoolOr::Bool(v),
Deser::Obj(v) => BoolOr::Data(v),
Deser::EmptyObject(_) => BoolOr::Bool(true),
}),
Err(..) => {
let d = de::ContentDeserializer::<D::Error>::new(content);
Ok(BoolOr::Data(T::deserialize(d)?))
}
}
}
}

View file

@ -0,0 +1,9 @@
pub use self::{
bool_config::BoolConfig,
bool_or_data::{BoolOr, BoolOrDataConfig},
option::MergingOption,
};
mod bool_config;
mod bool_or_data;
mod option;

View file

@ -0,0 +1,47 @@
use serde::{Deserialize, Serialize};
use crate::merge::Merge;
/// You can create this type like `Some(...).into()` or `None.into()`.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct MergingOption<T>(Option<T>)
where
T: Merge + Default;
impl<T> MergingOption<T>
where
T: Merge + Default,
{
pub fn into_inner(self) -> Option<T> {
self.0
}
pub fn as_ref(&self) -> Option<&T> {
self.0.as_ref()
}
}
impl<T> From<Option<T>> for MergingOption<T>
where
T: Merge + Default,
{
#[inline]
fn from(v: Option<T>) -> Self {
Self(v)
}
}
impl<T> Merge for MergingOption<T>
where
T: Merge + Default,
{
fn merge(&mut self, other: Self) {
if let Some(other) = other.0 {
if self.0.is_none() {
self.0 = Some(Default::default());
}
self.0.as_mut().unwrap().merge(other);
}
}
}

View file

@ -0,0 +1,6 @@
//! Configuration for swc
#[macro_use]
mod macros;
pub mod config_types;
pub mod merge;

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,83 @@
use std::{collections::HashMap, path::PathBuf};
use indexmap::IndexMap;
pub use swc_config_macro::Merge;
/// Deriavable trait for overrding configurations.
///
/// Typically, correct implementation of this trait for a struct is calling
/// merge for all fields, and `#[derive(Merge)]` will do it for you.
pub trait Merge: Sized {
/// `self` has higher priority.
fn merge(&mut self, other: Self);
}
/// Modifies `self` iff `self` is [None]
impl<T> Merge for Option<T> {
#[inline]
fn merge(&mut self, other: Self) {
if self.is_none() {
*self = other;
}
}
}
impl<T> Merge for Box<T>
where
T: Merge,
{
#[inline]
fn merge(&mut self, other: Self) {
(**self).merge(*other);
}
}
/// Modifies `self` iff `self` is empty.
impl<T> Merge for Vec<T> {
#[inline]
fn merge(&mut self, other: Self) {
if self.is_empty() {
*self = other;
}
}
}
/// Modifies `self` iff `self` is empty.
impl<K, V, S> Merge for HashMap<K, V, S> {
#[inline]
fn merge(&mut self, other: Self) {
if self.is_empty() {
*self = other;
}
}
}
/// Modifies `self` iff `self` is empty.
impl<K, V, S> Merge for IndexMap<K, V, S> {
#[inline]
fn merge(&mut self, other: Self) {
if self.is_empty() {
*self = other;
}
}
}
/// Modifies `self` iff `self` is empty.
impl Merge for String {
#[inline]
fn merge(&mut self, other: Self) {
if self.is_empty() {
*self = other;
}
}
}
/// Modifies `self` iff `self` is empty.
impl Merge for PathBuf {
#[inline]
fn merge(&mut self, other: Self) {
if self.as_os_str().is_empty() {
*self = other;
}
}
}

View file

@ -0,0 +1,29 @@
use serde_json::Value;
use swc_config::config_types::BoolConfig;
fn bool_config(v: Value) -> BoolConfig<false> {
serde_json::from_value(v).unwrap()
}
#[test]
fn test_bool_config_serde() {
assert_eq!(bool_config(Value::Null), BoolConfig::new(None));
assert_eq!(bool_config(Value::Bool(true)), BoolConfig::new(Some(true)));
assert_eq!(
bool_config(Value::Bool(false)),
BoolConfig::new(Some(false))
);
}
#[test]
fn test_bool_config_default() {
assert_eq!(
BoolConfig::<false>::default(),
BoolConfig::<false>::new(None)
);
assert_eq!(BoolConfig::<true>::default(), BoolConfig::<true>::new(None));
assert!(!BoolConfig::<false>::default().into_bool());
assert!(BoolConfig::<true>::default().into_bool());
}

View file

@ -0,0 +1,24 @@
use swc_config::merge::Merge;
#[derive(Merge)]
struct Fields {
a: Option<()>,
}
#[test]
fn test_fields() {
let mut fields = Fields { a: None };
fields.merge(Fields { a: Some(()) });
assert_eq!(fields.a, Some(()));
}
#[derive(Merge)]
struct Tuple(Option<()>);
#[test]
fn test_tuple() {
let mut tuple = Tuple(None);
tuple.merge(Tuple(Some(())));
assert_eq!(tuple.0, Some(()));
}