Rename UserVal::set to new to be clearer (#3913)

This commit is contained in:
Jonathan Tran
2024-09-18 20:23:19 -04:00
committed by GitHub
parent 5cc92f0162
commit db5331d9b9
3 changed files with 13 additions and 14 deletions

View File

@ -319,7 +319,7 @@ pub enum KclValue {
impl KclValue {
pub(crate) fn new_user_val<T: Serialize>(meta: Vec<Metadata>, val: T) -> Self {
Self::UserVal(UserVal::set(meta, val))
Self::UserVal(UserVal::new(meta, val))
}
pub(crate) fn get_extrude_group_set(&self) -> Result<ExtrudeGroupSet> {
@ -364,14 +364,14 @@ impl KclValue {
impl From<SketchGroupSet> for KclValue {
fn from(sg: SketchGroupSet) -> Self {
KclValue::UserVal(UserVal::set(sg.meta(), sg))
KclValue::UserVal(UserVal::new(sg.meta(), sg))
}
}
impl From<Vec<Box<SketchGroup>>> for KclValue {
fn from(sg: Vec<Box<SketchGroup>>) -> Self {
let meta = sg.iter().flat_map(|sg| sg.meta.clone()).collect();
KclValue::UserVal(UserVal::set(meta, sg))
KclValue::UserVal(UserVal::new(meta, sg))
}
}
@ -662,6 +662,13 @@ pub struct UserVal {
}
impl UserVal {
pub fn new<T: serde::Serialize>(meta: Vec<Metadata>, val: T) -> Self {
Self {
meta,
value: serde_json::to_value(val).expect("all KCL values should be compatible with JSON"),
}
}
/// If the UserVal matches the type `T`, return it.
pub fn get<T: serde::de::DeserializeOwned>(&self) -> Option<(T, Vec<Metadata>)> {
let meta = self.meta.clone();
@ -685,17 +692,9 @@ impl UserVal {
return Ok(());
};
mutate(&mut val)?;
*self = Self::set(meta, val);
*self = Self::new(meta, val);
Ok(())
}
/// Put the given value into this UserVal.
pub fn set<T: serde::Serialize>(meta: Vec<Metadata>, val: T) -> Self {
Self {
meta,
value: serde_json::to_value(val).expect("all KCL values should be compatible with JSON"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ts_rs::TS, JsonSchema)]

View File

@ -21,7 +21,7 @@ pub async fn array_reduce(exec_state: &mut ExecState, args: Args) -> Result<KclV
};
inner_array_reduce(array, start, reduce_fn, exec_state, &args)
.await
.map(|sg| KclValue::UserVal(UserVal::set(sg.meta.clone(), sg)))
.map(|sg| KclValue::UserVal(UserVal::new(sg.meta.clone(), sg)))
}
/// Take a starting value. Then, for each element of an array, calculate the next value,

View File

@ -53,7 +53,7 @@ pub async fn offset_plane(_exec_state: &mut ExecState, args: Args) -> Result<Kcl
let plane = inner_offset_plane(std_plane, offset).await?;
Ok(KclValue::UserVal(UserVal::set(
Ok(KclValue::UserVal(UserVal::new(
vec![Metadata {
source_range: args.source_range,
}],