-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
802 additions
and
588 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "calcit_std" | ||
version = "0.1.0" | ||
version = "0.0.2" | ||
authors = ["jiyinyiyong <[email protected]>"] | ||
edition = "2018" | ||
|
||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,76 @@ | ||
use cirru_edn::Edn; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
#[no_mangle] | ||
pub fn read_file(name: String) -> Result<String, String> { | ||
let task = fs::read_to_string(&name); | ||
match task { | ||
Ok(s) => Ok(s), | ||
Err(e) => Err(format!("Failed to read file {:?}: {}", name, e)), | ||
pub fn read_file(args: Vec<Edn>) -> Result<Edn, String> { | ||
if args.len() == 1 { | ||
if let Edn::Str(name) = &args[0] { | ||
let task = fs::read_to_string(&name); | ||
match task { | ||
Ok(s) => Ok(Edn::Str(s)), | ||
Err(e) => Err(format!("Failed to read file {:?}: {}", name, e)), | ||
} | ||
} else { | ||
Err(format!("read-file expected 1 filename, got {:?}", &args[0])) | ||
} | ||
} else { | ||
Err(format!("read-file expected 1 argument, got {:?}", args)) | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub fn write_file(name: String, content: String) -> Result<(), String> { | ||
let task = fs::write(name.to_owned(), content.to_owned()); | ||
match task { | ||
Ok(()) => Ok(()), | ||
Err(e) => Err(format!("Failed to write to file {:?}: {}", name, e)), | ||
pub fn write_file(args: Vec<Edn>) -> Result<Edn, String> { | ||
if args.len() == 2 { | ||
match (&args[0], &args[1]) { | ||
(Edn::Str(name), Edn::Str(content)) => { | ||
let task = fs::write(name.to_owned(), content.to_owned()); | ||
match task { | ||
Ok(()) => Ok(Edn::Nil), | ||
Err(e) => Err(format!("Failed to write to file {:?}: {}", name, e)), | ||
} | ||
} | ||
(_, _) => Err(format!("write-file expected 2 strings, got {:?}", args)), | ||
} | ||
} else { | ||
Err(format!("write-file expected 2 args, got {:?}", args)) | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub fn path_exists(name: String) -> Result<bool, String> { | ||
return Ok(Path::new(&name).exists()); | ||
pub fn path_exists(args: Vec<Edn>) -> Result<Edn, String> { | ||
if args.len() == 1 { | ||
if let Edn::Str(name) = &args[0] { | ||
Ok(Edn::Bool(Path::new(&name).exists())) | ||
} else { | ||
Err(format!("path-exists? expected 1 filename, got {:?}", args)) | ||
} | ||
} else { | ||
Err(format!("path-exists? expected 1 arg, got {:?}", args)) | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub fn read_dir(name: String) -> Result<Vec<String>, String> { | ||
let task = fs::read_dir(&name); | ||
match task { | ||
Ok(children) => { | ||
let mut content = vec![]; | ||
for c in children { | ||
content.push(format!("{}", c.unwrap().path().display())); | ||
} | ||
// println!("child dir: {:?}", content); | ||
pub fn read_dir(args: Vec<Edn>) -> Result<Edn, String> { | ||
if args.len() == 1 { | ||
if let Edn::Str(name) = &args[0] { | ||
let task = fs::read_dir(&name); | ||
match task { | ||
Ok(children) => { | ||
let mut content: Vec<Edn> = vec![]; | ||
for c in children { | ||
content.push(Edn::Str(format!("{}", c.unwrap().path().display()))); | ||
} | ||
// println!("child dir: {:?}", content); | ||
|
||
Ok(content) | ||
Ok(Edn::List(content)) | ||
} | ||
Err(e) => Err(format!("Failed to read dir {:?}: {}", name, e)), | ||
} | ||
} else { | ||
Err(format!("read-dir expected a string, {}", &args[0])) | ||
} | ||
Err(e) => Err(format!("Failed to read dir {:?}: {}", name, e)), | ||
} else { | ||
Err(format!("read-dir expected 2 args {:?}", args)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,44 @@ | ||
use cirru_edn::Edn; | ||
use std::process::Command; | ||
|
||
#[no_mangle] | ||
pub fn execute_command(dir: String, pieces: Vec<String>) -> Result<(String, String), String> { | ||
let mut cmd = String::from(""); | ||
let mut args: Vec<String> = vec![]; | ||
pub fn execute_command(args: Vec<Edn>) -> Result<Edn, String> { | ||
if args.len() == 2 { | ||
match (&args[0], &args[1]) { | ||
(Edn::Str(dir), Edn::List(ys)) => { | ||
let mut cmd = String::from(""); | ||
let mut xs: Vec<String> = vec![]; | ||
|
||
for (idx, piece) in pieces.iter().enumerate() { | ||
if idx == 0 { | ||
cmd = piece.to_string(); | ||
} else { | ||
args.push(piece.to_string()); | ||
} | ||
} | ||
for (idx, piece) in ys.iter().enumerate() { | ||
if let Edn::Str(s) = piece { | ||
if idx == 0 { | ||
cmd = s.to_owned(); | ||
} else { | ||
xs.push(s.to_owned()); | ||
} | ||
} else { | ||
return Err(format!( | ||
"execute-command expected string in list, got {}", | ||
piece | ||
)); | ||
} | ||
} | ||
|
||
match Command::new(cmd).current_dir(dir).args(&args).output() { | ||
Ok(t) => { | ||
let content = String::from_utf8(t.stdout).unwrap(); | ||
let stderr = String::from_utf8(t.stderr).unwrap(); | ||
Ok((content, stderr)) | ||
match Command::new(cmd).current_dir(dir).args(&xs).output() { | ||
Ok(t) => { | ||
let content = String::from_utf8(t.stdout).unwrap(); | ||
let stderr = String::from_utf8(t.stderr).unwrap(); | ||
Ok(Edn::List(vec![Edn::Str(content), Edn::Str(stderr)])) | ||
} | ||
Err(e) => Err(format!("Failed to excute: {}", e)), | ||
} | ||
} | ||
(_, _) => Err(format!( | ||
"excute-command expected string and list: {:?}", | ||
args | ||
)), | ||
} | ||
Err(e) => Err(format!("Failed to excute: {}", e)), | ||
} else { | ||
Err(format!("excute-command expected 2 args: {:?}", args)) | ||
} | ||
} |