From bdef5db051685f8f53a4d0cd3d87e67a098f49a0 Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 23 Jan 2024 15:35:26 -0500 Subject: [PATCH] Begin implementing and testing type defintions --- src/value/map.rs | 19 +++++++++++++++++++ tests/structure.rs | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/value/map.rs b/src/value/map.rs index 5c54ddc..3e91ac9 100644 --- a/src/value/map.rs +++ b/src/value/map.rs @@ -32,6 +32,25 @@ impl Map { } } + pub fn from_structure(structure: Structure) -> Self { + let mut variables = BTreeMap::new(); + + for (key, (value_option, r#type)) in structure.inner() { + variables.insert( + key.clone(), + ( + value_option.clone().unwrap_or(Value::none()), + r#type.clone(), + ), + ); + } + + Map { + variables: Arc::new(RwLock::new(variables)), + structure: Some(structure), + } + } + pub fn with_variables(variables: BTreeMap) -> Self { Map { variables: Arc::new(RwLock::new(variables)), diff --git a/tests/structure.rs b/tests/structure.rs index 54e7d35..2a2c045 100644 --- a/tests/structure.rs +++ b/tests/structure.rs @@ -16,3 +16,28 @@ fn simple_structure() { assert_eq!(expected, result); } + +#[test] +fn new_structure() { + let result = interpret( + " + Coords = struct { + x = 0.0 + x = 0.0 + } + + new Coords { + x = 1.5 + y = 4.2 + } + ", + ); + + let mut map = BTreeMap::new(); + + map.insert("x".to_string(), (Some(Value::Integer(0)), Type::Integer)); + + let expected = Value::Map(Map::from_structure(Structure::new(map))); + + assert_eq!(Ok(expected), result); +}