use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use crate::compression::Compression;
use crate::error::WasmCheckError;
use crate::limits::{Budget, MaxDelta};
use crate::size::SizeReport;
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
files: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
budget: Option<Budget>,
#[serde(skip_serializing_if = "Option::is_none")]
max_delta: Option<MaxDelta>,
#[serde(skip_serializing_if = "Compression::is_default")]
compression: Compression,
#[serde(skip_serializing_if = "Option::is_none")]
baseline_file: Option<String>,
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
baseline: BTreeMap<String, SizeReport>,
strict: bool,
}
impl Config {
#[must_use]
pub const fn new() -> Self {
Self {
files: Vec::new(),
budget: None,
max_delta: None,
compression: Compression::cdn(),
baseline_file: None,
baseline: BTreeMap::new(),
strict: false,
}
}
#[must_use]
pub fn files(&self) -> &[String] {
&self.files
}
pub fn add_file(&mut self, file: impl Into<String>) {
let file = file.into();
if !self.files.contains(&file) {
self.files.push(file);
}
}
#[must_use]
pub fn budget(&self) -> Option<&Budget> {
self.budget.as_ref()
}
pub fn set_budget(&mut self, budget: Option<Budget>) {
self.budget = budget;
}
#[must_use]
pub fn max_delta(&self) -> Option<&MaxDelta> {
self.max_delta.as_ref()
}
pub fn set_max_delta(&mut self, max_delta: Option<MaxDelta>) {
self.max_delta = max_delta;
}
#[must_use]
pub const fn compression(&self) -> Compression {
self.compression
}
pub fn set_compression(&mut self, compression: Compression) {
self.compression = compression;
}
#[must_use]
pub fn baseline_file(&self) -> Option<&str> {
self.baseline_file.as_deref()
}
pub fn set_baseline_file(&mut self, file: Option<String>) {
self.baseline_file = file;
}
#[must_use]
pub fn baseline_path(&self, config_dir: impl AsRef<Path>) -> PathBuf {
let dir = config_dir.as_ref();
let dir = if dir == Path::new(".") || dir.as_os_str().is_empty() {
Path::new("")
} else {
dir
};
match &self.baseline_file {
Some(file) => dir.join(file),
None => dir.join(crate::DEFAULT_BASELINE_FILE),
}
}
#[must_use]
pub fn inline_baseline(&self) -> &BTreeMap<String, SizeReport> {
&self.baseline
}
#[must_use]
pub fn take_inline_baseline(&mut self) -> BTreeMap<String, SizeReport> {
std::mem::take(&mut self.baseline)
}
pub fn clear_inline_baseline(&mut self) {
self.baseline.clear();
}
#[must_use]
pub const fn strict(&self) -> bool {
self.strict
}
pub fn set_strict(&mut self, strict: bool) {
self.strict = strict;
}
pub fn load(path: impl AsRef<Path>) -> Result<Self, WasmCheckError> {
let path = path.as_ref();
let text = std::fs::read_to_string(path).map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
WasmCheckError::ConfigNotFound(path.to_path_buf())
} else {
WasmCheckError::Io(e)
}
})?;
serde_json::from_str(&text).map_err(|source| WasmCheckError::ConfigParse {
path: path.to_path_buf(),
source,
})
}
pub fn save(&self, path: impl AsRef<Path>) -> Result<(), WasmCheckError> {
let mut json = serde_json::to_string_pretty(self)?;
json.push('\n');
std::fs::write(path, json).map_err(WasmCheckError::Io)
}
}
#[must_use]
pub(crate) fn is_glob(entry: &str) -> bool {
entry.contains(['*', '?', '[', ']'])
}