From 894931a6e77815b5f2aabd6ba8bb1055d4e9ba52 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Mon, 3 Mar 2025 11:43:43 -0800 Subject: [PATCH 1/3] Replace `try_set` with `set_if_ok` --- app-proto/src/assembly.rs | 12 +++++------- app-proto/src/outline.rs | 9 ++++++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app-proto/src/assembly.rs b/app-proto/src/assembly.rs index d30a371..9b123f5 100644 --- a/app-proto/src/assembly.rs +++ b/app-proto/src/assembly.rs @@ -181,13 +181,11 @@ pub struct Regulator { } impl Regulator { - pub fn try_set(&self, set_pt_spec: String) -> bool { - match SpecifiedValue::try_from(set_pt_spec) { - Ok(set_pt) => { - self.set_point.set(set_pt); - true - } - Err(_) => false + /* TO DO */ + // if it's called for, add a `set` method that takes a bare SpecifiedValue + pub fn set_if_ok(&self, set_pt_result: Result) { + if let Ok(set_pt) = set_pt_result { + self.set_point.set(set_pt); } } } diff --git a/app-proto/src/outline.rs b/app-proto/src/outline.rs index 04bda9a..8f81a82 100644 --- a/app-proto/src/outline.rs +++ b/app-proto/src/outline.rs @@ -13,6 +13,7 @@ use crate::{ ElementKey, Regulator, RegulatorKey, + SpecifiedValue, SpecifiedValue::* } }; @@ -55,9 +56,11 @@ fn RegulatorInput(regulator: Regulator) -> View { }, placeholder=regulator.measurement.with(|result| result.to_string()), bind:value=value, - on:change=move |_| valid.set( - regulator.try_set(value.get_clone_untracked()) - ), + on:change=move |_| { + let set_pt_result = SpecifiedValue::try_from(value.get_clone_untracked()); + valid.set(set_pt_result.is_ok()); + regulator.set_if_ok(set_pt_result); + }, on:keydown={ move |event: KeyboardEvent| { match event.key().as_str() { From 309b0881dfdc63e73b10db0641b272b2350b6cb3 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Mon, 3 Mar 2025 12:22:43 -0800 Subject: [PATCH 2/3] Move `SpecifiedValue` into its own module --- app-proto/src/assembly.rs | 66 +++----------------------------------- app-proto/src/main.rs | 1 + app-proto/src/outline.rs | 7 ++-- app-proto/src/specified.rs | 56 ++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 65 deletions(-) create mode 100644 app-proto/src/specified.rs diff --git a/app-proto/src/assembly.rs b/app-proto/src/assembly.rs index 9b123f5..01dee9d 100644 --- a/app-proto/src/assembly.rs +++ b/app-proto/src/assembly.rs @@ -1,15 +1,14 @@ use nalgebra::{DMatrix, DVector, DVectorView, Vector3}; use rustc_hash::FxHashMap; use slab::Slab; -use std::{ - collections::BTreeSet, - num::ParseFloatError, - sync::atomic::{AtomicU64, Ordering} -}; +use std::{collections::BTreeSet, sync::atomic::{AtomicU64, Ordering}}; use sycamore::prelude::*; use web_sys::{console, wasm_bindgen::JsValue}; /* DEBUG */ -use crate::engine::{Q, local_unif_to_std, realize_gram, ConfigSubspace, PartialMatrix}; +use crate::{ + engine::{Q, local_unif_to_std, realize_gram, ConfigSubspace, PartialMatrix}, + specified::{SpecifiedValue, SpecifiedValue::{Absent, Present}} +}; // the types of the keys we use to access an assembly's elements and regulators pub type ElementKey = usize; @@ -118,61 +117,6 @@ impl Element { } } -// to construct a `SpecifiedValue` that might be `Present`, use the associated -// function `try_from`. this ensures that `spec` is always a valid specification -// of `value` according to the format discussed at the implementation of -// `TryFrom` -pub enum SpecifiedValue { - Absent, - Present { - spec: String, - value: f64 - } -} - -use SpecifiedValue::*; - -impl SpecifiedValue { - // get the specification for this value. the associated function `try_from` - // is essentially a left inverse of this method: - // - // SpecifiedValue::try_from(x.spec()) == Ok(x) - // - pub fn spec(&self) -> String { - match self { - Absent => String::new(), - Present { spec, .. } => spec.clone() - } - } - - fn is_present(&self) -> bool { - match self { - Absent => false, - Present { .. } => true - } - } -} - -// we can try to turn a specification string into a `SpecifiedValue`. if the -// specification is empty, the `SpecifiedValue` is `Absent`. if the -// specification parses to a floating-point value `x`, the `SpecifiedValue` is -// `Present`, with a `value` of `x`, and the specification is stored in `spec`. -// these are currently the only valid specifications; any other produces an -// error -impl TryFrom for SpecifiedValue { - type Error = ParseFloatError; - - fn try_from(spec: String) -> Result { - if spec.is_empty() { - Ok(Absent) - } else { - spec.parse::().map( - |value| Present { spec: spec, value: value } - ) - } - } -} - #[derive(Clone, Copy)] pub struct Regulator { pub subjects: (ElementKey, ElementKey), diff --git a/app-proto/src/main.rs b/app-proto/src/main.rs index f961504..6ab3e49 100644 --- a/app-proto/src/main.rs +++ b/app-proto/src/main.rs @@ -3,6 +3,7 @@ mod assembly; mod display; mod engine; mod outline; +mod specified; use rustc_hash::FxHashSet; use sycamore::prelude::*; diff --git a/app-proto/src/outline.rs b/app-proto/src/outline.rs index 8f81a82..31837bb 100644 --- a/app-proto/src/outline.rs +++ b/app-proto/src/outline.rs @@ -12,10 +12,9 @@ use crate::{ assembly::{ ElementKey, Regulator, - RegulatorKey, - SpecifiedValue, - SpecifiedValue::* - } + RegulatorKey + }, + specified::{SpecifiedValue, SpecifiedValue::{Absent, Present}} }; // an editable view of a regulator diff --git a/app-proto/src/specified.rs b/app-proto/src/specified.rs new file mode 100644 index 0000000..ea873e9 --- /dev/null +++ b/app-proto/src/specified.rs @@ -0,0 +1,56 @@ +use std::num::ParseFloatError; + +// to construct a `SpecifiedValue` that might be `Present`, use the associated +// function `try_from`. this ensures that `spec` is always a valid specification +// of `value` according to the format discussed at the implementation of +// `TryFrom` +pub enum SpecifiedValue { + Absent, + Present { + spec: String, + value: f64 + } +} + +use SpecifiedValue::{Absent, Present}; + +impl SpecifiedValue { + // get the specification for this value. the associated function `try_from` + // is essentially a left inverse of this method: + // + // SpecifiedValue::try_from(x.spec()) == Ok(x) + // + pub fn spec(&self) -> String { + match self { + Absent => String::new(), + Present { spec, .. } => spec.clone() + } + } + + pub fn is_present(&self) -> bool { + match self { + Absent => false, + Present { .. } => true + } + } +} + +// we can try to turn a specification string into a `SpecifiedValue`. if the +// specification is empty, the `SpecifiedValue` is `Absent`. if the +// specification parses to a floating-point value `x`, the `SpecifiedValue` is +// `Present`, with a `value` of `x`, and the specification is stored in `spec`. +// these are currently the only valid specifications; any other produces an +// error +impl TryFrom for SpecifiedValue { + type Error = ParseFloatError; + + fn try_from(spec: String) -> Result { + if spec.is_empty() { + Ok(Absent) + } else { + spec.parse::().map( + |value| Present { spec: spec, value: value } + ) + } + } +} \ No newline at end of file From c58fed073db9748d4c1e4d81fdb757286c7e6a25 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Mon, 3 Mar 2025 12:31:30 -0800 Subject: [PATCH 3/3] Drop the `is_present` utility method --- app-proto/src/assembly.rs | 2 +- app-proto/src/specified.rs | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/app-proto/src/assembly.rs b/app-proto/src/assembly.rs index 01dee9d..002b420 100644 --- a/app-proto/src/assembly.rs +++ b/app-proto/src/assembly.rs @@ -269,7 +269,7 @@ impl Assembly { console::log_1(&JsValue::from( format!("Updated constraint with subjects ({}, {})", subjects.0, subjects.1) )); - if set_point.with(|set_pt| set_pt.is_present()) { + if set_point.with(|set_pt| matches!(set_pt, Present { .. })) { self.realize(); } }); diff --git a/app-proto/src/specified.rs b/app-proto/src/specified.rs index ea873e9..a943f2b 100644 --- a/app-proto/src/specified.rs +++ b/app-proto/src/specified.rs @@ -26,13 +26,6 @@ impl SpecifiedValue { Present { spec, .. } => spec.clone() } } - - pub fn is_present(&self) -> bool { - match self { - Absent => false, - Present { .. } => true - } - } } // we can try to turn a specification string into a `SpecifiedValue`. if the