feat: a lot

This commit is contained in:
2025-08-06 18:30:19 +08:00
parent 32c602f21c
commit f946cb2fd1
22 changed files with 735 additions and 591 deletions

View File

@@ -1,6 +1,6 @@
use core::ops::Deref;
use std::rc::Rc;
use std::fmt::Debug;
use std::rc::Rc;
use derive_more::Constructor;
use hashbrown::{HashMap, HashSet};
@@ -15,11 +15,11 @@ use crate::EvalContext;
#[repr(transparent)]
#[derive(Constructor, PartialEq)]
pub struct AttrSet<Ctx: EvalContext> {
data: HashMap<String, Value<Ctx>>,
pub struct AttrSet {
data: HashMap<String, Value>,
}
impl<Ctx: EvalContext> Debug for AttrSet<Ctx> {
impl Debug for AttrSet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Value::*;
write!(f, "{{ ")?;
@@ -34,7 +34,7 @@ impl<Ctx: EvalContext> Debug for AttrSet<Ctx> {
}
}
impl<Ctx: EvalContext> Clone for AttrSet<Ctx> {
impl Clone for AttrSet {
fn clone(&self) -> Self {
AttrSet {
data: self.data.clone(),
@@ -42,31 +42,31 @@ impl<Ctx: EvalContext> Clone for AttrSet<Ctx> {
}
}
impl<Ctx: EvalContext> From<HashMap<String, Value<Ctx>>> for AttrSet<Ctx> {
fn from(data: HashMap<String, Value<Ctx>>) -> Self {
impl From<HashMap<String, Value>> for AttrSet {
fn from(data: HashMap<String, Value>) -> Self {
Self { data }
}
}
impl<Ctx: EvalContext> Deref for AttrSet<Ctx> {
type Target = HashMap<String, Value<Ctx>>;
impl Deref for AttrSet {
type Target = HashMap<String, Value>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<Ctx: EvalContext> AttrSet<Ctx> {
impl AttrSet {
pub fn with_capacity(cap: usize) -> Self {
AttrSet {
data: HashMap::with_capacity(cap),
}
}
pub fn push_attr_force(&mut self, sym: String, val: Value<Ctx>) {
pub fn push_attr_force(&mut self, sym: String, val: Value) {
self.data.insert(sym, val);
}
pub fn push_attr(&mut self, sym: String, val: Value<Ctx>) {
pub fn push_attr(&mut self, sym: String, val: Value) {
if self.data.get(&sym).is_some() {
todo!()
}
@@ -76,7 +76,7 @@ impl<Ctx: EvalContext> AttrSet<Ctx> {
pub fn select(
&self,
mut path: impl DoubleEndedIterator<Item = Result<String>>,
) -> Result<Value<Ctx>> {
) -> Result<Value> {
let mut data = &self.data;
let last = path.nth_back(0).unwrap();
for item in path {
@@ -116,15 +116,15 @@ impl<Ctx: EvalContext> AttrSet<Ctx> {
}
}
pub fn as_inner(&self) -> &HashMap<String, Value<Ctx>> {
pub fn as_inner(&self) -> &HashMap<String, Value> {
&self.data
}
pub fn into_inner(self: Rc<Self>) -> Rc<HashMap<String, Value<Ctx>>> {
pub fn into_inner(self: Rc<Self>) -> Rc<HashMap<String, Value>> {
unsafe { core::mem::transmute(self) }
}
pub fn from_inner(data: HashMap<String, Value<Ctx>>) -> Self {
pub fn from_inner(data: HashMap<String, Value>) -> Self {
Self { data }
}
@@ -137,11 +137,11 @@ impl<Ctx: EvalContext> AttrSet<Ctx> {
.all(|((k1, v1), (k2, v2))| k1 == k2 && v1.eq_impl(v2))
}
pub fn to_public(&self, ctx: &Ctx, seen: &mut HashSet<Value<Ctx>>) -> p::Value {
pub fn to_public(&self, seen: &mut HashSet<Value>) -> p::Value {
p::Value::AttrSet(p::AttrSet::new(
self.data
.iter()
.map(|(sym, value)| (sym.as_str().into(), value.to_public(ctx, seen)))
.map(|(sym, value)| (sym.as_str().into(), value.to_public(seen)))
.collect(),
))
}