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,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;
}
}
}