[niepce/lr-import: 9/20] npc-fwk: PropertyBag: derive clone, added get




commit 6f10676cbf2ccd97ed885ec9ed72af3e31d22303
Author: Hubert Figuière <hub figuiere net>
Date:   Tue Nov 16 23:48:45 2021 -0500

    npc-fwk: PropertyBag: derive clone, added get
    
    - Also add PropertyValue convert from i32 and &str

 crates/npc-fwk/src/base/propertybag.rs   | 10 ++++++++++
 crates/npc-fwk/src/base/propertyvalue.rs | 12 ++++++++++++
 2 files changed, 22 insertions(+)
---
diff --git a/crates/npc-fwk/src/base/propertybag.rs b/crates/npc-fwk/src/base/propertybag.rs
index e5ccf92..8fb97b8 100644
--- a/crates/npc-fwk/src/base/propertybag.rs
+++ b/crates/npc-fwk/src/base/propertybag.rs
@@ -21,6 +21,12 @@ use std::collections::BTreeMap;
 
 use crate::base::propertyvalue::PropertyValue;
 
+/// A container for type properties whose order of addition
+/// is kept
+///
+/// Insertion and lookup are same as for BTreeMap.
+/// Removal is as long as lookup in a vector: O(n).
+#[derive(Clone)]
 pub struct PropertyBag<Index> {
     pub bag: Vec<Index>,
     pub map: BTreeMap<Index, PropertyValue>,
@@ -48,6 +54,10 @@ impl<Index: Ord + Copy> PropertyBag<Index> {
         self.bag.len()
     }
 
+    pub fn get(&self, key: &Index) -> Option<&PropertyValue> {
+        self.map.get(key)
+    }
+
     pub fn set_value(&mut self, key: Index, value: PropertyValue) -> bool {
         let ret = self.map.insert(key, value);
         if ret.is_some() {
diff --git a/crates/npc-fwk/src/base/propertyvalue.rs b/crates/npc-fwk/src/base/propertyvalue.rs
index 513500f..881ff35 100644
--- a/crates/npc-fwk/src/base/propertyvalue.rs
+++ b/crates/npc-fwk/src/base/propertyvalue.rs
@@ -31,6 +31,18 @@ pub enum PropertyValue {
     Date(Date),
 }
 
+impl From<i32> for PropertyValue {
+    fn from(value: i32) -> PropertyValue {
+        Self::Int(value)
+    }
+}
+
+impl From<&str> for PropertyValue {
+    fn from(value: &str) -> PropertyValue {
+        Self::String(value.into())
+    }
+}
+
 unsafe impl Send for PropertyValue {}
 
 /// Create a new String %PropertyValue from a C string


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]