swc_config/
merge.rs
use std::{collections::HashMap, path::PathBuf};
use indexmap::IndexMap;
pub use swc_config_macro::Merge;
pub trait Merge: Sized {
fn merge(&mut self, other: Self);
}
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);
}
}
impl<T> Merge for Vec<T> {
#[inline]
fn merge(&mut self, other: Self) {
if self.is_empty() {
*self = other;
}
}
}
impl<K, V, S> Merge for HashMap<K, V, S> {
#[inline]
fn merge(&mut self, other: Self) {
if self.is_empty() {
*self = other;
}
}
}
impl<K, V, S> Merge for IndexMap<K, V, S> {
#[inline]
fn merge(&mut self, other: Self) {
if self.is_empty() {
*self = other;
}
}
}
impl Merge for String {
#[inline]
fn merge(&mut self, other: Self) {
if self.is_empty() {
*self = other;
}
}
}
impl Merge for PathBuf {
#[inline]
fn merge(&mut self, other: Self) {
if self.as_os_str().is_empty() {
*self = other;
}
}
}