use super::*;
use crate::registry_core::lexicon::path_is_under;
impl fmt::Display for OwnedSourceLocation {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"{}:{}:{}",
portable_path(&self.file),
self.line,
self.column
)
}
}
impl OwnedSourceLocation {
pub fn portable_file(&self) -> String {
portable_path(&self.file)
}
pub fn describe(&self) -> String {
format!("{} function={}", self, self.function)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OwnedLocalizedText {
pub zh: String,
pub en: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OwnedRequirementSpec {
pub capability: String,
pub provider: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OwnedAdmission {
pub allowed_paths: Vec<String>,
pub denied_paths: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OwnedRegistrationRule {
pub required_preset: Option<String>,
pub required_parts: Vec<String>,
pub required_exports: Vec<String>,
pub required_handle_traits: Vec<String>,
pub required_part_traits: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OwnedObjectContract {
pub required_parts: Vec<String>,
pub provided_parts: Vec<String>,
}
impl OwnedAdmission {
pub fn accepts(&self, path: &str) -> bool {
if self
.denied_paths
.iter()
.any(|prefix| path_is_under(path, prefix))
{
return false;
}
self.allowed_paths.is_empty()
|| self
.allowed_paths
.iter()
.any(|prefix| path_is_under(path, prefix))
}
}
impl OwnedRegistrationRule {
pub fn validate(&self, snapshot: &RegistrationSnapshot) -> Vec<String> {
validate_registration_requirements(RegistrationRequirementCheck {
required_preset: self.required_preset.as_deref(),
required_parts: &self.required_parts,
required_exports: &self.required_exports,
required_handle_traits: &self.required_handle_traits,
required_part_traits: &self.required_part_traits,
preset: &snapshot.preset,
provided_parts: &snapshot.contract.provided_parts,
exports: &snapshot.exports,
handle: &snapshot.handle,
handle_traits: &snapshot.handle_traits,
parts: &snapshot.parts,
part_traits: &snapshot.part_traits,
})
}
}
impl OwnedObjectContract {
pub fn validate(&self, object: &str) -> Vec<String> {
validate_object_contract(&self.required_parts, &self.provided_parts, object)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OwnedSourceLocation {
pub file: String,
pub line: u32,
pub column: u32,
pub function: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RegistrationSnapshot {
pub namespace: String,
pub id: NodeId,
pub parent: NodeId,
pub kind: String,
pub preset: String,
pub parts: String,
pub params: String,
pub handle: String,
pub stable_name: Option<String>,
pub name: OwnedLocalizedText,
pub summary: OwnedLocalizedText,
pub exports: Vec<String>,
pub needs_registry: bool,
pub registry_name: String,
pub getting_from_other_registry: Option<String>,
pub registry_rule_path: String,
pub registry_rule: OwnedRegistrationRule,
pub admission: OwnedAdmission,
pub requires: Vec<OwnedRequirementSpec>,
pub provides: Vec<String>,
pub contract: OwnedObjectContract,
pub flow: crate::OwnedFlowContract,
pub flow_provider: Option<String>,
pub handle_traits: Vec<String>,
pub part_traits: Vec<String>,
pub runtime_checks: Vec<RuntimeCheckSpec>,
pub plugin: Option<crate::PluginManifest>,
pub source: OwnedSourceLocation,
}
impl RegistrationSnapshot {
pub fn stable_face_id(&self) -> StableFaceId {
StableFaceId::from_name(self.stable_name.as_deref().unwrap_or(&self.kind))
}
pub fn explicit_stable_face_id(&self) -> Option<StableFaceId> {
self.stable_name.as_deref().map(StableFaceId::from_name)
}
pub fn merge_authored(mut self, authored: Self) -> Self {
self.namespace = authored.namespace;
self.kind = authored.kind;
self.preset = authored.preset;
self.parts = authored.parts;
self.params = authored.params;
self.handle = authored.handle;
self.stable_name = authored.stable_name;
self.name = authored.name;
self.summary = authored.summary;
self.exports = authored.exports;
self.needs_registry = authored.needs_registry;
self.registry_name = authored.registry_name;
self.getting_from_other_registry = authored.getting_from_other_registry;
self.registry_rule_path = authored.registry_rule_path;
self.registry_rule = authored.registry_rule;
self.admission = authored.admission;
self.requires = authored.requires;
self.provides = authored.provides;
if authored.flow.is_declared() {
self.flow = authored.flow;
}
if authored.flow_provider.is_some() {
self.flow_provider = authored.flow_provider;
}
self.handle_traits = authored.handle_traits;
self.part_traits = authored.part_traits;
self.source = authored.source;
self
}
}