use crate::serialization::types::TypeCode;
use super::sfunc::SFunc;
use super::smethod::SMethod;
use super::smethod::SMethodDesc;
use super::stype::SType;
use super::stype_companion::STypeCompanion;
use super::stype_param::STypeVar;
use crate::types::smethod::MethodId;
use lazy_static::lazy_static;
pub const TYPE_CODE: TypeCode = TypeCode::OPTION;
pub static TYPE_NAME: &str = "Option";
pub const MAP_METHOD_ID: MethodId = MethodId(7);
pub const FILTER_METHOD_ID: MethodId = MethodId(8);
lazy_static! {
pub(crate) static ref METHOD_DESC: Vec<&'static SMethodDesc> =
vec![
&MAP_METHOD_DESC,
&FILTER_METHOD_DESC,
]
;
}
lazy_static! {
static ref MAP_METHOD_DESC: SMethodDesc = SMethodDesc {
method_id: MAP_METHOD_ID,
name: "map",
tpe: SFunc::new(
vec![
SType::SOption(SType::STypeVar(STypeVar::iv()).into()),
SFunc::new(
vec![STypeVar::iv().into()],
STypeVar::ov().into(),
).into()
],
SType::SOption(SType::STypeVar(STypeVar::ov()).into()),
),
};
pub static ref MAP_METHOD: SMethod = SMethod::new(
STypeCompanion::Option,
MAP_METHOD_DESC.clone());
}
lazy_static! {
static ref FILTER_METHOD_DESC: SMethodDesc = SMethodDesc {
method_id: FILTER_METHOD_ID,
name: "filter",
tpe: SFunc::new(
vec![
SType::SOption(SType::STypeVar(STypeVar::iv()).into()),
SFunc::new(
vec![STypeVar::iv().into()],
SType::SBoolean,
).into()
],
SType::SOption(SType::STypeVar(STypeVar::iv()).into()),
),
};
pub static ref FILTER_METHOD: SMethod = SMethod::new(
STypeCompanion::Option,
FILTER_METHOD_DESC.clone());
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_ids() {
assert!(SMethod::from_ids(TYPE_CODE, MAP_METHOD_ID).map(|e| e.name()) == Ok("map"));
assert!(SMethod::from_ids(TYPE_CODE, FILTER_METHOD_ID).map(|e| e.name()) == Ok("filter"));
}
}