Skip to content

Commit

Permalink
Add fs.write
Browse files Browse the repository at this point in the history
  • Loading branch information
danhper committed Jan 7, 2025
1 parent 040ef53 commit 92f1444
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Add `abi.decodeData` to decode function calldata and errors from any known ABI
* Add `address.transfer` to send ETH to an address
* Add `json.stringify` to convert a value to a JSON string
* Add `fs.write` to write a string to a file

### Other changes

Expand Down
30 changes: 30 additions & 0 deletions src/interpreter/builtins/fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::sync::Arc;

use anyhow::{anyhow, Result};
use lazy_static::lazy_static;

use crate::interpreter::{
functions::{FunctionDef, FunctionParam, SyncMethod},
Env, Type, Value,
};

fn write(_env: &mut Env, _receiver: &Value, args: &[Value]) -> Result<Value> {
let filepath = args
.first()
.ok_or(anyhow!("missing filepath"))?
.as_string()?;
let value = args.get(1).ok_or(anyhow!("missing value"))?.as_string()?;
std::fs::write(filepath, value)?;
Ok(Value::Null)
}

lazy_static! {
pub static ref FS_WRITE: Arc<dyn FunctionDef> = SyncMethod::arc(
"write",
write,
vec![vec![
FunctionParam::new("filepath", Type::String),
FunctionParam::new("content", Type::String)
]]
);
}
6 changes: 6 additions & 0 deletions src/interpreter/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod console;
mod event;
mod events;
mod format;
mod fs;
mod iterable;
mod json;
mod misc;
Expand All @@ -32,6 +33,7 @@ lazy_static! {
m.insert("repl".to_string(), Value::TypeObject(Type::Repl));
m.insert("console".to_string(), Value::TypeObject(Type::Console));
m.insert("json".to_string(), Value::TypeObject(Type::Json));
m.insert("fs".to_string(), Value::TypeObject(Type::Fs));
m.insert("block".to_string(), Value::TypeObject(Type::Block));
m.insert("events".to_string(), Value::TypeObject(Type::Events));
m.insert(
Expand Down Expand Up @@ -145,6 +147,10 @@ lazy_static! {
json_methods.insert("stringify".to_string(), json::JSON_STRINGIFY.clone());
m.insert(NonParametricType::Json, json_methods);

let mut fs_methods = HashMap::new();
fs_methods.insert("write".to_string(), fs::FS_WRITE.clone());
m.insert(NonParametricType::Fs, fs_methods);

let mut event_methods = HashMap::new();
event_methods.insert("selector".to_string(), event::EVENT_SELECTOR.clone());
m.insert(NonParametricType::Event, event_methods);
Expand Down
4 changes: 4 additions & 0 deletions src/interpreter/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub enum NonParametricType {
Repl,
Block,
Console,
Fs,
Json,
Events,
Abi,
Expand Down Expand Up @@ -168,6 +169,7 @@ pub enum Type {
Repl,
Block,
Console,
Fs,
Json,
Events,
Abi,
Expand Down Expand Up @@ -208,6 +210,7 @@ impl Display for Type {
Type::Events => write!(f, "events"),
Type::Console => write!(f, "console"),
Type::Json => write!(f, "json"),
Type::Fs => write!(f, "fs"),
Type::Abi => write!(f, "abi"),
Type::Type(t) => write!(f, "type({})", t),
}
Expand Down Expand Up @@ -238,6 +241,7 @@ impl<T: AsRef<Type>> From<T> for NonParametricType {
Type::Repl => NonParametricType::Repl,
Type::Block => NonParametricType::Block,
Type::Console => NonParametricType::Console,
Type::Fs => NonParametricType::Fs,
Type::Json => NonParametricType::Json,
Type::Events => NonParametricType::Events,
Type::Abi => NonParametricType::Abi,
Expand Down

0 comments on commit 92f1444

Please sign in to comment.