Skip to content

Commit 85a4ab3

Browse files
committed
Add InputValue ser/de support for metadata container
Signed-off-by: akhilles <[email protected]>
1 parent c979615 commit 85a4ab3

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

crates/pcb-zen-core/src/lang/input.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use starlark::values::record::{FrozenRecord, FrozenRecordType, Record, RecordTyp
2121
use starlark::values::{Trace, Value, ValueLike};
2222

2323
use crate::lang::interface::{get_promotion_map, FrozenInterfaceValue, InterfaceValue};
24+
use crate::lang::metadata::MetadataContainer;
2425
use crate::lang::net::NetValue;
2526
use crate::lang::physical::PhysicalValue;
2627
use crate::{FrozenNetValue, NetId};
@@ -66,6 +67,9 @@ pub enum InputValue {
6667
promotion_by_type: SmallMap<String, String>,
6768
},
6869

70+
/// Represents a metadata container (stores ref_id for later lookup)
71+
MetadataContainer(MetadataContainer),
72+
6973
/// Fallback for unsupported / complex values.
7074
#[allocative(skip)]
7175
Unsupported(String),
@@ -112,6 +116,7 @@ impl fmt::Display for InputValue {
112116
)
113117
}
114118
}
119+
InputValue::MetadataContainer(mc) => write!(f, "{}", mc),
115120
InputValue::Unsupported(s) => write!(f, "Unsupported({s})"),
116121
}
117122
}
@@ -278,6 +283,7 @@ impl InputValue {
278283
eval.eval_function(typ_val, &[], &named)
279284
.map_err(|e| anyhow!(e.to_string()))
280285
}
286+
InputValue::MetadataContainer(mc) => Ok(heap.alloc(*mc)),
281287
InputValue::Unsupported(s) => Err(anyhow!("unsupported input value type: {s}")),
282288
}
283289
}
@@ -423,6 +429,10 @@ impl InputValue {
423429
};
424430
}
425431

432+
if let Some(metadata_container) = value.downcast_ref::<MetadataContainer>() {
433+
return InputValue::MetadataContainer(*metadata_container);
434+
}
435+
426436
InputValue::Unsupported(value.get_type().to_owned())
427437
}
428438
}

crates/pcb-zen-core/src/lang/metadata.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::sync::atomic::{AtomicU64, Ordering};
33

44
use allocative::Allocative;
55
use anyhow::anyhow;
6+
use serde::{Deserialize, Serialize};
67
use starlark::any::ProvidesStaticType;
78
use starlark::environment::GlobalsBuilder;
89
use starlark::eval::{Arguments, Evaluator};
@@ -16,7 +17,7 @@ use starlark::values::{Freeze, Heap, NoSerialize, StarlarkValue, Value, ValueLik
1617

1718
use crate::lang::evaluator_ext::EvaluatorExt;
1819
use crate::lang::input::InputValue;
19-
use crate::lang::physical::PhysicalValue;
20+
use crate::lang::physical::{physical_unit_from_ty, PhysicalValue};
2021

2122
/// Helper to access metadata store with error handling
2223
fn with_metadata_store<T>(
@@ -96,7 +97,9 @@ impl MetadataStore {
9697
}
9798

9899
/// A metadata container that holds a reference ID
99-
#[derive(Debug, Copy, Clone, ProvidesStaticType, NoSerialize, Allocative, Freeze)]
100+
#[derive(
101+
Debug, Copy, Clone, PartialEq, ProvidesStaticType, Serialize, Deserialize, Allocative, Freeze,
102+
)]
100103
pub struct MetadataContainer {
101104
pub ref_id: u64,
102105
}
@@ -201,8 +204,10 @@ impl<'v> StarlarkValue<'v> for MetadataContainerPush {
201204
);
202205
}
203206
// If physical value, do unit checking in addition to type checking
204-
if let Some(physical_value) = value.downcast_ref::<PhysicalValue>() {
205-
physical_value.check_unit_ty(type_compiled.as_ty())?;
207+
if let Some(expected) = physical_unit_from_ty(type_compiled.as_ty()) {
208+
if let Some(physical_value) = value.downcast_ref::<PhysicalValue>() {
209+
physical_value.check_unit(expected.into())?;
210+
}
206211
}
207212

208213
let input_value = InputValue::from_value(value);

crates/pcb-zen-core/src/lang/physical.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,6 @@ impl PhysicalValue {
159159
Ok(self)
160160
}
161161

162-
pub fn check_unit_ty(self, ty: &Ty) -> Result<Self, PhysicalValueError> {
163-
let expected_unit =
164-
physical_unit_from_ty(ty).ok_or(PhysicalValueError::InvalidPhysicalUnit)?;
165-
self.check_unit(expected_unit.into())
166-
}
167-
168162
pub fn from_arguments<'v, T: PhysicalUnitType<'v>>(
169163
positional: &[Value<'v>],
170164
kwargs: &SmallMap<ValueTyped<'v, StarlarkStr>, Value<'v>>,
@@ -1432,7 +1426,7 @@ define_physical_unit!(MagneticFluxType, PhysicalUnit::Webers, "MagneticFlux");
14321426

14331427
/// Helper function to extract PhysicalUnit from a Ty by matching the type name
14341428
/// Returns Some(PhysicalUnit) if the type matches a known physical unit type
1435-
fn physical_unit_from_ty(ty: &Ty) -> Option<PhysicalUnit> {
1429+
pub fn physical_unit_from_ty(ty: &Ty) -> Option<PhysicalUnit> {
14361430
// Try to convert the type to string and match against known names
14371431
let type_str = ty.to_string();
14381432
match type_str.as_str() {

0 commit comments

Comments
 (0)