GJVR74FXYXUF6APQ2RJJ27BE5ZG6JCN6OREHHPCV3TP6ZMNNVCAAC
use std::collections::HashMap;
use serde::Deserialize;
use serde_yaml::Value;
use crate::json_pointer;
#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct Transformation {
pub filter: Option<Filter>,
pub add: Option<TransAdd>,
pub replace: Option<TransReplace>,
pub remove: Option<TransRemove>,
pub prefix: Option<TransPrefix>,
pub suffix: Option<TransSuffix>,
}
pub type Filter = HashMap<String, Value>;
pub type TransAdd = HashMap<String, Value>;
pub type TransReplace = HashMap<String, Value>;
pub type TransRemove = HashMap<String, ()>;
pub type TransPrefix = HashMap<String, String>;
pub type TransSuffix = HashMap<String, String>;
pub fn t_add(result: &mut Value, t: Option<&TransAdd>) {}
pub fn t_replace(result: &mut Value, t: Option<&TransReplace>) {}
pub fn t_prefix(result: &mut Value, t: Option<&TransPrefix>) {}
pub fn t_suffix(result: &mut Value, t: Option<&TransSuffix>) {}
pub fn matches_filter(doc: &Value, filter: Option<&Filter>) -> bool {
if let Some(f) = filter {
// Check each field from the filter and return
// false as soon as one doesn't match
for (field, value) in f.iter() {
if json_pointer::get(doc, field) != Some(value) {
return false;
}
}
}
// If all fields match or if no fields were provided
// keep this transformation in the list to apply.
true
}
}
impl Karbonfile {
pub fn from_value(url: &Url, doc: &Value) -> Result<Karbonfile, serde_yaml::Error> {
let mut v = doc.to_owned();
println!("{:#?}", &v);
v["fileUrl"] = serde_yaml::from_str(url.as_str()).unwrap();
serde_yaml::from_value::<Karbonfile>(v)
}
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct RawKarbonfile {
file_url: Url,
api_version: String,
kind: String,
#[serde(default)]
resources: HashMap<String, ()>,
#[serde(default)]
generators: Vec<Generator>,
#[serde(default)]
transformations: Vec<Transformation>,
}
impl TryFrom<RawKarbonfile> for Karbonfile {
type Error = &'static str;
fn try_from(t: RawKarbonfile) -> Result<Self, &'static str> {
let r = t
.resources
.into_iter()
.map(|(k, v)| {
if let Ok(url) = Url::parse(&k) {
return url;
} else {
let path = Path::new(&k);
let base = t.file_url.to_file_path().unwrap();
let canonical = base.join(path).canonicalize().unwrap();
if canonical.is_file() {
return Url::from_file_path(canonical).unwrap();
} else {
println!("{:#?}", &path);
return Url::from_directory_path(canonical).unwrap();
}
}
})
.collect();
Ok(Self {
api_version: t.api_version,
kind: t.kind,
resources: r,
generators: t.generators,
transformations: t.transformations,
})
}
#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct Transformation {
filter: Option<Filter>,
add: Option<TransAdd>,
replace: Option<TransReplace>,
remove: Option<TransRemove>,
prefix: Option<TransPrefix>,
suffix: Option<TransSuffix>,
}
type Filter = HashMap<String, Value>;
type TransAdd = HashMap<String, Value>;
type TransReplace = HashMap<String, Value>;
type TransRemove = HashMap<String, ()>;
type TransPrefix = HashMap<String, String>;
type TransSuffix = HashMap<String, String>;
fn matches_filter(doc: &Value, filter: Option<&Filter>) -> bool {
if let Some(f) = filter {
// Check each field from the filter and return
// false as soon as one doesn't match
for (field, value) in f.iter() {
if json_pointer::get(doc, field) != Some(value) {
return false;
}
}
}
// If all fields match or if no fields were provided
// keep this transformation in the list to apply.
true
}
fn t_add(result: &mut Value, t: Option<&TransAdd>) {}
fn t_replace(result: &mut Value, t: Option<&TransReplace>) {}
fn t_prefix(result: &mut Value, t: Option<&TransPrefix>) {}
fn t_suffix(result: &mut Value, t: Option<&TransSuffix>) {}