diff --git a/src/built_in_type_definitions.rs b/src/built_in_type_definitions.rs index 8d97630..7a33962 100644 --- a/src/built_in_type_definitions.rs +++ b/src/built_in_type_definitions.rs @@ -5,6 +5,7 @@ use enum_iterator::{all, Sequence}; use crate::{EnumDefinition, Identifier, Type, TypeDefinition, VariantContent}; static OPTION: OnceLock = OnceLock::new(); +static RESULT: OnceLock = OnceLock::new(); pub fn all_built_in_type_definitions() -> impl Iterator { all() @@ -13,12 +14,14 @@ pub fn all_built_in_type_definitions() -> impl Iterator &'static str { match self { BuiltInTypeDefinition::Option => "Option", + BuiltInTypeDefinition::Result => "Result", } } @@ -26,13 +29,22 @@ impl BuiltInTypeDefinition { match self { BuiltInTypeDefinition::Option => OPTION.get_or_init(|| { TypeDefinition::Enum(EnumDefinition::new( - Identifier::new("Option"), + Identifier::new(self.name()), vec![ (Identifier::new("Some"), VariantContent::Type(Type::Any)), (Identifier::new("None"), VariantContent::None), ], )) }), + BuiltInTypeDefinition::Result => RESULT.get_or_init(|| { + TypeDefinition::Enum(EnumDefinition::new( + Identifier::new(self.name()), + vec![ + (Identifier::new("Ok"), VariantContent::Type(Type::Any)), + (Identifier::new("Err"), VariantContent::Type(Type::Any)), + ], + )) + }), } } } diff --git a/tests/built_in_type_definitions.rs b/tests/built_in_type_definitions.rs index 39fedf5..0ff3051 100644 --- a/tests/built_in_type_definitions.rs +++ b/tests/built_in_type_definitions.rs @@ -36,4 +36,17 @@ fn result() { Value::Integer(1) ))) ); + assert_eq!( + interpret( + " + result = new Result:Error('uh-oh!') + result + " + ), + Ok(Value::Enum(EnumInstance::new( + "Result".to_string(), + "Error".to_string(), + Value::String("uh-oh!".to_string()) + ))) + ); }