master
Bel LaPointe 2025-11-11 11:41:07 -07:00
parent 7d36455bad
commit 60ddbc673c
1 changed files with 40 additions and 15 deletions

View File

@ -54,7 +54,7 @@ struct Flags {
}
impl Flags {
fn new() -> Result<Flags, String> {
pub fn new() -> Result<Flags, String> {
let mut result = Flags::parse();
if result.path.get(..1) == Some("$") {
@ -69,7 +69,7 @@ impl Flags {
Ok(result)
}
fn files(&self) -> Result<Files, String> {
pub fn files(&self) -> Result<Files, String> {
let metadata = match std::fs::metadata(self.path.clone()) {
Ok(v) => Ok(v),
Err(msg) => Err(format!("failed to load {}: {}", self.path.clone(), msg)),
@ -119,7 +119,7 @@ struct Files {
}
impl Files {
fn new(files: &Vec<String>) -> Files {
pub fn new(files: &Vec<String>) -> Files {
let mut files = files.clone();
files.sort();
Files {
@ -134,15 +134,15 @@ struct File {
}
impl File {
fn new(file: &String) -> File {
pub fn new(file: &String) -> File {
File { file: file.clone() }
}
fn events(&self) -> Result<Events, String> {
pub fn events(&self) -> Result<Events, String> {
Events::new(&self.file)
}
fn stage_persisted(&self) -> Result<(), String> {
pub fn stage_persisted(&self) -> Result<(), String> {
let stage = self.events()?.snapshot()?;
let plaintext = serde_yaml::to_string(&stage).unwrap();
let mut f = std::fs::File::create(&self.file).expect("failed to open file for writing");
@ -150,7 +150,7 @@ impl File {
Ok(())
}
fn persist_stage(&self) -> Result<(), String> {
pub fn persist_stage(&self) -> Result<(), String> {
let persisted = self.events()?.snapshot()?;
let snapshot = serde_json::to_string(&persisted).unwrap();
let snapshot = snapshot.as_str();
@ -228,6 +228,10 @@ mod test_file {
#[test]
fn test_file() {
let d = tempdir::TempDir::new("").unwrap();
let mut f = std::fs::File::create(d.path().join("plain")).unwrap();
writeln!(f, "hello world").unwrap();
assert!(false, "not impl");
}
}
@ -239,14 +243,14 @@ struct Delta {
}
impl Delta {
fn new(patch: json_patch::PatchOperation, ts: u64) -> Delta {
pub fn new(patch: json_patch::PatchOperation, ts: u64) -> Delta {
Delta {
patch: patch,
ts: ts,
}
}
fn now(patch: json_patch::PatchOperation) -> Delta {
pub fn now(patch: json_patch::PatchOperation) -> Delta {
Self::new(
patch,
std::time::SystemTime::now()
@ -266,7 +270,7 @@ struct Task(serde_yaml::Value);
struct Events(Vec<Delta>);
impl Events {
fn new(file: &String) -> Result<Events, String> {
pub fn new(file: &String) -> Result<Events, String> {
let logs = match std::fs::read_dir(Self::dir(&file)) {
Ok(files) => Ok(files
.filter(|x| x.is_ok())
@ -284,11 +288,13 @@ impl Events {
Ok(f) => {
for line in std::io::BufReader::new(f).lines() {
let line = line.unwrap();
let delta = match serde_json::from_str(&line) {
Ok(v) => Ok(v),
Err(msg) => Err(format!("failed to parse line {}: {}", &line, msg)),
}?;
result.push(delta);
if line.len() > 0 {
let delta = match serde_json::from_str(&line) {
Ok(v) => Ok(v),
Err(msg) => Err(format!("failed to parse line {}: {}", &line, msg)),
}?;
result.push(delta);
}
}
Ok(())
}
@ -345,6 +351,25 @@ mod test_events {
#[test]
fn test_events() {
let d = tempdir::TempDir::new("").unwrap();
test_file(&d, "plain", "- persisted\n- stage only");
test_file(
&d,
".plain.log",
r#"
{"ts":1, "patch":{"op":"replace", "path":"", "value":["persisted"]}}
"#,
);
let events = Events::new(&d.path().join("plain").to_str().unwrap().to_string()).unwrap();
assert_eq!(1, events.0.len(), "events: {:?}", events);
assert!(false, "not impl");
}
}
fn test_file(d: &tempdir::TempDir, fname: &str, content: &str) {
let mut f = std::fs::File::create(d.path().join("plain")).unwrap();
writeln!(f, "- persisted\n- stage only").unwrap();
f.sync_all().unwrap();
}