use cimmodel::base::ClassDef;
use cimmodel::registry::type_registry;
use quick_xml::Reader;
use quick_xml::events::Event;
use crate::mrid_of;
pub use crate::text::{LineIndex, Position, Range, contains};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IdKind {
Id,
About,
}
#[derive(Debug)]
pub struct Element {
pub qname: String,
pub mrid: String,
pub id_kind: Option<IdKind>,
pub head: Range,
pub id_range: Option<Range>,
pub full: Range,
pub fields: Vec<Field>,
pub name: Option<String>,
}
#[derive(Debug)]
pub struct Field {
pub qname: String,
pub head: Range,
pub full: Range,
pub resource: Option<(String, Range)>,
}
impl Field {
pub fn key(&self) -> &str {
local(&self.qname)
}
}
#[derive(Debug, Default)]
pub struct Index {
pub namespaces: Vec<(String, String)>,
pub elements: Vec<Element>,
}
pub fn local(qname: &str) -> &str {
qname.rsplit_once(':').map_or(qname, |(_, l)| l)
}
pub fn prefix(qname: &str) -> &str {
qname.split_once(':').map_or("", |(p, _)| p)
}
impl Index {
pub fn build(text: &str) -> Self {
let lines = LineIndex::new(text);
let mut ix = Index::default();
let mut reader = Reader::from_str(text);
reader.config_mut().check_end_names = false;
let mut depth = 0u32;
let mut current: Option<Element> = None;
let mut field: Option<Field> = None;
let mut name_text: Option<String> = None;
loop {
let start = reader.buffer_position() as usize;
let event = match reader.read_event() {
Ok(Event::Eof) | Err(_) => break,
Ok(e) => e,
};
let end = reader.buffer_position() as usize;
let range = || lines.range(text, start, end);
let empty = matches!(event, Event::Empty(_));
match event {
Event::Start(e) | Event::Empty(e) if depth == 0 => {
for a in e.attributes().flatten() {
let key = String::from_utf8_lossy(a.key.as_ref());
let prefix = if key == "xmlns" {
Some("")
} else {
key.strip_prefix("xmlns:")
};
if let Some(p) = prefix {
ix.namespaces.push((
p.to_string(),
String::from_utf8_lossy(&a.value).into_owned(),
));
}
}
if !empty {
depth = 1;
}
}
Event::Start(e) | Event::Empty(e) if depth == 1 => {
let tag = &text[start..end];
let (id_kind, id_range, mrid) = match attr_value(tag, "rdf:ID")
.map(|s| (IdKind::Id, s))
.or_else(|| attr_value(tag, "rdf:about").map(|s| (IdKind::About, s)))
{
Some((kind, (vs, ve))) => (
Some(kind),
Some(lines.range(text, start + vs, start + ve)),
mrid_of(&tag[vs..ve]).to_string(),
),
None => (None, None, String::new()),
};
let el = Element {
qname: String::from_utf8_lossy(e.name().as_ref()).into_owned(),
mrid,
id_kind,
head: range(),
id_range,
full: range(),
fields: Vec::new(),
name: None,
};
if !empty {
current = Some(el);
depth = 2;
} else {
ix.elements.push(el);
}
}
Event::Start(e) | Event::Empty(e) if depth == 2 => {
let tag = &text[start..end];
let resource = attr_value(tag, "rdf:resource").map(|(vs, ve)| {
(
mrid_of(&tag[vs..ve]).to_string(),
lines.range(text, start + vs, start + ve),
)
});
let f = Field {
qname: String::from_utf8_lossy(e.name().as_ref()).into_owned(),
head: range(),
full: range(),
resource,
};
if let Some(el) = current.as_mut() {
if !empty {
name_text = (f.key() == "IdentifiedObject.name").then(String::new);
field = Some(f);
} else {
el.fields.push(f);
}
}
if !empty {
depth = 3;
}
}
Event::Start(_) => depth += 1,
Event::Text(t) if depth == 3 => {
if let Some(name) = name_text.as_mut() {
name.push_str(&t.unescape().unwrap_or_default());
}
}
Event::CData(t) if depth == 3 => {
if let Some(name) = name_text.as_mut() {
name.push_str(&String::from_utf8_lossy(&t));
}
}
Event::End(_) => {
depth = depth.saturating_sub(1);
match depth {
2 => {
if let (Some(el), Some(mut f)) = (current.as_mut(), field.take()) {
f.full.end = range().end;
if let Some(name) = name_text.take() {
el.name = Some(name.trim().to_string());
}
if !f.qname.is_empty() {
el.fields.push(f);
}
}
}
1 => {
if let Some(mut el) = current.take() {
el.full.end = range().end;
ix.elements.push(el);
}
}
_ => {}
}
}
_ => {}
}
}
if let Some(mut el) = current {
el.full.end = lines.position(text, text.len());
if let Some(f) = field.filter(|f| !f.qname.is_empty()) {
el.fields.push(f);
}
ix.elements.push(el);
}
ix
}
pub fn namespace(&self, prefix: &str) -> Option<&str> {
self.namespaces
.iter()
.find(|(p, _)| p == prefix)
.map(|(_, ns)| ns.as_str())
}
pub fn prefix_of(&self, ns: &str) -> Option<&str> {
self.namespaces
.iter()
.find(|(_, n)| n == ns)
.map(|(p, _)| p.as_str())
}
pub fn class_of(&self, qname: &str) -> Option<&'static ClassDef> {
let reg = type_registry();
self.namespace(prefix(qname))
.and_then(|ns| reg.ns_table(ns))
.and_then(|t| t.get(local(qname)).copied())
.or_else(|| reg.bare().get(local(qname)).copied())
}
pub fn element_at(&self, pos: Position) -> Option<&Element> {
let i = self.elements.partition_point(|e| e.full.end < pos);
self.elements.get(i).filter(|e| contains(e.full, pos))
}
}
fn attr_value(tag: &str, name: &str) -> Option<(usize, usize)> {
let bytes = tag.as_bytes();
let mut from = 0;
while let Some(i) = tag[from..].find(name).map(|i| i + from) {
from = i + name.len();
if i == 0 || !bytes[i - 1].is_ascii_whitespace() {
continue;
}
let rest = tag[from..].trim_start();
let Some(rest) = rest.strip_prefix('=') else {
continue;
};
let rest = rest.trim_start();
let quote = rest.chars().next()?;
if quote != '"' && quote != '\'' {
continue;
}
let vs = tag.len() - rest.len() + 1;
let ve = vs + tag[vs..].find(quote)?;
return Some((vs, ve));
}
None
}
#[cfg(test)]
mod tests {
use super::*;
const DOC: &str = r##"<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:cim="http://iec.ch/TC57/CIM100#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<cim:ACLineSegment rdf:ID="_line">
<cim:IdentifiedObject.name>Line & 1</cim:IdentifiedObject.name>
<cim:ACLineSegment.r>0.5</cim:ACLineSegment.r>
<cim:ConductingEquipment.BaseVoltage rdf:resource="#_bv"/>
</cim:ACLineSegment>
<cim:BaseVoltage
rdf:about="urn:uuid:x#_bv" />
</rdf:RDF>
"##;
fn pos(line: u32, character: u32) -> Position {
Position { line, character }
}
#[test]
fn indexes_elements_fields_and_references() {
let ix = Index::build(DOC);
assert_eq!(ix.namespace("cim"), Some("http://iec.ch/TC57/CIM100#"));
assert_eq!(ix.elements.len(), 2);
let line = &ix.elements[0];
assert_eq!(
(line.qname.as_str(), line.mrid.as_str(), line.id_kind),
("cim:ACLineSegment", "_line", Some(IdKind::Id))
);
assert_eq!(
line.id_range,
Some(Range {
start: pos(2, 29),
end: pos(2, 34)
})
);
assert_eq!(line.full.start, pos(2, 2));
assert_eq!(line.full.end, pos(6, 22));
assert_eq!(line.name.as_deref(), Some("Line & 1"));
let keys: Vec<&str> = line.fields.iter().map(Field::key).collect();
assert_eq!(
keys,
[
"IdentifiedObject.name",
"ACLineSegment.r",
"ConductingEquipment.BaseVoltage"
]
);
assert_eq!(
line.fields[1].full,
Range {
start: pos(4, 4),
end: pos(4, 50)
}
);
let (target, at) = line.fields[2].resource.clone().unwrap();
assert_eq!(target, "_bv");
assert_eq!(
at,
Range {
start: pos(5, 55),
end: pos(5, 59)
}
);
let bv = &ix.elements[1];
assert_eq!((bv.mrid.as_str(), bv.id_kind), ("_bv", Some(IdKind::About)));
assert_eq!(
bv.head,
Range {
start: pos(7, 2),
end: pos(8, 35)
}
);
assert_eq!(
ix.element_at(pos(4, 10)).map(|e| e.mrid.as_str()),
Some("_line")
);
assert!(ix.element_at(pos(1, 3)).is_none());
}
#[test]
fn a_truncated_document_keeps_what_it_has() {
let cut = &DOC[..DOC.find("<cim:ACLineSegment.r>").unwrap() + 25];
let ix = Index::build(cut);
assert_eq!(ix.elements.len(), 1);
assert_eq!(ix.elements[0].mrid, "_line");
assert_eq!(
ix.elements[0].full.end,
LineIndex::new(cut).position(cut, cut.len())
);
}
}