Skip to content

Commit

Permalink
Merge pull request #24 from calcit-lang/edn
Browse files Browse the repository at this point in the history
upgrade edn to fix
  • Loading branch information
NoEgAm authored Feb 27, 2024
2 parents 1e89f89 + 36f0e07 commit d65e1e5
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 56 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ jobs:

- uses: supplypike/setup-bin@v3
with:
uri: "https://github.com/calcit-lang/calcit/releases/download/0.8.28/cr"
uri: "https://github.com/calcit-lang/calcit/releases/download/0.8.36/cr"
name: "cr"
version: "0.8.28"
version: "0.8.36"

- uses: supplypike/setup-bin@v3
with:
uri: "https://github.com/calcit-lang/calcit/releases/download/0.8.28/caps"
uri: "https://github.com/calcit-lang/calcit/releases/download/0.8.36/caps"
name: "caps"
version: "0.8.28"
version: "0.8.36"

- run: cargo build --release

Expand Down
10 changes: 5 additions & 5 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit_std"
version = "0.2.1"
version = "0.2.2"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2021"

Expand All @@ -17,7 +17,7 @@ crate-type = ["dylib"] # Creates dynamic lib
serde = { version = "1.0.185", features = ["derive"] }
serde_json = "1.0.105"
chrono = "0.4.26"
cirru_edn = "0.5.4"
cirru_edn = "0.6.2"
cirru_parser = "0.1.28"
nanoid = "0.4.0"
rand = "0.8.5"
Expand Down
8 changes: 5 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cargo build --release
rm -rfv dylibs/
mkdir -p dylibs/ && ls target/release/ && cp -v target/release/libcalcit_std.* dylibs/
cargo build --release && \
rm -rfv dylibs && \
mkdir -p dylibs/ && \
ls target/release/ && \
cp -v target/release/libcalcit_std.* dylibs/
56 changes: 31 additions & 25 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::{collections::HashMap, sync::Arc};

use chrono::{DateTime, Datelike, Duration, Local, LocalResult, NaiveDate, TimeZone, Timelike, Utc, Weekday};
use cirru_edn::Edn;
use cirru_edn::{Edn, EdnTupleView};
use std::ops::Add;

/// calcit represents DateTime in f64
Expand Down Expand Up @@ -92,7 +92,7 @@ pub fn extract_time(args: Vec<Edn>) -> Result<Edn, String> {
data.insert(Edn::tag("week"), Edn::Number(time.date_naive().iso_week().week() as f64));
data.insert(Edn::tag("week0"), Edn::Number(time.date_naive().iso_week().week0() as f64));

Ok(Edn::Map(data))
Ok(Edn::from(data))
}
_ => Err(format!("extract-time expected f64 and string, got: {args:?}")),
}
Expand All @@ -114,15 +114,18 @@ pub fn from_ymd(args: Vec<Edn>) -> Result<Edn, String> {
.and_hms_opt(0, 0, 0)
.ok_or("from_ymd got none")?,
) {
LocalResult::None => Ok(Edn::Tuple(Arc::new(Edn::tag("none")), vec![])),
LocalResult::Single(d) => Ok(Edn::Tuple(
Arc::new(Edn::tag("single")),
vec![Edn::Number(d.timestamp_millis() as f64)],
)),
LocalResult::Ambiguous(d, d2) => Ok(Edn::Tuple(
Arc::new(Edn::tag("ambiguous")),
vec![Edn::Number(d.timestamp_millis() as f64), Edn::Number(d2.timestamp_millis() as f64)],
)),
LocalResult::None => Ok(Edn::Tuple(EdnTupleView {
tag: Arc::new(Edn::tag("none")),
extra: vec![],
})),
LocalResult::Single(d) => Ok(Edn::Tuple(EdnTupleView {
tag: Arc::new(Edn::tag("single")),
extra: vec![Edn::Number(d.timestamp_millis() as f64)],
})),
LocalResult::Ambiguous(d, d2) => Ok(Edn::Tuple(EdnTupleView {
tag: Arc::new(Edn::tag("ambiguous")),
extra: vec![Edn::Number(d.timestamp_millis() as f64), Edn::Number(d2.timestamp_millis() as f64)],
})),
}
}
(a, b, c) => Err(format!("from-ymd expected 2 args, got: {a} {b} {c}")),
Expand All @@ -147,23 +150,26 @@ pub fn from_ywd(args: Vec<Edn>) -> Result<Edn, String> {
5 => Weekday::Fri,
6 => Weekday::Sat,
_ => {
return Ok(Edn::Tuple(
Arc::new(Edn::tag("err")),
vec![Edn::str(format!("invalid digit for weekday: {d}"))],
))
return Ok(Edn::Tuple(EdnTupleView {
tag: Arc::new(Edn::tag("err")),
extra: vec![Edn::str(format!("invalid digit for weekday: {d}"))],
}))
}
};
match NaiveDate::from_isoywd_opt(*y as i32, *w as u32, weekday) {
Some(time) => match Local.from_local_datetime(&time.and_hms_opt(0, 0, 0).ok_or("hms got none")?) {
LocalResult::None => Ok(Edn::Tuple(Arc::new(Edn::tag("none")), vec![])),
LocalResult::Single(d) => Ok(Edn::Tuple(
Arc::new(Edn::tag("single")),
vec![Edn::Number(d.timestamp_millis() as f64)],
)),
LocalResult::Ambiguous(d, d2) => Ok(Edn::Tuple(
Arc::new(Edn::tag("single")),
vec![Edn::Number(d.timestamp_millis() as f64), Edn::Number(d2.timestamp_millis() as f64)],
)),
LocalResult::None => Ok(Edn::Tuple(EdnTupleView {
tag: Arc::new(Edn::tag("none")),
extra: vec![],
})),
LocalResult::Single(d) => Ok(Edn::Tuple(EdnTupleView {
tag: Arc::new(Edn::tag("single")),
extra: vec![Edn::Number(d.timestamp_millis() as f64)],
})),
LocalResult::Ambiguous(d, d2) => Ok(Edn::Tuple(EdnTupleView {
tag: Arc::new(Edn::tag("single")),
extra: vec![Edn::Number(d.timestamp_millis() as f64), Edn::Number(d2.timestamp_millis() as f64)],
})),
},
None => Err(format!("from-ywd got invalid args: {y} {w} {weekday}")),
}
Expand All @@ -186,7 +192,7 @@ pub fn add_duration(args: Vec<Edn>) -> Result<Edn, String> {
LocalResult::Ambiguous(min_time, max_time) => return Err(format!("add-duration ambiguous: {min_time} {max_time}")),
};

match &(*k.to_str()) {
match k.ref_str() {
"week" | "weeks" => Ok(Edn::Number(time.add(Duration::weeks(*n as i64)).timestamp_millis() as f64)),
"day" | "days" => Ok(Edn::Number(time.add(Duration::days(*n as i64)).timestamp_millis() as f64)),
"h" | "hour" | "hours" => Ok(Edn::Number(time.add(Duration::hours(*n as i64)).timestamp_millis() as f64)),
Expand Down
6 changes: 3 additions & 3 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub fn read_dir(args: Vec<Edn>) -> Result<Edn, String> {
}
// println!("child dir: {:?}", content);

Ok(Edn::List(content))
Ok(Edn::from(content))
}
Err(e) => Err(format!("Failed to read dir {name:?}: {e}")),
}
Expand Down Expand Up @@ -248,7 +248,7 @@ pub fn walk_dir(args: Vec<Edn>) -> Result<Edn, String> {
content.push(Edn::Str(format!("{}", path.display()).into()));
}
}
Ok(Edn::List(content))
Ok(Edn::from(content))
} else {
Err(format!("walk-dir expected a string, got: {}", &args[0]))
}
Expand All @@ -273,7 +273,7 @@ pub fn glob_call(args: Vec<Edn>) -> Result<Edn, String> {
Err(e) => return Err(format!("Failed to read: {}", e)),
}
}
Ok(Edn::List(content))
Ok(Edn::from(content))
} else {
Err(format!("glob expected a string, got: {}", &args[0]))
}
Expand Down
24 changes: 12 additions & 12 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, EdnTag};
use cirru_edn::{Edn, EdnRecordView, EdnTag};

pub fn json_to_edn(data: &Value) -> Edn {
match data {
Expand All @@ -22,7 +22,7 @@ pub fn json_to_edn(data: &Value) -> Edn {
for x in xs {
ys.push(json_to_edn(x));
}
Edn::List(ys)
Edn::from(ys)
}
Value::Object(xs) => {
let mut ys: HashMap<Edn, Edn> = HashMap::new();
Expand All @@ -34,17 +34,17 @@ pub fn json_to_edn(data: &Value) -> Edn {
};
ys.insert(key, json_to_edn(v));
}
Edn::Map(ys)
Edn::from(ys)
}
}
}

/// option for "add colon to keyword"
pub fn edn_to_json(data: Edn, add_colon: bool) -> Result<Value, String> {
pub fn edn_to_json(data: &Edn, add_colon: bool) -> Result<Value, String> {
match data {
Edn::Nil => Ok(Value::Null),
Edn::Bool(b) => Ok(Value::Bool(b)),
Edn::Number(n) => match serde_json::value::Number::from_f64(n) {
Edn::Bool(b) => Ok(Value::Bool(*b)),
Edn::Number(n) => match serde_json::value::Number::from_f64(*n) {
Some(v) => Ok(Value::Number(v)),
None => Err(format!("failed to convert to number: {n}")),
},
Expand All @@ -53,7 +53,7 @@ pub fn edn_to_json(data: Edn, add_colon: bool) -> Result<Value, String> {
if add_colon {
Ok(Value::String(format!(":{s}")))
} else {
Ok(Value::String((*s.to_str()).to_string()))
Ok(Value::String((*s.ref_str()).to_string()))
}
}
Edn::Str(s) => Ok(Value::String((*s).to_string())),
Expand All @@ -66,7 +66,7 @@ pub fn edn_to_json(data: Edn, add_colon: bool) -> Result<Value, String> {
}
Edn::Map(xs) => {
let mut data = serde_json::Map::new();
for (k, v) in xs {
for (k, v) in &xs.0 {
match k {
Edn::Str(s) => {
data.insert(s.to_string(), edn_to_json(v, add_colon)?);
Expand All @@ -84,10 +84,10 @@ pub fn edn_to_json(data: Edn, add_colon: bool) -> Result<Value, String> {

Ok(Value::Object(data))
}
Edn::Record(_, entries) => {
Edn::Record(EdnRecordView { pairs: entries, .. }) => {
let mut data = serde_json::Map::new();
for entry in entries {
data.insert(entry.0.to_string(), edn_to_json(entry.1.to_owned(), add_colon)?);
data.insert(entry.0.to_string(), edn_to_json(&entry.1, add_colon)?);
}
Ok(Value::Object(data))
}
Expand Down Expand Up @@ -116,14 +116,14 @@ pub fn stringify_json(args: Vec<Edn>) -> Result<Edn, String> {
if args.len() == 2 {
match &args[1] {
Edn::Bool(add_colon) => {
let ret = edn_to_json(args[0].to_owned(), *add_colon)?;
let ret = edn_to_json(&args[0], *add_colon)?;
match serde_json::to_string(&ret) {
Ok(s) => Ok(Edn::Str(s.into())),
Err(e) => Err(format!("failed to generate string: {e}")),
}
}
Edn::Nil => {
let ret = edn_to_json(args[0].to_owned(), false)?;
let ret = edn_to_json(&args[0], false)?;
match serde_json::to_string(&ret) {
Ok(s) => Ok(Edn::Str(s.into())),
Err(e) => Err(format!("failed to generate string: {e}")),
Expand Down
4 changes: 2 additions & 2 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn execute_command(args: Vec<Edn>) -> Result<Edn, String> {
let mut cmd = String::from("");
let mut xs: Vec<String> = vec![];

for (idx, piece) in ys.iter().enumerate() {
for (idx, piece) in ys.0.iter().enumerate() {
if let Edn::Str(s) = piece {
if idx == 0 {
cmd = (**s).to_owned();
Expand All @@ -25,7 +25,7 @@ pub fn execute_command(args: Vec<Edn>) -> Result<Edn, String> {
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.into()), Edn::Str(stderr.into())]))
Ok(Edn::from(vec![Edn::Str(content.into()), Edn::Str(stderr.into())]))
}
Err(e) => Err(format!("Failed to excute: {e}")),
}
Expand Down

0 comments on commit d65e1e5

Please sign in to comment.