From 2aed203bebf4ed2fe5f96cd25a14a20c6743dd24 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Tue, 19 Mar 2024 16:30:37 +0300 Subject: [PATCH] use Arc instead of String in pathes --- svd-parser/src/expand.rs | 70 +++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/svd-parser/src/expand.rs b/svd-parser/src/expand.rs index f776f473..9f428676 100644 --- a/svd-parser/src/expand.rs +++ b/svd-parser/src/expand.rs @@ -1,9 +1,7 @@ //! Provides [expand] method to convert arrays, clusters and derived items in regular instances use anyhow::{anyhow, Result}; -use std::collections::HashMap; -use std::fmt; -use std::mem::take; +use std::{collections::HashMap, fmt, mem::take, rc::Rc}; use svd_rs::{ array::names, cluster, field, peripheral, register, Cluster, ClusterInfo, DeriveFrom, Device, EnumeratedValues, Field, Peripheral, Register, RegisterCluster, RegisterProperties, @@ -12,23 +10,23 @@ use svd_rs::{ /// Path to `peripheral` or `cluster` element #[derive(Clone, Debug, PartialEq, Hash, Eq)] pub struct BlockPath { - pub peripheral: String, - pub path: Vec, + pub peripheral: Rc, + pub path: Vec>, } impl BlockPath { - pub fn new(p: impl Into) -> Self { + pub fn new(p: impl Into>) -> Self { Self { peripheral: p.into(), path: Vec::new(), } } - pub fn new_cluster(&self, name: impl Into) -> Self { + pub fn new_cluster(&self, name: impl Into>) -> Self { let mut child = self.clone(); child.path.push(name.into()); child } - pub fn new_register(&self, name: impl Into) -> RegisterPath { + pub fn new_register(&self, name: impl Into>) -> RegisterPath { RegisterPath::new(self.clone(), name) } pub fn parse_str(s: &str) -> (Option, &str) { @@ -46,7 +44,7 @@ impl BlockPath { }; (block, name) } - pub fn name(&self) -> &String { + pub fn name(&self) -> &str { self.path.last().unwrap() } pub fn parent(&self) -> Option { @@ -63,11 +61,11 @@ impl PartialEq for BlockPath { } let mut parts = other.split('.'); if let Some(part1) = parts.next() { - if self.peripheral != part1 { + if self.peripheral.as_ref() != part1 { return false; } for p in parts.zip(self.path.iter()) { - if p.0 != p.1 { + if p.0 != p.1.as_ref() { return false; } } @@ -93,17 +91,17 @@ impl fmt::Display for BlockPath { #[derive(Clone, Debug, PartialEq, Hash, Eq)] pub struct RegisterPath { pub block: BlockPath, - pub name: String, + pub name: Rc, } impl RegisterPath { - pub fn new(block: BlockPath, name: impl Into) -> Self { + pub fn new(block: BlockPath, name: impl Into>) -> Self { Self { block, name: name.into(), } } - pub fn new_field(&self, name: impl Into) -> FieldPath { + pub fn new_field(&self, name: impl Into>) -> FieldPath { FieldPath::new(self.clone(), name) } pub fn parse_str(s: &str) -> (Option, &str) { @@ -112,7 +110,7 @@ impl RegisterPath { pub fn parse_vec(v: Vec<&str>) -> (Option, &str) { BlockPath::parse_vec(v) } - pub fn peripheral(&self) -> &String { + pub fn peripheral(&self) -> &str { &self.block.peripheral } } @@ -120,7 +118,7 @@ impl RegisterPath { impl PartialEq for RegisterPath { fn eq(&self, other: &str) -> bool { if let Some((block, reg)) = other.rsplit_once('.') { - self.name == reg && &self.block == block + self.name.as_ref() == reg && &self.block == block } else { false } @@ -140,17 +138,17 @@ impl fmt::Display for RegisterPath { #[derive(Clone, Debug, PartialEq, Hash, Eq)] pub struct FieldPath { pub register: RegisterPath, - pub name: String, + pub name: Rc, } impl FieldPath { - pub fn new(register: RegisterPath, name: impl Into) -> Self { + pub fn new(register: RegisterPath, name: impl Into>) -> Self { Self { register, name: name.into(), } } - pub fn new_enum(&self, name: impl Into) -> EnumPath { + pub fn new_enum(&self, name: impl Into>) -> EnumPath { EnumPath::new(self.clone(), name) } pub fn parse_str(s: &str) -> (Option, &str) { @@ -172,7 +170,7 @@ impl FieldPath { pub fn register(&self) -> &RegisterPath { &self.register } - pub fn peripheral(&self) -> &String { + pub fn peripheral(&self) -> &str { self.register.peripheral() } } @@ -180,7 +178,7 @@ impl FieldPath { impl PartialEq for FieldPath { fn eq(&self, other: &str) -> bool { if let Some((reg, field)) = other.rsplit_once('.') { - self.name == field && &self.register == reg + self.name.as_ref() == field && &self.register == reg } else { false } @@ -200,11 +198,11 @@ impl fmt::Display for FieldPath { #[derive(Clone, Debug, PartialEq, Hash, Eq)] pub struct EnumPath { pub field: FieldPath, - pub name: String, + pub name: Rc, } impl EnumPath { - pub fn new(field: FieldPath, name: impl Into) -> Self { + pub fn new(field: FieldPath, name: impl Into>) -> Self { Self { field, name: name.into(), @@ -216,7 +214,7 @@ impl EnumPath { pub fn register(&self) -> &RegisterPath { &self.field.register } - pub fn peripheral(&self) -> &String { + pub fn peripheral(&self) -> &str { self.field.peripheral() } } @@ -224,7 +222,7 @@ impl EnumPath { impl PartialEq for EnumPath { fn eq(&self, other: &str) -> bool { if let Some((field, evs)) = other.rsplit_once('.') { - self.name == evs && &self.field == field + self.name.as_ref() == evs && &self.field == field } else { false } @@ -263,7 +261,7 @@ impl<'a> Index<'a> { self.peripherals.insert(path, p); } } - let path = BlockPath::new(&p.name); + let path = BlockPath::new(p.name.as_ref()); for r in p.registers() { self.add_register(&path, r); } @@ -286,7 +284,7 @@ impl<'a> Index<'a> { self.clusters.insert(cpath, c); } } - let cpath = path.new_cluster(&c.name); + let cpath = path.new_cluster(c.name.as_ref()); for r in c.registers() { self.add_register(&cpath, r); } @@ -305,7 +303,7 @@ impl<'a> Index<'a> { self.registers.insert(rpath, r); } } - let rpath = path.new_register(&r.name); + let rpath = path.new_register(r.name.as_ref()); for f in r.fields() { self.add_field(&rpath, f); } @@ -317,16 +315,16 @@ impl<'a> Index<'a> { let fpath = path.new_field(name); for evs in &f.enumerated_values { if let Some(name) = evs.name.as_ref() { - self.evs.insert(fpath.new_enum(name), evs); + self.evs.insert(fpath.new_enum(name.as_ref()), evs); } } self.fields.insert(fpath, f); } } - let fpath = path.new_field(&f.name); + let fpath = path.new_field(f.name.as_ref()); for evs in &f.enumerated_values { if let Some(name) = evs.name.as_ref() { - self.evs.insert(fpath.new_enum(name), evs); + self.evs.insert(fpath.new_enum(name.as_ref()), evs); } } self.fields.insert(fpath, f); @@ -365,7 +363,7 @@ fn expand_cluster_array( if let Some(dpath) = dpath { cpath = derive_cluster(&mut c, &dpath, path, index)?; } - let cpath = cpath.unwrap_or_else(|| path.new_cluster(&c.name)); + let cpath = cpath.unwrap_or_else(|| path.new_cluster(c.name.as_ref())); for rc in take(&mut c.children) { expand_register_cluster(&mut c.children, rc, &cpath, index)?; @@ -499,7 +497,7 @@ fn expand_register_array( if let Some(dpath) = dpath { rpath = derive_register(&mut r, &dpath, path, index)?; } - let rpath = rpath.unwrap_or_else(|| path.new_register(&r.name)); + let rpath = rpath.unwrap_or_else(|| path.new_register(r.name.as_ref())); if let Some(field) = r.fields.as_mut() { for f in take(field) { @@ -529,7 +527,7 @@ fn expand_field( if let Some(dpath) = dpath { fpath = derive_field(&mut f, &dpath, rpath, index)?; } - let fpath = fpath.unwrap_or_else(|| rpath.new_field(&f.name)); + let fpath = fpath.unwrap_or_else(|| rpath.new_field(f.name.as_ref())); for ev in &mut f.enumerated_values { let dpath = ev.derived_from.take(); @@ -564,7 +562,7 @@ pub fn derive_enumerated_values( if let Some(r) = index.registers.get(rdpath) { let mut found = None; for f in r.fields() { - let epath = EnumPath::new(rdpath.new_field(&f.name), dname); + let epath = EnumPath::new(rdpath.new_field(f.name.as_ref()), dname); if let Some(d) = index.evs.get(&epath) { found = Some((d, epath)); break; @@ -645,7 +643,7 @@ pub fn expand(indevice: &Device) -> Result { if let Some(dpath) = dpath { path = derive_peripheral(&mut p, &dpath, &index)?; } - let path = path.unwrap_or_else(|| BlockPath::new(&p.name)); + let path = path.unwrap_or_else(|| BlockPath::new(p.name.as_ref())); if let Some(regs) = p.registers.as_mut() { for rc in take(regs) { expand_register_cluster(regs, rc, &path, &index)?;