Implement Result type
This commit is contained in:
parent
ed1f139595
commit
b8f2fe7eb4
@ -5,6 +5,7 @@ use enum_iterator::{all, Sequence};
|
||||
use crate::{EnumDefinition, Identifier, Type, TypeDefinition, VariantContent};
|
||||
|
||||
static OPTION: OnceLock<TypeDefinition> = OnceLock::new();
|
||||
static RESULT: OnceLock<TypeDefinition> = OnceLock::new();
|
||||
|
||||
pub fn all_built_in_type_definitions() -> impl Iterator<Item = BuiltInTypeDefinition> {
|
||||
all()
|
||||
@ -13,12 +14,14 @@ pub fn all_built_in_type_definitions() -> impl Iterator<Item = BuiltInTypeDefini
|
||||
#[derive(Sequence)]
|
||||
pub enum BuiltInTypeDefinition {
|
||||
Option,
|
||||
Result,
|
||||
}
|
||||
|
||||
impl BuiltInTypeDefinition {
|
||||
pub fn name(&self) -> &'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)),
|
||||
],
|
||||
))
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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())
|
||||
)))
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user