use svd_rs as svd;
use crate::svd::Device;
use xmltree::{Element, EmitterConfig, XMLNode};
pub use crate::config::{
Config, DerivableSorting, IdentifierFormat, NumberFormat, RcSorting, Sorting,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
pub enum EncodeError {}
pub trait Encode {
type Error;
fn encode(&self) -> Result<Element, Self::Error> {
self.encode_with_config(&Config::default())
}
fn encode_with_config(&self, config: &Config) -> Result<Element, Self::Error>;
fn encode_node(&self) -> Result<XMLNode, Self::Error> {
self.encode().map(XMLNode::Element)
}
fn encode_node_with_config(&self, config: &Config) -> Result<XMLNode, Self::Error> {
self.encode_with_config(config).map(XMLNode::Element)
}
}
pub trait EncodeChildren {
type Error;
fn encode(&self) -> Result<Vec<XMLNode>, Self::Error> {
self.encode_with_config(&Config::default())
}
fn encode_with_config(&self, config: &Config) -> Result<Vec<XMLNode>, Self::Error>;
}
pub fn encode(d: &Device) -> Result<String, EncodeError> {
encode_with_config(d, &Config::default())
}
pub fn encode_with_config(d: &Device, config: &Config) -> Result<String, EncodeError> {
let root = d.encode_with_config(config)?;
let mut wr = Vec::new();
let mut cfg = EmitterConfig::new();
cfg.perform_indent = true;
cfg.pad_self_closing = false;
root.write_with_config(&mut wr, cfg).unwrap();
Ok(String::from_utf8(wr).unwrap())
}
trait ElementMerge {
fn merge(&mut self, n: &Self);
}
impl ElementMerge for Element {
fn merge(&mut self, r: &Self) {
self.children.extend(r.children.iter().cloned());
for (key, val) in &r.attributes {
self.attributes.insert(key.clone(), val.clone());
}
}
}
pub(crate) fn new_node(name: &str, text: String) -> XMLNode {
let mut e = Element::new(name);
e.children.push(XMLNode::Text(text));
XMLNode::Element(e)
}
mod access;
mod addressblock;
mod bitrange;
mod cluster;
mod config;
mod cpu;
mod device;
mod dimelement;
mod endian;
mod enumeratedvalue;
mod enumeratedvalues;
mod field;
mod interrupt;
mod modifiedwritevalues;
mod peripheral;
mod protection;
mod readaction;
mod register;
mod registercluster;
mod registerproperties;
mod usage;
mod writeconstraint;