summaryrefslogtreecommitdiff
path: root/src/export.rs
blob: 75ad782a5ec84697d0832160b0f3425e21ad4519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Export logs from the vault to plain text files.

mod json;
mod text;

use std::fs::File;
use std::io::{BufWriter, Write};

use crate::vault::EuphVault;

#[derive(Debug, Clone, Copy, clap::ValueEnum)]
pub enum Format {
    /// Human-readable tree-structured messages.
    Text,
    /// List of message objects in the same format as the euphoria API uses.
    Json,
}

impl Format {
    fn name(&self) -> &'static str {
        match self {
            Self::Text => "text",
            Self::Json => "json",
        }
    }

    fn extension(&self) -> &'static str {
        match self {
            Self::Text => "txt",
            Self::Json => "json",
        }
    }
}

#[derive(Debug, clap::Parser)]
pub struct Args {
    rooms: Vec<String>,

    /// Export all rooms.
    #[clap(long, short)]
    all: bool,

    /// Format of the output file.
    #[clap(long, short, value_enum, default_value_t = Format::Text)]
    format: Format,

    /// Location of the output file
    ///
    /// May include the following placeholders:
    /// `%r` - room name
    /// `%e` - format extension
    /// A literal `%` can be written as `%%`.
    ///
    /// If the value ends with a `/`, it is assumed to point to a directory and
    /// `%r.%e` will be appended.
    ///
    /// Must be a valid utf-8 encoded string.
    #[clap(long, short, default_value_t = Into::into("%r.%e"))]
    #[clap(verbatim_doc_comment)]
    out: String,
}

pub async fn export(vault: &EuphVault, mut args: Args) -> anyhow::Result<()> {
    if args.out.ends_with('/') {
        args.out.push_str("%r.%e");
    }

    let rooms = if args.all {
        let mut rooms = vault.rooms().await;
        rooms.sort_unstable();
        rooms
    } else {
        let mut rooms = args.rooms.clone();
        rooms.dedup();
        rooms
    };

    if rooms.is_empty() {
        println!("No rooms to export");
    }

    for room in rooms {
        let out = format_out(&args.out, &room, args.format);
        println!("Exporting &{room} as {} to {out}", args.format.name());

        let vault = vault.room(room);
        let mut file = BufWriter::new(File::create(out)?);
        match args.format {
            Format::Text => text::export_to_file(&vault, &mut file).await?,
            Format::Json => json::export_to_file(&vault, &mut file).await?,
        }
        file.flush()?;
    }

    Ok(())
}

fn format_out(out: &str, room: &str, format: Format) -> String {
    let mut result = String::new();

    let mut special = false;
    for char in out.chars() {
        if special {
            match char {
                'r' => result.push_str(room),
                'e' => result.push_str(format.extension()),
                '%' => result.push('%'),
                _ => {
                    result.push('%');
                    result.push(char);
                }
            }
            special = false;
        } else if char == '%' {
            special = true;
        } else {
            result.push(char);
        }
    }

    result
}