automerge/rust/automerge-cli/src/examine.rs
Alex Good dd3c6d1303
Move rust workspace into ./rust
After some discussion with PVH I realise that the repo structure in the
last reorg was very rust-centric. In an attempt to put each language on
a level footing move the rust code and project files into ./rust
2022-10-16 19:55:51 +01:00

51 lines
1.5 KiB
Rust

use automerge as am;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ExamineError {
#[error("Error reading change file: {:?}", source)]
ReadingChanges {
#[source]
source: std::io::Error,
},
#[error("Error loading changes: {:?}", source)]
ApplyingInitialChanges {
#[source]
source: am::AutomergeError,
},
#[error("Error writing to output: {:?}", source)]
WritingToOutput {
#[source]
source: std::io::Error,
},
}
pub fn examine(
mut input: impl std::io::Read,
mut output: impl std::io::Write,
is_tty: bool,
) -> Result<(), ExamineError> {
let mut buf: Vec<u8> = Vec::new();
input
.read_to_end(&mut buf)
.map_err(|e| ExamineError::ReadingChanges { source: e })?;
let doc = am::Automerge::load(&buf)
.map_err(|e| ExamineError::ApplyingInitialChanges { source: e })?;
let uncompressed_changes: Vec<_> = doc
.get_changes(&[])
.unwrap()
.iter()
.map(|c| c.decode())
.collect();
if is_tty {
let json_changes = serde_json::to_value(uncompressed_changes).unwrap();
colored_json::write_colored_json(&json_changes, &mut output).unwrap();
writeln!(output).unwrap();
} else {
let json_changes = serde_json::to_string_pretty(&uncompressed_changes).unwrap();
output
.write_all(&json_changes.into_bytes())
.map_err(|e| ExamineError::WritingToOutput { source: e })?;
}
Ok(())
}