Skip to content

Commit

Permalink
Rename UserVal::set to new to be clearer (#3913)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtran authored Sep 19, 2024
1 parent 5cc92f0 commit db5331d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
23 changes: 11 additions & 12 deletions src/wasm-lib/kcl/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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();
Expand All @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/kcl/src/std/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/kcl/src/std/planes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}],
Expand Down

0 comments on commit db5331d

Please sign in to comment.