From 01709b24adb7c3a1cb92ead8b0cf07cda4618054 Mon Sep 17 00:00:00 2001 From: tiye Date: Tue, 4 Jul 2023 13:23:09 +0800 Subject: [PATCH] new API read-file-by-line! ; tag 0.1.1 --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 2 ++ calcit.cirru | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++- compact.cirru | 11 ++++++++-- src/fs.rs | 47 +++++++++++++++++++++++++++++++++++++++- 6 files changed, 117 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eaba91a..b74d6d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -31,7 +31,7 @@ checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" [[package]] name = "calcit_std" -version = "0.0.20" +version = "0.1.1" dependencies = [ "chrono", "cirru_edn", diff --git a/Cargo.toml b/Cargo.toml index ee2d7e1..e2bc8df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "calcit_std" -version = "0.0.20" +version = "0.1.1" authors = ["jiyinyiyong "] edition = "2021" diff --git a/README.md b/README.md index 357d88e..ade0704 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ calcit.std.fs/create-dir! path calcit.std.fs/create-dir-all! path calcit.std.fs/rename! from to + +calcit.std.fs/read-file-by-line! a $ fn (line) (println line) ``` ```cirru diff --git a/calcit.cirru b/calcit.cirru index 5b2ceb1..68faf6e 100644 --- a/calcit.cirru +++ b/calcit.cirru @@ -1,6 +1,6 @@ {} - :configs $ {} (:init-fn |calcit.std.test/main!) (:port 6001) (:reload-fn |calcit.std.test/reload!) (:version |0.1.0) + :configs $ {} (:init-fn |calcit.std.test/main!) (:port 6001) (:reload-fn |calcit.std.test/reload!) (:version |0.1.1) :modules $ [] :entries $ {} :ir $ {} (:package |calcit.std) @@ -472,6 +472,24 @@ |j $ {} (:at 1635220825098) (:by |u0) (:text "|\"/dylibs/libcalcit_std") (:type :leaf) |n $ {} (:at 1630176304763) (:by |u0) (:text "|\"read_file") (:type :leaf) |r $ {} (:at 1630171465347) (:by |u0) (:text |name) (:type :leaf) + |read-file-by-line! $ {} (:at 1688447271554) (:by |u0) (:type :expr) + :data $ {} + |T $ {} (:at 1688447271554) (:by |u0) (:text |defn) (:type :leaf) + |b $ {} (:at 1688447422938) (:by |u0) (:text |read-file-by-line!) (:type :leaf) + |h $ {} (:at 1688447278540) (:by |u0) (:type :expr) + :data $ {} + |T $ {} (:at 1688447278540) (:by |u0) (:text |name) (:type :leaf) + |b $ {} (:at 1688447303653) (:by |u0) (:text |cb) (:type :leaf) + |l $ {} (:at 1688447278540) (:by |u0) (:type :expr) + :data $ {} + |T $ {} (:at 1688448703272) (:by |u0) (:text |&blocking-dylib-edn-fn) (:type :leaf) + |b $ {} (:at 1688447278540) (:by |u0) (:type :expr) + :data $ {} + |T $ {} (:at 1688447278540) (:by |u0) (:text |get-dylib-path) (:type :leaf) + |b $ {} (:at 1688447278540) (:by |u0) (:text "|\"/dylibs/libcalcit_std") (:type :leaf) + |h $ {} (:at 1688447428492) (:by |u0) (:text "|\"read_file_by_line") (:type :leaf) + |l $ {} (:at 1688447278540) (:by |u0) (:text |name) (:type :leaf) + |o $ {} (:at 1688447312392) (:by |u0) (:text |cb) (:type :leaf) |rename! $ {} (:at 1636553285707) (:by |u0) (:type :expr) :data $ {} |T $ {} (:at 1636553285707) (:by |u0) (:text |defn) (:type :leaf) @@ -1484,6 +1502,44 @@ |T $ {} (:at 1630176262288) (:by |u0) (:text |read-file!) (:type :leaf) |j $ {} (:at 1630245682315) (:by |u0) (:text "|\"README.md") (:type :leaf) |j $ {} (:at 1633182648511) (:by |u0) (:text |1000) (:type :leaf) + |xT $ {} (:at 1688447533888) (:by |u0) (:type :expr) + :data $ {} + |D $ {} (:at 1688447534508) (:by |u0) (:text |let) (:type :leaf) + |T $ {} (:at 1688447540723) (:by |u0) (:type :expr) + :data $ {} + |T $ {} (:at 1688447541078) (:by |u0) (:type :expr) + :data $ {} + |D $ {} (:at 1688447542675) (:by |u0) (:text |*c) (:type :leaf) + |L $ {} (:at 1688447543653) (:by |u0) (:type :expr) + :data $ {} + |T $ {} (:at 1688447544552) (:by |u0) (:text |atom) (:type :leaf) + |b $ {} (:at 1688447544778) (:by |u0) (:text |0) (:type :leaf) + |b $ {} (:at 1688447547831) (:by |u0) (:type :expr) + :data $ {} + |b $ {} (:at 1688447547831) (:by |u0) (:text |read-file-by-line!) (:type :leaf) + |h $ {} (:at 1688447547831) (:by |u0) (:text "|\"README.md") (:type :leaf) + |l $ {} (:at 1688447547831) (:by |u0) (:type :expr) + :data $ {} + |T $ {} (:at 1688447547831) (:by |u0) (:text |fn) (:type :leaf) + |b $ {} (:at 1688447547831) (:by |u0) (:type :expr) + :data $ {} + |T $ {} (:at 1688447547831) (:by |u0) (:text |line) (:type :leaf) + |h $ {} (:at 1688447547831) (:by |u0) (:type :expr) + :data $ {} + |D $ {} (:at 1688448734708) (:by |u0) (:text |;) (:type :leaf) + |T $ {} (:at 1688447547831) (:by |u0) (:text |println) (:type :leaf) + |b $ {} (:at 1688447547831) (:by |u0) (:text "|\"readling line:") (:type :leaf) + |h $ {} (:at 1688447547831) (:by |u0) (:text |line) (:type :leaf) + |l $ {} (:at 1688447553554) (:by |u0) (:type :expr) + :data $ {} + |T $ {} (:at 1688447555421) (:by |u0) (:text |swap!) (:type :leaf) + |b $ {} (:at 1688447557717) (:by |u0) (:text |*c) (:type :leaf) + |h $ {} (:at 1688447559107) (:by |u0) (:text |inc) (:type :leaf) + |h $ {} (:at 1688447561189) (:by |u0) (:type :expr) + :data $ {} + |T $ {} (:at 1688447561871) (:by |u0) (:text |println) (:type :leaf) + |b $ {} (:at 1688447562879) (:by |u0) (:text "|\"lines") (:type :leaf) + |h $ {} (:at 1688448482279) (:by |u0) (:text |@*c) (:type :leaf) |y $ {} (:at 1630219294072) (:by |u0) (:type :expr) :data $ {} |T $ {} (:at 1630219294943) (:by |u0) (:text |println) (:type :leaf) @@ -1591,6 +1647,7 @@ |yj $ {} (:at 1636559452607) (:by |u0) (:text |check-write-file!) (:type :leaf) |z $ {} (:at 1672681420295) (:by |u0) (:text |walk-dir!) (:type :leaf) |zD $ {} (:at 1673543378088) (:by |u0) (:text |glob!) (:type :leaf) + |zP $ {} (:at 1688447406097) (:by |u0) (:text |read-file-by-line!) (:type :leaf) |v $ {} (:at 1630234432423) (:by |u0) (:type :expr) :data $ {} |T $ {} (:at 1630234435928) (:by |u0) (:text |calcit.std.process) (:type :leaf) diff --git a/compact.cirru b/compact.cirru index 5c5715e..69289a3 100644 --- a/compact.cirru +++ b/compact.cirru @@ -1,6 +1,6 @@ {} (:package |calcit.std) - :configs $ {} (:init-fn |calcit.std.test/main!) (:reload-fn |calcit.std.test/reload!) (:version |0.1.0) + :configs $ {} (:init-fn |calcit.std.test/main!) (:reload-fn |calcit.std.test/reload!) (:version |0.1.1) :modules $ [] :entries $ {} :files $ {} @@ -74,6 +74,9 @@ |read-file! $ quote defn read-file! (name) &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"read_file" name + |read-file-by-line! $ quote + defn read-file-by-line! (name cb) + &blocking-dylib-edn-fn (get-dylib-path "\"/dylibs/libcalcit_std") "\"read_file_by_line" name cb |rename! $ quote defn rename! (from to) &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"rename_path" from to @@ -232,6 +235,10 @@ println $ > count $ read-file! "\"README.md" , 1000 + let + *c $ atom 0 + read-file-by-line! "\"README.md" $ fn (line) (; println "\"readling line:" line) (swap! *c inc) + println "\"lines" @*c println (path-exists? "\"README.md") (path-exists? "\"build.js") println $ read-dir! "\"./" println "\"dirs:" $ execute! ([] "\"ls") @@ -245,7 +252,7 @@ :ns $ quote ns calcit.std.test.fs $ :require calcit.std.$meta :refer $ calcit-filename calcit-dirname - 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.fs :refer $ read-file! append-file! write-file! path-exists? read-dir! create-dir! create-dir-all! rename! check-write-file! walk-dir! glob! read-file-by-line! calcit.std.process :refer $ execute! |calcit.std.test.json $ {} :defs $ {} diff --git a/src/fs.rs b/src/fs.rs index 9d177b2..1b473f4 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -2,8 +2,11 @@ use cirru_edn::Edn; use glob::glob; -use std::fs; +use std::fs::File; +use std::io::{self, BufRead}; use std::path::Path; +use std::sync::Arc; +use std::{fs, vec}; use walkdir::WalkDir; #[no_mangle] @@ -23,6 +26,48 @@ pub fn read_file(args: Vec) -> Result { } } +// The output is wrapped in a Result to allow matching on errors +// Returns an Iterator to the Reader of the lines of the file. +fn read_lines

(filename: P) -> io::Result>> +where + P: AsRef, +{ + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} + +#[no_mangle] +pub fn read_file_by_line( + args: Vec, + handler: Arc) -> Result + Send + Sync + 'static>, + finish: Box, +) -> Result { + if args.len() == 1 { + if let Edn::Str(name) = &args[0] { + match read_lines(&**name) { + Ok(lines) => { + // Consumes the iterator, returns an (Optional) String + for line in lines { + if let Ok(ip) = line { + match handler(vec![Edn::str(ip)]) { + Ok(_) => {} + Err(e) => return Err(format!("failed reading line: {}", e)), + } + } + } + finish(); + Ok(Edn::Nil) + } + Err(e) => Err(format!("Failed to read file {name:?}: {e}")), + } + } else { + Err(format!("read-file-by-line expected 1 filename, got {:?}", &args[0])) + } + } else { + Err(format!("read-file-by-line expected 1 argument, got {args:?}")) + } +} + #[no_mangle] pub fn write_file(args: Vec) -> Result { if args.len() == 2 {