can read deltas
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use clap::Parser;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_yaml;
|
||||
use std::io::{Read, Write};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use std::io::{BufRead, Read, Write};
|
||||
|
||||
fn main() {
|
||||
for file in Flags::new()
|
||||
@@ -11,7 +11,7 @@ fn main() {
|
||||
.files
|
||||
.iter()
|
||||
{
|
||||
file.reconcile_snapshot_changes().unwrap();
|
||||
file.persist_stage().unwrap();
|
||||
println!(
|
||||
"{} => {:?}",
|
||||
file.file,
|
||||
@@ -100,8 +100,9 @@ impl File {
|
||||
Events::new(&self.file)
|
||||
}
|
||||
|
||||
fn stash_staged_changes(&self, stashed: Vec<Task>) -> Result<(), String> {
|
||||
let snapshot = serde_json::to_string(&stashed).unwrap();
|
||||
fn persist_stage(&self) -> Result<(), String> {
|
||||
let persisted = self.events()?.snapshot();
|
||||
let snapshot = serde_json::to_string(&persisted).unwrap();
|
||||
let snapshot: serde_json::Value = serde_json::from_str(snapshot.as_str()).unwrap();
|
||||
|
||||
let stage = self.snapshot()?;
|
||||
@@ -109,12 +110,13 @@ impl File {
|
||||
let stage: serde_json::Value = serde_json::from_str(stage.as_str()).unwrap();
|
||||
|
||||
let patches = json_patch::diff(&snapshot, &stage);
|
||||
let deltas: Vec<Delta> = patches.iter()
|
||||
let deltas: Vec<Delta> = patches
|
||||
.iter()
|
||||
.map(|patch| patch.clone())
|
||||
.map(|patch| Delta::now(patch.clone()))
|
||||
.collect();
|
||||
for delta in deltas.iter() {
|
||||
self.append(serde_json::to_string(delta).unwrap())?;
|
||||
self.append(delta.clone())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -146,17 +148,23 @@ impl File {
|
||||
fn append(&self, delta: Delta) -> Result<(), String> {
|
||||
use std::fs::OpenOptions;
|
||||
let hostname = gethostname::gethostname();
|
||||
let log = format!("{}{}", Events::log_prefix(&self.file), gethostname::gethostname().into_string().unwrap());
|
||||
let mut file = match OpenOptions::new().write(true).append(true).open(&log) {
|
||||
assert!(hostname.len() > 0, "empty hostname");
|
||||
let log = format!(
|
||||
"{}{}",
|
||||
Events::log_prefix(&self.file),
|
||||
hostname.into_string().unwrap()
|
||||
);
|
||||
let mut file = match OpenOptions::new()
|
||||
.write(true)
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(&log)
|
||||
{
|
||||
Ok(f) => Ok(f),
|
||||
Err(msg) => Err(format!(
|
||||
"failed to open {} for appending: {}",
|
||||
&self.file, msg
|
||||
)),
|
||||
Err(msg) => Err(format!("failed to open {} for appending: {}", &log, msg)),
|
||||
}?;
|
||||
let line = serde_json::to_string(&delta).unwrap();
|
||||
match
|
||||
writeln!(file, "{}", line) {
|
||||
match writeln!(file, "{}", line) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(msg) => Err(format!("failed to append: {}", msg)),
|
||||
}
|
||||
@@ -164,22 +172,30 @@ impl File {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
struct Delta{
|
||||
ts: u64,
|
||||
patch: json_patch::PatchOperation,
|
||||
struct Delta {
|
||||
ts: u64,
|
||||
patch: json_patch::PatchOperation,
|
||||
}
|
||||
|
||||
impl Delta {
|
||||
fn new(patch: json_patch::PatchOperation, ts: u64) -> Delta {
|
||||
Delta{
|
||||
patch: patch,
|
||||
ts: ts,
|
||||
}
|
||||
}
|
||||
fn new(patch: json_patch::PatchOperation, ts: u64) -> Delta {
|
||||
Delta {
|
||||
patch: patch,
|
||||
ts: ts,
|
||||
}
|
||||
}
|
||||
|
||||
fn now(patch: json_patch::PatchOperation) -> Delta {
|
||||
Self::new(patch, std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs().try_into().unwrap())
|
||||
}
|
||||
fn now(patch: json_patch::PatchOperation) -> Delta {
|
||||
Self::new(
|
||||
patch,
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs()
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
@@ -201,10 +217,24 @@ impl Events {
|
||||
Err(msg) => Err(format!("failed to read dir {}: {}", Self::dir(&file), msg)),
|
||||
}?;
|
||||
|
||||
let mut result = vec![];
|
||||
let mut result: Vec<Delta> = vec![];
|
||||
for log in logs.iter() {
|
||||
panic!("{:?}", log);
|
||||
match std::fs::File::open(&log) {
|
||||
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);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Err(msg) => Err(format!("failed to read {}: {}", &log, msg)),
|
||||
}?;
|
||||
}
|
||||
result.sort_by(|a, b| a.ts.cmp(&b.ts));
|
||||
Ok(Events(result))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user