microcad_lang/eval/parameter/
parameter_value.rs1use microcad_lang_base::SrcRef;
7use microcad_lang_proc_macros::SrcReferrer;
8
9use crate::{ty::*, value::*};
10
11#[derive(Clone, Debug, Default, SrcReferrer)]
13pub struct ParameterValue {
14 pub specified_type: Option<Type>,
16 pub default_value: Option<Value>,
18 pub src_ref: SrcRef,
20}
21
22impl ParameterValue {
23 pub fn invalid(src_ref: SrcRef) -> Self {
25 Self {
26 specified_type: None,
27 default_value: None,
28 src_ref,
29 }
30 }
31}
32
33impl Ty for ParameterValue {
34 fn ty(&self) -> Type {
39 if let Some(ty) = &self.specified_type {
40 ty.clone()
41 } else if let Some(def) = &self.default_value {
42 def.ty()
43 } else {
44 Type::Invalid
45 }
46 }
47}
48
49impl std::fmt::Display for ParameterValue {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51 if let Some(def) = &self.default_value {
52 write!(f, "{} = {def}", def.ty())?;
53 } else if let Some(ty) = &self.specified_type {
54 write!(f, "= {ty}")?;
55 }
56 Ok(())
57 }
58}
59
60#[test]
61fn test_is_list_of() {
62 assert!(
63 Type::Array(Box::new(QuantityType::Scalar.into()))
64 .is_array_of(&QuantityType::Scalar.into())
65 );
66}