Skip to content

Commit

Permalink
add append-file function; tag 0.0.20
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Mar 22, 2023
1 parent 0d4e665 commit 0096113
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit_std"
version = "0.0.19"
version = "0.0.20"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2021"

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Providing:
```cirru
calcit.std.fs/read-file! a
calcit.std.fs/write-file! a b
calcit.std.fs/append-file! a b
calcit.std.fs/check-write-file! a b
calcit.std.fs/path-exists? a
calcit.std.fs/read-dir! a
Expand Down
30 changes: 29 additions & 1 deletion calcit.cirru

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions compact.cirru
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

{} (:package |calcit.std)
:configs $ {} (:init-fn |calcit.std.test/main!) (:reload-fn |calcit.std.test/reload!) (:version |0.0.19)
:configs $ {} (:init-fn |calcit.std.test/main!) (:reload-fn |calcit.std.test/reload!) (:version |0.0.20)
:modules $ []
:entries $ {}
:files $ {}
Expand Down Expand Up @@ -50,6 +50,9 @@
calcit.std.util :refer $ get-dylib-path
|calcit.std.fs $ {}
:defs $ {}
|append-file! $ quote
defn append-file! (name content)
&call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"append_file" name content
|check-write-file! $ quote
defn check-write-file! (name content)
&call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"check_write_file" name content
Expand Down Expand Up @@ -238,10 +241,11 @@
rename! "\"target/dir1" "\"target/dir4"
create-dir-all! "\"target/dir2/dir3"
check-write-file! "\"target/dir8/dir9/file.text" "\"TODO"
append-file! "\"target/dir8/dir9/file.text" $ str &newline "\"NEWLINE TODO"
:ns $ quote
ns calcit.std.test.fs $ :require
calcit.std.$meta :refer $ calcit-filename calcit-dirname
calcit.std.fs :refer $ read-file! write-file! path-exists? read-dir! create-dir! create-dir-all! rename! check-write-file! walk-dir! glob!
calcit.std.fs :refer $ read-file! append-file! write-file! path-exists? read-dir! create-dir! create-dir-all! rename! check-write-file! walk-dir! glob!
calcit.std.process :refer $ execute!
|calcit.std.test.json $ {}
:defs $ {}
Expand Down
23 changes: 23 additions & 0 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ pub fn write_file(args: Vec<Edn>) -> Result<Edn, String> {
}
}

#[no_mangle]
pub fn append_file(args: Vec<Edn>) -> Result<Edn, String> {
use std::fs::OpenOptions;
use std::io::prelude::*;

if args.len() == 2 {
match (&args[0], &args[1]) {
(Edn::Str(name), Edn::Str(content)) => {
let mut file = OpenOptions::new().write(true).append(true).open(&**name).unwrap();

if let Err(e) = writeln!(file, "{}", content) {
Err(format!("Failed to append to file {name:?}: {e}"))
} else {
Ok(Edn::Nil)
}
}
(_, _) => Err(format!("append-file expected 2 strings, got {args:?}")),
}
} else {
Err(format!("append-file expected 2 args, got {args:?}"))
}
}

#[no_mangle]
pub fn path_exists(args: Vec<Edn>) -> Result<Edn, String> {
if args.len() == 1 {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod random;
mod time;

pub use date::{add_duration, format_time, now_bang, parse_time};
pub use fs::{glob_call, path_exists, read_dir, read_file, walk_dir, write_file};
pub use fs::{append_file, glob_call, path_exists, read_dir, read_file, walk_dir, write_file};
pub use json::{parse_json, stringify_json};
pub use path::join_path;
pub use process::execute_command;
Expand Down

0 comments on commit 0096113

Please sign in to comment.