Add json-stream export format

This commit is contained in:
Joscha 2023-02-12 00:53:50 +01:00
parent ba1b8b419c
commit 0ceaffc608
3 changed files with 37 additions and 2 deletions

View file

@ -16,6 +16,7 @@ Procedure when bumping the version number:
### Added
- `--verbose` flag
- `json-stream` room export format
### Changed
- Respect colon-delimited emoji when calculating nick hue

View file

@ -12,8 +12,10 @@ use crate::vault::EuphVault;
pub enum Format {
/// Human-readable tree-structured messages.
Text,
/// List of message objects in the same format as the euphoria API uses.
/// Array of message objects in the same format as the euphoria API uses.
Json,
/// Message objects in the same format as the euphoria API uses, one per line.
JsonStream,
}
impl Format {
@ -21,13 +23,14 @@ impl Format {
match self {
Self::Text => "text",
Self::Json => "json",
Self::JsonStream => "json stream",
}
}
fn extension(&self) -> &'static str {
match self {
Self::Text => "txt",
Self::Json => "json",
Self::Json | Self::JsonStream => "json",
}
}
}
@ -88,6 +91,7 @@ pub async fn export(vault: &EuphVault, mut args: Args) -> anyhow::Result<()> {
match args.format {
Format::Text => text::export_to_file(&vault, &mut file).await?,
Format::Json => json::export_to_file(&vault, &mut file).await?,
Format::JsonStream => json::export_stream_to_file(&vault, &mut file).await?,
}
file.flush()?;
}

View file

@ -42,3 +42,33 @@ pub async fn export_to_file(
Ok(())
}
pub async fn export_stream_to_file(
vault: &EuphRoomVault,
file: &mut BufWriter<File>,
) -> anyhow::Result<()> {
let mut total = 0;
let mut offset = 0;
loop {
let messages = vault.chunk_at_offset(CHUNK_SIZE, offset).await;
offset += messages.len();
if messages.is_empty() {
break;
}
for message in messages {
serde_json::to_writer(&mut *file, &message)?; // Fancy reborrow! :D
writeln!(file)?;
total += 1;
}
if total % 100000 == 0 {
println!(" {total} messages");
}
}
println!(" {total} messages in total");
Ok(())
}