use std::collections::BTreeMap;
use crate::registry_core::declaration::{
OwnedFlowContract, OwnedLocalizedText, OwnedObjectContract, OwnedSourceLocation,
RegistrationSnapshot, RuntimeCheckSpec,
};
use crate::registry_core::identity::NodeId;
use super::parse::{
parse_admission_snapshot, parse_field_list, parse_flow_value, parse_registration_rule_snapshot,
try_parse_requirements_owned,
};
use super::validation::rule_path_for_source;
pub fn snapshot_from_values(
values: &BTreeMap<String, String>,
default_namespace: &str,
) -> Result<RegistrationSnapshot, String> {
let value = |key| values.get(key).map(String::as_str).unwrap_or("");
let source = value("source").to_owned();
let kind = value("kind").to_owned();
let declaration_line = value("declaration_line").parse().unwrap_or(1);
let parent = value("parent_node")
.parse::<NodeId>()
.map_err(|_| "generated face has an invalid parent node identity".to_owned())?;
let needs_registry = match value("needs_registry") {
"true" => true,
"false" => false,
_ => return Err("generated face has an invalid needs_registry value".to_owned()),
};
let registry_name = if value("registry_name").is_empty() {
value("module")
} else {
value("registry_name")
};
let preset = if value("preset").is_empty() {
"NoPreset"
} else {
value("preset")
};
let parts = if value("parts").is_empty() {
"NoParts"
} else {
value("parts")
};
let handle = if value("handle").is_empty() {
kind.clone()
} else {
value("handle").to_owned()
};
let name_zh = if value("name_zh").is_empty() {
kind.clone()
} else {
value("name_zh").to_owned()
};
let name_en = if value("name_en").is_empty() {
kind.clone()
} else {
value("name_en").to_owned()
};
let registry_rule_path = if value("registry_rule_path").is_empty() {
rule_path_for_source(&source)
} else {
value("registry_rule_path").to_owned()
};
let exports = parse_field_list(value("exports"));
let handle_traits = parse_field_list(value("handle_traits"));
let part_traits = parse_field_list(value("part_traits"));
let registration_rule = parse_registration_rule_snapshot(value("registration_rule"))?;
let admission = parse_admission_snapshot(value("admission"))?;
let namespace = if value("namespace").is_empty() {
default_namespace.to_owned()
} else {
value("namespace").to_owned()
};
Ok(RegistrationSnapshot {
namespace: namespace.clone(),
id: NodeId::from_namespaced_path(&namespace, &source, &kind),
parent,
kind: kind.clone(),
preset: preset.to_owned(),
parts: parts.to_owned(),
params: if value("params").is_empty() {
kind.clone()
} else {
value("params").to_owned()
},
handle: handle.clone(),
stable_name: (!value("stable_name").is_empty()).then_some(value("stable_name").to_owned()),
name: OwnedLocalizedText {
zh: name_zh,
en: name_en,
},
summary: OwnedLocalizedText {
zh: value("summary_zh").to_owned(),
en: value("summary_en").to_owned(),
},
exports,
needs_registry,
registry_name: registry_name.to_owned(),
getting_from_other_registry: (!value("getting_from_other_registry").is_empty())
.then_some(value("getting_from_other_registry").to_owned()),
registry_rule_path: registry_rule_path.to_owned(),
registry_rule: registration_rule,
admission,
requires: try_parse_requirements_owned(value("requires"))
.map_err(|error| error.to_string())?,
provides: parse_field_list(value("provides")),
contract: OwnedObjectContract {
required_parts: parse_field_list(value("required_parts")),
provided_parts: parse_field_list(value("provided_parts")),
},
flow: parse_flow_value(value("flow"))?.unwrap_or_else(OwnedFlowContract::none),
flow_provider: (!value("flow_provider").is_empty())
.then_some(value("flow_provider").to_owned()),
handle_traits,
part_traits,
runtime_checks: RuntimeCheckSpec::parse_list(value("runtime_checks"))?,
plugin: None,
source: OwnedSourceLocation {
file: source,
line: declaration_line,
column: 1,
function: handle,
},
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_handle_field_and_the_source_function_are_one_value() {
let cases = [
("an omitted handle", None, "Widget"),
("a declared handle", Some("CustomHandle"), "CustomHandle"),
];
for (label, declared, expected) in cases {
let mut values = BTreeMap::new();
for (key, value) in [
("source", "widget/widget.rs"),
("kind", "Widget"),
("namespace", "probe"),
("needs_registry", "false"),
("registration_rule", "ANY"),
("admission", "ANY"),
] {
values.insert(key.to_owned(), value.to_owned());
}
values.insert(
"parent_node".to_owned(),
NodeId::from_namespaced_path("probe", "root.rs", "root").to_string(),
);
if let Some(declared) = declared {
values.insert("handle".to_owned(), declared.to_owned());
}
let snapshot =
snapshot_from_values(&values, "probe").expect("a face with required keys converts");
assert_eq!(snapshot.handle, expected, "{label}: the handle field");
assert_eq!(
snapshot.source.function, expected,
"{label}: `source.function` is the derived handle, not a second reading of the key"
);
}
}
fn face_values(source: &str, kind: &str) -> BTreeMap<String, String> {
let mut values = BTreeMap::new();
for (key, value) in [
("source", source),
("kind", kind),
("namespace", "probe"),
("needs_registry", "false"),
("registration_rule", "ANY"),
("admission", "ANY"),
] {
values.insert(key.to_owned(), value.to_owned());
}
values.insert(
"parent_node".to_owned(),
NodeId::from_namespaced_path("probe", "root.rs", "root").to_string(),
);
values
}
#[test]
fn the_rule_path_field_names_the_location_the_rule_would_be_read_from() {
let values = face_values("control/object/slider/slider.rs", "Slider");
let snapshot = snapshot_from_values(&values, "probe").expect("a readable face");
assert_eq!(
snapshot.registry_rule_path,
rule_path_for_source("control/object/slider/slider.rs")
);
assert_eq!(
snapshot.registry_rule_path,
"src/control/object/slider/registry_rule/registry_rule.rs"
);
let again = snapshot_from_values(&values, "probe").expect("a readable face");
assert_eq!(again.registry_rule_path, snapshot.registry_rule_path);
let mut declared = values.clone();
declared.insert(
"registry_rule_path".to_owned(),
"src/elsewhere/registry_rule/registry_rule.rs".to_owned(),
);
let declared = snapshot_from_values(&declared, "probe").expect("a readable face");
assert_eq!(
declared.registry_rule_path,
"src/elsewhere/registry_rule/registry_rule.rs"
);
}
}