use crate::registry_core::declaration::SourceLocation;
use crate::registry_core::diagnostic::{RegistryError, RegistryResult};
use crate::registry_core::identity::NodeId;
use crate::registry_core::plugin::graft::document::GraftPlanDocument;
use super::super::Registry;
use super::RecordedGraft;
use super::reports::RecordReport;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ResolvedRecord {
Slot {
slot: NodeId,
path: String,
full: bool,
reports: Vec<RecordReport>,
},
Unkept {
reports: Vec<RecordReport>,
},
}
impl ResolvedRecord {
pub fn reports(&self) -> &[RecordReport] {
match self {
Self::Slot { reports, .. } | Self::Unkept { reports } => reports,
}
}
pub fn into_slot(self) -> Option<(NodeId, String, bool)> {
match self {
Self::Slot {
slot, path, full, ..
} => Some((slot, path, full)),
Self::Unkept { .. } => None,
}
}
}
impl Registry {
pub fn resolve_record(&self, record: &RecordedGraft) -> RegistryResult<ResolvedRecord> {
let document = &record.document;
let mut reports = Vec::new();
if record.selector != document.graft {
return Err(Box::new(RegistryError::new(
document.target,
self.current_path(document.target, document),
SourceLocation {
file: "<graft>",
line: 0,
column: 0,
function: "Registry::resolve_record",
},
format!(
"graft record directory `{}` and its plan's `graft={}` disagree; rename the directory or the file so the selector and the implementation name agree",
record.selector, document.graft
),
)));
}
let by_id = self.find_registry(document.target).map(|info| info.id);
let by_path = self.resolve_path(&document.target_path);
match (by_id, by_path) {
(Some(a), Some(b)) if a == b => Ok(ResolvedRecord::Slot {
slot: a,
path: self.current_path(a, document),
full: document.full,
reports,
}),
(Some(a), None) => {
reports.push(RecordReport::IdentityDrifted {
selector: record.selector.clone(),
target: document.target,
target_path: document.target_path.clone(),
});
Ok(ResolvedRecord::Slot {
slot: a,
path: self.current_path(a, document),
full: document.full,
reports,
})
}
(None, Some(b)) => {
reports.push(RecordReport::IdentityDrifted {
selector: record.selector.clone(),
target: document.target,
target_path: document.target_path.clone(),
});
Ok(ResolvedRecord::Slot {
slot: b,
path: self.current_path(b, document),
full: document.full,
reports,
})
}
(Some(a), Some(b)) => {
let id_path = self.current_path(a, document);
let path_path = self.current_path(b, document);
Err(Box::new(RegistryError::new(
document.target,
id_path.clone(),
SourceLocation {
file: "<graft>",
line: 0,
column: 0,
function: "Registry::resolve_record",
},
format!(
"graft record `{}` is contradictory: target {} names `{}`, but target_path `{}` names `{}`",
record.selector, document.target, id_path, document.target_path, path_path
),
)))
}
(None, None) => {
reports.push(RecordReport::UnkeptSlot {
selector: record.selector.clone(),
target_path: document.target_path.clone(),
});
Ok(ResolvedRecord::Unkept { reports })
}
}
}
fn current_path(&self, slot: NodeId, document: &GraftPlanDocument) -> String {
self.path_for(slot)
.unwrap_or_else(|| document.target_path.clone())
}
}