chore: moved formatters into ext mod

This commit is contained in:
əlemi 2024-10-19 21:53:40 +02:00
parent 6211a1630e
commit 58900630e1
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 26 additions and 25 deletions

24
src/ext.rs Normal file
View file

@ -0,0 +1,24 @@
pub fn stringify_toml(v: &toml::Value) -> String {
match v {
toml::Value::Boolean(x) => x.to_string(),
toml::Value::Integer(x) => x.to_string(),
toml::Value::Float(x) => x.to_string(),
toml::Value::String(x) => x.clone(),
toml::Value::Datetime(x) => x.to_string(),
toml::Value::Array(x) => serde_json::to_string(&x).unwrap_or_default(),
toml::Value::Table(x) => serde_json::to_string(&x).unwrap_or_default(),
}
}
pub fn stringify_json(v: &serde_json::Value) -> String {
match v {
serde_json::Value::Null => "null".to_string(),
serde_json::Value::Bool(x) => x.to_string(),
serde_json::Value::Number(x) => x.to_string(),
serde_json::Value::String(x) => x.clone(),
serde_json::Value::Array(x) => serde_json::to_string(&x).unwrap_or_default(),
serde_json::Value::Object(x) => serde_json::to_string(&x).unwrap_or_default(),
}
}

View file

@ -50,29 +50,6 @@ fn replace_recursive(element: toml::Value, from: &str, to: &str) -> toml::Value
}
}
fn stringify_toml(v: &toml::Value) -> String {
match v {
toml::Value::Boolean(x) => x.to_string(),
toml::Value::Integer(x) => x.to_string(),
toml::Value::Float(x) => x.to_string(),
toml::Value::String(x) => x.clone(),
toml::Value::Datetime(x) => x.to_string(),
toml::Value::Array(x) => serde_json::to_string(&x).unwrap_or_default(),
toml::Value::Table(x) => serde_json::to_string(&x).unwrap_or_default(),
}
}
fn stringify_json(v: &serde_json::Value) -> String {
match v {
serde_json::Value::Null => "null".to_string(),
serde_json::Value::Bool(x) => x.to_string(),
serde_json::Value::Number(x) => x.to_string(),
serde_json::Value::String(x) => x.clone(),
serde_json::Value::Array(x) => serde_json::to_string(&x).unwrap_or_default(),
serde_json::Value::Object(x) => serde_json::to_string(&x).unwrap_or_default(),
}
}
impl Endpoint {
pub fn fill(mut self, env: &toml::Table) -> Self {
let mut vars: HashMap<String, String> = HashMap::default();
@ -80,7 +57,7 @@ impl Endpoint {
vars.insert("POSTWOMAN_TIMESTAMP".to_string(), chrono::Local::now().timestamp().to_string());
for (k, v) in env {
vars.insert(k.to_string(), stringify_toml(v));
vars.insert(k.to_string(), crate::ext::stringify_toml(v));
}
for (k, v) in std::env::vars() {
@ -190,7 +167,7 @@ impl Endpoint {
let json: serde_json::Value = res.json().await?;
let selection = jq(&query, json)?;
if selection.len() == 1 {
stringify_json(&selection[0]) + "\n"
crate::ext::stringify_json(&selection[0]) + "\n"
} else {
serde_json::to_string_pretty(&selection)? + "\n"
}