From 0226dbd02b819d0e5f02c28ee674834ec108ca8a Mon Sep 17 00:00:00 2001 From: Mark <> Date: Wed, 31 Jan 2024 19:23:01 +0100 Subject: [PATCH] add missing file, bump version --- mers/Cargo.toml | 2 +- mers_lib/Cargo.toml | 2 +- mers_lib/src/program/configs/util.rs | 151 +++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 mers_lib/src/program/configs/util.rs diff --git a/mers/Cargo.toml b/mers/Cargo.toml index 9fd7da5..043849f 100755 --- a/mers/Cargo.toml +++ b/mers/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mers" -version = "0.3.4" +version = "0.3.5" edition = "2021" license = "MIT OR Apache-2.0" description = "dynamically typed but type-checked programming language" diff --git a/mers_lib/Cargo.toml b/mers_lib/Cargo.toml index 4c7b404..7fdcb38 100755 --- a/mers_lib/Cargo.toml +++ b/mers_lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mers_lib" -version = "0.3.4" +version = "0.3.5" edition = "2021" license = "MIT OR Apache-2.0" description = "library to use the mers language in other projects" diff --git a/mers_lib/src/program/configs/util.rs b/mers_lib/src/program/configs/util.rs new file mode 100644 index 0000000..9cd2b4b --- /dev/null +++ b/mers_lib/src/program/configs/util.rs @@ -0,0 +1,151 @@ +use std::sync::{Arc, Mutex}; + +use crate::{ + data::{self, Data, MersType, Type}, + errors::CheckError, + info::Info, +}; + +pub fn to_mers_func( + out: impl Fn(&Type) -> Result + Send + Sync + 'static, + run: impl Fn(Data) -> Data + Send + Sync + 'static, +) -> data::function::Function { + data::function::Function { + info: Arc::new(Info::neverused()), + info_check: Arc::new(Mutex::new(Info::neverused())), + out: Arc::new(move |a, _| out(a)), + run: Arc::new(move |a, _| run(a)), + inner_statements: None, + } +} + +pub fn to_mers_func_with_in_type( + in_type: Type, + out: impl Fn(&Type) -> Result + Send + Sync + 'static, + run: impl Fn(Data) -> Data + Send + Sync + 'static, +) -> data::function::Function { + to_mers_func( + move |a| { + if a.is_included_in(&in_type) { + out(a) + } else { + Err(format!("Function argument must be {in_type}, but was {a}.").into()) + } + }, + run, + ) +} + +pub fn to_mers_func_with_in_out_types( + in_type: Type, + out_type: Type, + run: impl Fn(Data) -> Data + Send + Sync + 'static, +) -> data::function::Function { + to_mers_func( + move |a| { + if a.is_included_in(&in_type) { + Ok(out_type.clone()) + } else { + Err(format!("Function argument must be {in_type}, but was {a}.").into()) + } + }, + run, + ) +} + +pub fn to_mers_func_concrete_string_to_any( + out_type: Type, + f: impl Fn(&str) -> Data + Send + Sync + 'static, +) -> data::function::Function { + to_mers_func_with_in_out_types(Type::new(data::string::StringT), out_type, move |a| { + f(a.get() + .as_any() + .downcast_ref::() + .unwrap() + .0 + .as_str()) + }) +} +pub fn to_mers_func_concrete_string_to_string( + f: impl Fn(&str) -> String + Send + Sync + 'static, +) -> data::function::Function { + to_mers_func_concrete_string_to_any(Type::new(data::string::StringT), move |a| { + Data::new(data::string::String(f(a))) + }) +} + +pub fn to_mers_func_concrete_string_string_to_any( + out_type: Type, + f: impl Fn(&str, &str) -> Data + Send + Sync + 'static, +) -> data::function::Function { + to_mers_func_with_in_out_types( + Type::new(data::tuple::TupleT(vec![ + Type::new(data::string::StringT), + Type::new(data::string::StringT), + ])), + out_type, + move |a| { + let a = a.get(); + let a = &a.as_any().downcast_ref::().unwrap().0; + let l = a[0].get(); + let r = a[1].get(); + f( + l.as_any() + .downcast_ref::() + .unwrap() + .0 + .as_str(), + r.as_any() + .downcast_ref::() + .unwrap() + .0 + .as_str(), + ) + }, + ) +} +pub fn to_mers_func_concrete_string_string_to_opt_int( + f: impl Fn(&str, &str) -> Option + Send + Sync + 'static, +) -> data::function::Function { + to_mers_func_concrete_string_string_to_any( + Type::newm(vec![ + Arc::new(data::tuple::TupleT(vec![])), + Arc::new(data::int::IntT), + ]), + move |a, b| { + f(a, b) + .map(|v| Data::new(data::int::Int(v))) + .unwrap_or_else(|| Data::empty_tuple()) + }, + ) +} +pub fn to_mers_func_concrete_string_string_to_bool( + f: impl Fn(&str, &str) -> bool + Send + Sync + 'static, +) -> data::function::Function { + to_mers_func_concrete_string_string_to_any(Type::new(data::bool::BoolT), move |a, b| { + Data::new(data::bool::Bool(f(a, b))) + }) +} +pub fn to_mers_func_concrete_string_string_to_opt_string_string( + f: impl Fn(&str, &str) -> Option<(String, String)> + Send + Sync + 'static, +) -> data::function::Function { + to_mers_func_concrete_string_string_to_any( + Type::newm(vec![ + Arc::new(data::tuple::TupleT(vec![])), + Arc::new(data::tuple::TupleT(vec![ + Type::new(data::string::StringT), + Type::new(data::string::StringT), + ])), + ]), + move |a, b| { + f(a, b) + .map(|(a, b)| { + Data::new(data::tuple::Tuple(vec![ + Data::new(data::string::String(a)), + Data::new(data::string::String(b)), + ])) + }) + .unwrap_or_else(|| Data::empty_tuple()) + }, + ) +}