1use std::fmt::{self, Display};
13use std::path::{Path, PathBuf};
14
15use serde::Serialize;
16
17use crate::error::Result;
18
19#[derive(Clone, PartialEq)]
21pub enum ValuesFile {
22 Path(PathBuf),
24 Url(String),
26 Json(serde_json::Value),
28}
29
30impl fmt::Debug for ValuesFile {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 match self {
33 Self::Path(path) => f.debug_tuple("Path").field(path).finish(),
34 Self::Url(url) => f.debug_tuple("Url").field(url).finish(),
35 Self::Json(_) => f.write_str("Json(<redacted>)"),
36 }
37 }
38}
39
40#[derive(Debug, Clone, Default, PartialEq)]
42pub struct Values {
43 pub files: Vec<ValuesFile>,
45 pub set_json: Vec<String>,
47 pub set: Vec<String>,
49 pub set_string: Vec<String>,
51 pub set_file: Vec<String>,
53 pub set_literal: Vec<String>,
55}
56
57impl Values {
58 #[must_use]
60 pub fn new() -> Self {
61 Self::default()
62 }
63
64 #[must_use]
66 pub fn is_empty(&self) -> bool {
67 self == &Self::default()
68 }
69
70 #[must_use]
72 pub fn file(mut self, path: impl AsRef<Path>) -> Self {
73 self.files.push(ValuesFile::Path(path.as_ref().to_owned()));
74
75 self
76 }
77
78 #[must_use]
80 pub fn url(mut self, url: impl Into<String>) -> Self {
81 self.files.push(ValuesFile::Url(url.into()));
82
83 self
84 }
85
86 #[must_use]
88 pub fn json(mut self, value: serde_json::Value) -> Self {
89 self.files.push(ValuesFile::Json(value));
90
91 self
92 }
93
94 pub fn serialize(self, value: &impl Serialize) -> Result<Self> {
96 Ok(self.json(serde_json::to_value(value)?))
97 }
98
99 #[must_use]
101 pub fn set(mut self, key: impl Display, value: impl Display) -> Self {
102 self.set.push(format!("{key}={value}"));
103
104 self
105 }
106
107 #[must_use]
109 pub fn set_string(mut self, key: impl Display, value: impl Display) -> Self {
110 self.set_string.push(format!("{key}={value}"));
111
112 self
113 }
114
115 #[must_use]
117 pub fn set_file(mut self, key: impl Display, path: impl AsRef<Path>) -> Self {
118 self.set_file
119 .push(format!("{key}={}", path.as_ref().display()));
120
121 self
122 }
123
124 #[must_use]
126 pub fn set_json(mut self, key: impl Display, value: &serde_json::Value) -> Self {
127 self.set_json.push(format!("{key}={value}"));
128
129 self
130 }
131
132 #[must_use]
134 pub fn set_literal(mut self, key: impl Display, value: impl Display) -> Self {
135 self.set_literal.push(format!("{key}={value}"));
136
137 self
138 }
139}
140
141#[cfg(test)]
142mod tests;