refactor: type
This commit is contained in:
65
src/ty/internal/attrset.rs
Normal file
65
src/ty/internal/attrset.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use derive_more::Constructor;
|
||||
use rpds::HashTrieMapSync;
|
||||
|
||||
use super::super::public as p;
|
||||
use super::super::common::Symbol;
|
||||
|
||||
use crate::vm::VM;
|
||||
use super::{ToPublic, Value};
|
||||
|
||||
#[derive(Debug, Constructor, Clone, PartialEq)]
|
||||
pub struct AttrSet {
|
||||
data: HashTrieMapSync<Symbol, Value>,
|
||||
}
|
||||
|
||||
impl AttrSet {
|
||||
pub fn empty() -> AttrSet {
|
||||
AttrSet { data: HashTrieMapSync::new_sync() }
|
||||
}
|
||||
|
||||
pub fn push_attr_force(&mut self, sym: Symbol, val: Value) {
|
||||
self.data.insert_mut(sym, val);
|
||||
}
|
||||
|
||||
pub fn push_attr(&mut self, sym: Symbol, val: Value) {
|
||||
if self.data.get(&sym).is_some() {
|
||||
todo!()
|
||||
}
|
||||
self.data.insert_mut(sym, val);
|
||||
}
|
||||
|
||||
pub fn select(&self, sym: Symbol) -> Option<Value> {
|
||||
self.data.get(&sym).cloned()
|
||||
}
|
||||
|
||||
pub fn has_attr(&self, sym: Symbol) -> bool {
|
||||
self.data.get(&sym).is_some()
|
||||
}
|
||||
|
||||
pub fn update(mut self, other: AttrSet) -> AttrSet {
|
||||
for (k, v) in other.data.iter() {
|
||||
self.push_attr_force(k.clone(), v.clone())
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn to_data(self) -> HashTrieMapSync<Symbol, Value> {
|
||||
self.data
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPublic for AttrSet {
|
||||
fn to_public(self, vm: &VM) -> p::Value {
|
||||
p::Value::AttrSet(p::AttrSet::new(
|
||||
self.data
|
||||
.iter()
|
||||
.map(|(sym, value)| {
|
||||
(
|
||||
sym.clone(),
|
||||
value.clone().to_public(vm),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user