use std::fmt::{self, Display};
use std::path::{Path, PathBuf};
use serde::Serialize;
use crate::error::Result;
#[derive(Clone, PartialEq)]
pub enum ValuesFile {
Path(PathBuf),
Url(String),
Json(serde_json::Value),
}
impl fmt::Debug for ValuesFile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Path(path) => f.debug_tuple("Path").field(path).finish(),
Self::Url(url) => f.debug_tuple("Url").field(url).finish(),
Self::Json(_) => f.write_str("Json(<redacted>)"),
}
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Values {
pub files: Vec<ValuesFile>,
pub set_json: Vec<String>,
pub set: Vec<String>,
pub set_string: Vec<String>,
pub set_file: Vec<String>,
pub set_literal: Vec<String>,
}
impl Values {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self == &Self::default()
}
#[must_use]
pub fn file(mut self, path: impl AsRef<Path>) -> Self {
self.files.push(ValuesFile::Path(path.as_ref().to_owned()));
self
}
#[must_use]
pub fn url(mut self, url: impl Into<String>) -> Self {
self.files.push(ValuesFile::Url(url.into()));
self
}
#[must_use]
pub fn json(mut self, value: serde_json::Value) -> Self {
self.files.push(ValuesFile::Json(value));
self
}
pub fn serialize(self, value: &impl Serialize) -> Result<Self> {
Ok(self.json(serde_json::to_value(value)?))
}
#[must_use]
pub fn set(mut self, key: impl Display, value: impl Display) -> Self {
self.set.push(format!("{key}={value}"));
self
}
#[must_use]
pub fn set_string(mut self, key: impl Display, value: impl Display) -> Self {
self.set_string.push(format!("{key}={value}"));
self
}
#[must_use]
pub fn set_file(mut self, key: impl Display, path: impl AsRef<Path>) -> Self {
self.set_file
.push(format!("{key}={}", path.as_ref().display()));
self
}
#[must_use]
pub fn set_json(mut self, key: impl Display, value: &serde_json::Value) -> Self {
self.set_json.push(format!("{key}={value}"));
self
}
#[must_use]
pub fn set_literal(mut self, key: impl Display, value: impl Display) -> Self {
self.set_literal.push(format!("{key}={value}"));
self
}
}
#[cfg(test)]
mod tests;