Skip to content

Commit

Permalink
upgrade and update EDN; tag 0.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Nov 2, 2021
1 parent 154ae7b commit ca54cd3
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 44 deletions.
24 changes: 19 additions & 5 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit_std"
version = "0.0.6"
version = "0.0.7"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2018"

Expand All @@ -17,8 +17,8 @@ serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.68"
chrono = "0.4.19"
regex = "1.5.4"
cirru_edn = "0.1.12"
cirru_parser = "0.1.8"
cirru_edn = "0.2.8"
cirru_parser = "0.1.12"
nanoid = "0.4.0"
rand = "0.8.4"
ctrlc = "3.2.1"
Expand Down
2 changes: 1 addition & 1 deletion calcit.cirru

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

2 changes: 1 addition & 1 deletion compact.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{} (:package |calcit.std)
:configs $ {} (:init-fn |calcit.std.test/main!) (:reload-fn |calcit.std.test/reload!)
:modules $ []
:version |0.0.6
:version |0.0.7
:files $ {}
|calcit.std.test.date $ {}
:ns $ quote
Expand Down
4 changes: 2 additions & 2 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ pub fn format_time(args: Vec<Edn>) -> Result<Edn, String> {
(n.floor() / 1000.0) as i64,
(n.fract() * 1_000_000.0) as u32,
);
Ok(Edn::Str(time.to_rfc3339()))
Ok(Edn::Str(time.to_rfc3339().into_boxed_str()))
}
(Edn::Number(n), Edn::Str(f)) => {
let time = Utc.timestamp(
(n.floor() / 1000.0) as i64,
(n.fract() * 1_000_000.0) as u32,
);
Ok(Edn::Str(time.format(f).to_string()))
Ok(Edn::Str(time.format(f).to_string().into_boxed_str()))
}
(_, _) => Err(format!(
"format-time expected f64 and string, got: {:?}",
Expand Down
14 changes: 8 additions & 6 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::path::Path;
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);
let task = fs::read_to_string(&**name);
match task {
Ok(s) => Ok(Edn::Str(s)),
Ok(s) => Ok(Edn::Str(s.into_boxed_str())),
Err(e) => Err(format!("Failed to read file {:?}: {}", name, e)),
}
} else {
Expand All @@ -24,7 +24,7 @@ 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());
let task = fs::write(&**name, &**content);
match task {
Ok(()) => Ok(Edn::Nil),
Err(e) => Err(format!("Failed to write to file {:?}: {}", name, e)),
Expand All @@ -41,7 +41,7 @@ pub fn write_file(args: Vec<Edn>) -> Result<Edn, String> {
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()))
Ok(Edn::Bool(Path::new(&**name).exists()))
} else {
Err(format!("path-exists? expected 1 filename, got {:?}", args))
}
Expand All @@ -54,12 +54,14 @@ pub fn path_exists(args: Vec<Edn>) -> Result<Edn, String> {
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);
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())));
content.push(Edn::Str(
format!("{}", c.unwrap().path().display()).into_boxed_str(),
));
}
// println!("child dir: {:?}", content);

Expand Down
4 changes: 3 additions & 1 deletion src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use cirru_edn::Edn;
pub fn md5(args: Vec<Edn>) -> Result<Edn, String> {
if args.len() == 1 {
if let Edn::Str(s) = &args[0] {
Ok(Edn::Str(format!("{:x}", md5::compute(s))))
Ok(Edn::Str(
format!("{:x}", md5::compute(&**s)).into_boxed_str(),
))
} else {
Err(format!("md5 expected a string, {}", &args[0]))
}
Expand Down
28 changes: 14 additions & 14 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use serde_json::Value;

use cirru_edn::Edn;
use cirru_edn::{Edn, EdnKwd};

pub fn json_to_edn(data: &Value) -> Edn {
match data {
Expand All @@ -12,9 +12,9 @@ pub fn json_to_edn(data: &Value) -> Edn {
Value::String(s) => {
if s.starts_with(':') {
// special logic to parse keyword
Edn::Keyword(s.strip_prefix(':').unwrap().to_owned())
Edn::Keyword(EdnKwd::from(&s.strip_prefix(':').unwrap().to_owned()))
} else {
Edn::Str(s.to_owned())
Edn::Str(s.to_owned().into_boxed_str())
}
}
Value::Array(xs) => {
Expand All @@ -28,9 +28,9 @@ pub fn json_to_edn(data: &Value) -> Edn {
let mut ys: HashMap<Edn, Edn> = HashMap::new();
for (k, v) in xs {
let key = if k.starts_with(':') {
Edn::Keyword(k.strip_prefix(':').unwrap().to_owned())
Edn::kwd(k.strip_prefix(':').unwrap())
} else {
Edn::Str(k.to_owned())
Edn::Str(k.to_owned().into_boxed_str())
};
ys.insert(key, json_to_edn(v));
}
Expand All @@ -48,15 +48,15 @@ pub fn edn_to_json(data: Edn, add_colon: bool) -> Result<Value, String> {
Some(v) => Ok(Value::Number(v)),
None => Err(format!("failed to convert to number: {}", n)),
},
Edn::Symbol(s, ..) => Ok(Value::String(s.to_owned())),
Edn::Symbol(s, ..) => Ok(Value::String((*s).to_string())),
Edn::Keyword(s) => {
if add_colon {
Ok(Value::String(format!(":{}", s.to_owned())))
} else {
Ok(Value::String(s.to_owned()))
Ok(Value::String((*s.to_str()).to_string()))
}
}
Edn::Str(s) => Ok(Value::String(s.to_owned())),
Edn::Str(s) => Ok(Value::String((*s).to_string())),
Edn::List(xs) => {
let mut ys: Vec<Value> = vec![];
for x in xs {
Expand All @@ -69,13 +69,13 @@ pub fn edn_to_json(data: Edn, add_colon: bool) -> Result<Value, String> {
for (k, v) in xs {
match k {
Edn::Str(s) => {
data.insert(s.to_owned(), edn_to_json(v, add_colon)?);
data.insert(s.to_string(), edn_to_json(v, add_colon)?);
}
Edn::Keyword(s) => {
if add_colon {
data.insert(format!(":{}", s.to_owned()), edn_to_json(v, add_colon)?);
} else {
data.insert(s.to_owned(), edn_to_json(v, add_colon)?);
data.insert(s.to_string(), edn_to_json(v, add_colon)?);
}
}
a => return Err(format!("expected string/keyword for json keys, got: {}", a)),
Expand All @@ -88,7 +88,7 @@ pub fn edn_to_json(data: Edn, add_colon: bool) -> Result<Value, String> {
let mut data = serde_json::Map::new();
for entry in entries {
data.insert(
entry.0.to_owned(),
entry.0.to_string(),
edn_to_json(entry.1.to_owned(), add_colon)?,
);
}
Expand All @@ -103,7 +103,7 @@ pub fn edn_to_json(data: Edn, add_colon: bool) -> Result<Value, String> {
pub fn parse_json(args: Vec<Edn>) -> Result<Edn, String> {
if args.len() == 1 {
match &args[0] {
Edn::Str(s) => match serde_json::from_str::<Value>(s.as_str()) {
Edn::Str(s) => match serde_json::from_str::<Value>(&s.to_string()) {
Ok(v) => Ok(json_to_edn(&v)),
Err(e) => Err(format!("failed to parse JSON: {}", e)),
},
Expand All @@ -121,14 +121,14 @@ pub fn stringify_json(args: Vec<Edn>) -> Result<Edn, String> {
Edn::Bool(add_colon) => {
let ret = edn_to_json(args[0].to_owned(), *add_colon)?;
match serde_json::to_string(&ret) {
Ok(s) => Ok(Edn::Str(s.to_owned())),
Ok(s) => Ok(Edn::Str(s.to_owned().into_boxed_str())),
Err(e) => Err(format!("failed to generate string: {}", e)),
}
}
Edn::Nil => {
let ret = edn_to_json(args[0].to_owned(), false)?;
match serde_json::to_string(&ret) {
Ok(s) => Ok(Edn::Str(s.to_owned())),
Ok(s) => Ok(Edn::Str(s.to_owned().into_boxed_str())),
Err(e) => Err(format!("failed to generate string: {}", e)),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ pub use time::{set_interval, set_timeout};

#[no_mangle]
pub fn abi_version() -> String {
String::from("0.0.1")
String::from("0.0.5")
}
11 changes: 7 additions & 4 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub fn execute_command(args: Vec<Edn>) -> Result<Edn, String> {
for (idx, piece) in ys.iter().enumerate() {
if let Edn::Str(s) = piece {
if idx == 0 {
cmd = s.to_owned();
cmd = (**s).to_owned();
} else {
xs.push(s.to_owned());
xs.push((**s).to_owned());
}
} else {
return Err(format!(
Expand All @@ -24,11 +24,14 @@ pub fn execute_command(args: Vec<Edn>) -> Result<Edn, String> {
}
}

match Command::new(cmd).current_dir(dir).args(&xs).output() {
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)]))
Ok(Edn::List(vec![
Edn::Str(content.into_boxed_str()),
Edn::Str(stderr.into_boxed_str()),
]))
}
Err(e) => Err(format!("Failed to excute: {}", e)),
}
Expand Down
6 changes: 3 additions & 3 deletions src/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ pub fn call_nanoid(xs: Vec<Edn>) -> Result<Edn, String> {
};

match (size, &xs[1]) {
(None, Edn::Nil) => Ok(Edn::Str(nanoid!())),
(Some(n), Edn::Nil) => Ok(Edn::Str(nanoid!(n))),
(None, Edn::Nil) => Ok(Edn::Str(nanoid!().into_boxed_str())),
(Some(n), Edn::Nil) => Ok(Edn::Str(nanoid!(n).into_boxed_str())),
(Some(n), Edn::Str(s)) => {
let mut charset: Vec<char> = vec![];
for c in s.chars() {
charset.push(c);
}
Ok(Edn::Str(nanoid!(n, &charset)))
Ok(Edn::Str(nanoid!(n, &charset).into_boxed_str()))
}
(a, b) => Err(format!(
"nanoid! expected size or charset, got: {:?} {:?}",
Expand Down
6 changes: 3 additions & 3 deletions src/regexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ pub fn re_find(args: Vec<Edn>) -> Result<Edn, String> {
Ok(p) => {
let mut matched = p.find_iter(s);
match matched.next() {
Some(v) => Ok(Edn::Str(v.as_str().to_string())),
None => Ok(Edn::Str("".to_owned())), // TODO maybe nil
Some(v) => Ok(Edn::Str(v.as_str().to_string().into_boxed_str())),
None => Ok(Edn::Str("".to_owned().into_boxed_str())), // TODO maybe nil
}
}
Err(e) => Err(format!("re-find failed, {}", e)),
Expand All @@ -68,7 +68,7 @@ pub fn re_find_all(args: Vec<Edn>) -> Result<Edn, String> {
Ok(p) => {
let mut ys: Vec<Edn> = vec![];
for v in p.find_iter(s) {
ys.push(Edn::Str(v.as_str().to_string()))
ys.push(Edn::Str(v.as_str().to_string().into_boxed_str()))
}
Ok(Edn::List(ys))
}
Expand Down

0 comments on commit ca54cd3

Please sign in to comment.