mirror of
https://github.com/Dummi26/mers.git
synced 2025-03-10 05:43:53 +01:00
add fs module
This commit is contained in:
parent
414017402e
commit
6b2ae08731
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mers"
|
name = "mers"
|
||||||
version = "0.9.19"
|
version = "0.9.20"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
description = "dynamically typed but type-checked programming language"
|
description = "dynamically typed but type-checked programming language"
|
||||||
@ -15,7 +15,7 @@ default = ["colored-output"]
|
|||||||
colored-output = ["mers_lib/ecolor-term", "mers_lib/pretty-print", "dep:colored"]
|
colored-output = ["mers_lib/ecolor-term", "mers_lib/pretty-print", "dep:colored"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
mers_lib = "0.9.19"
|
mers_lib = "0.9.20"
|
||||||
# mers_lib = { path = "../mers_lib" }
|
# mers_lib = { path = "../mers_lib" }
|
||||||
clap = { version = "4.3.19", features = ["derive"] }
|
clap = { version = "4.3.19", features = ["derive"] }
|
||||||
colored = { version = "2.1.0", optional = true }
|
colored = { version = "2.1.0", optional = true }
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mers_lib"
|
name = "mers_lib"
|
||||||
version = "0.9.19"
|
version = "0.9.20"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
description = "library to use the mers language in other projects"
|
description = "library to use the mers language in other projects"
|
||||||
|
@ -15,6 +15,7 @@ pub mod gen;
|
|||||||
pub mod util;
|
pub mod util;
|
||||||
pub mod with_base;
|
pub mod with_base;
|
||||||
pub mod with_command_running;
|
pub mod with_command_running;
|
||||||
|
pub mod with_fs;
|
||||||
pub mod with_get;
|
pub mod with_get;
|
||||||
pub mod with_iters;
|
pub mod with_iters;
|
||||||
pub mod with_list;
|
pub mod with_list;
|
||||||
@ -41,8 +42,10 @@ impl Config {
|
|||||||
/// - `with_stdio()`
|
/// - `with_stdio()`
|
||||||
/// - `with_command_running()`
|
/// - `with_command_running()`
|
||||||
/// - `with_multithreading()`
|
/// - `with_multithreading()`
|
||||||
|
/// - `with_fs()`
|
||||||
pub fn bundle_std(self) -> Self {
|
pub fn bundle_std(self) -> Self {
|
||||||
self.with_multithreading()
|
self.with_fs()
|
||||||
|
.with_multithreading()
|
||||||
.with_command_running()
|
.with_command_running()
|
||||||
.with_stdio()
|
.with_stdio()
|
||||||
.bundle_pure()
|
.bundle_pure()
|
||||||
|
99
mers_lib/src/program/configs/with_fs.rs
Normal file
99
mers_lib/src/program/configs/with_fs.rs
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use crate::data::{
|
||||||
|
self,
|
||||||
|
function::Function,
|
||||||
|
object::{Object, ObjectFieldsMap, ObjectT},
|
||||||
|
string::StringT,
|
||||||
|
tuple::{Tuple, TupleT},
|
||||||
|
Data, Type,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::Config;
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
pub fn with_fs(self) -> Self {
|
||||||
|
self.add_var(
|
||||||
|
"fs_read_text",
|
||||||
|
Function::new_generic(
|
||||||
|
|a, i| {
|
||||||
|
if a.is_included_in_single(&StringT) {
|
||||||
|
Ok(Type::newm(vec![
|
||||||
|
Arc::new(StringT),
|
||||||
|
Arc::new(ObjectT::new(vec![(
|
||||||
|
i.global.object_fields.get_or_add_field("fs_read_error"),
|
||||||
|
Type::new(data::string::StringT),
|
||||||
|
)])),
|
||||||
|
]))
|
||||||
|
} else {
|
||||||
|
Err(format!(
|
||||||
|
"Called fs_read_text with argument type {}, but expected String",
|
||||||
|
a.with_info(i)
|
||||||
|
))?
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|a, i| {
|
||||||
|
let a = a.get();
|
||||||
|
let a = a
|
||||||
|
.as_any()
|
||||||
|
.downcast_ref::<data::string::String>()
|
||||||
|
.expect("got non-string argument to fs_read_text");
|
||||||
|
Ok(match std::fs::read_to_string(&a.0) {
|
||||||
|
Ok(contents) => Data::new(data::string::String(contents)),
|
||||||
|
Err(e) => Data::new(Object::new(vec![(
|
||||||
|
i.global.object_fields.get_or_add_field("fs_read_error"),
|
||||||
|
Data::new(data::string::String(e.to_string())),
|
||||||
|
)])),
|
||||||
|
})
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.add_var(
|
||||||
|
"fs_write",
|
||||||
|
Function::new_generic(
|
||||||
|
|a, i| {
|
||||||
|
if a.is_included_in_single(&TupleT(vec![
|
||||||
|
Type::new(StringT),
|
||||||
|
Type::new(StringT),
|
||||||
|
])) {
|
||||||
|
Ok(Type::newm(vec![
|
||||||
|
Arc::new(TupleT(vec![])),
|
||||||
|
Arc::new(ObjectT::new(vec![(
|
||||||
|
i.global.object_fields.get_or_add_field("fs_write_error"),
|
||||||
|
Type::new(data::string::StringT),
|
||||||
|
)])),
|
||||||
|
]))
|
||||||
|
} else {
|
||||||
|
Err(format!(
|
||||||
|
"Called fs_write with argument type {}, but expected (String, String)",
|
||||||
|
a.with_info(i)
|
||||||
|
))?
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|a, i| {
|
||||||
|
let a = a.get();
|
||||||
|
let a = a
|
||||||
|
.as_any()
|
||||||
|
.downcast_ref::<Tuple>()
|
||||||
|
.expect("got non-tuple argument to fs_read_text");
|
||||||
|
let (a, b) = (a.0[0].get(), a.0[1].get());
|
||||||
|
let a = a
|
||||||
|
.as_any()
|
||||||
|
.downcast_ref::<data::string::String>()
|
||||||
|
.expect("file path was not a string in fs_write");
|
||||||
|
let b = b
|
||||||
|
.as_any()
|
||||||
|
.downcast_ref::<data::string::String>()
|
||||||
|
.expect("file content was not a string in fs_write");
|
||||||
|
Ok(match std::fs::write(&a.0, &b.0) {
|
||||||
|
Ok(()) => Data::empty_tuple(),
|
||||||
|
Err(e) => Data::new(Object::new(vec![(
|
||||||
|
i.global.object_fields.get_or_add_field("fs_write_error"),
|
||||||
|
Data::new(data::string::String(e.to_string())),
|
||||||
|
)])),
|
||||||
|
})
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user