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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use serde::Serialize;
use serde_json::{json, Value};
use std::{ops::ControlFlow, time::Duration};
use tokio::{
    io::{AsyncBufReadExt, BufReader},
    select, spawn,
    sync::mpsc,
    time::sleep,
};
use tokio_graceful_shutdown::SubsystemHandle;
use worterbuch_client::{Err, Key, KeyValuePair, LsState, PState, ServerMessage as SM, State};

pub async fn next_item<T>(rx: &mut mpsc::Receiver<T>, done: bool) -> Option<T> {
    if done {
        sleep(Duration::from_secs(10)).await;
        None
    } else {
        rx.recv().await
    }
}

pub fn provide_keys(keys: Option<Vec<String>>, subsys: SubsystemHandle) -> mpsc::Receiver<String> {
    let (tx, rx) = mpsc::channel(1);

    if let Some(keys) = keys {
        spawn(async move {
            for key in keys {
                if tx.send(key).await.is_err() {
                    break;
                }
            }
            drop(tx);
        });
    } else {
        spawn(async move {
            let mut lines = BufReader::new(tokio::io::stdin()).lines();
            loop {
                select! {
                    _ = subsys.on_shutdown_requested() => break,
                    recv = lines.next_line() => if let Ok(Some(key)) = recv {
                        if tx.send(key).await.is_err() {
                            break;
                        }
                    } else {
                        break;
                    }
                }
            }
        });
    }

    rx
}

pub fn provide_values(json: bool, subsys: SubsystemHandle) -> mpsc::Receiver<Value> {
    let (tx, rx) = mpsc::channel(1);

    spawn(async move {
        let mut lines = BufReader::new(tokio::io::stdin()).lines();
        loop {
            select! {
                _ = subsys.on_shutdown_requested() => break,
                recv = lines.next_line() => if let Ok(Some(line)) = recv {
                    if json {
                        match serde_json::from_str::<Value>(&line) {
                            Ok(value) => {
                                if tx.send(value).await.is_err() {
                                    break;
                                }
                            }
                            Err(e) => {
                                eprintln!("Error parsing json: {e}");
                            }
                        }
                    } else {
                        if tx.send(json!(line)).await.is_err() {
                            break;
                        }
                    }
                } else {
                    break;
                }
            }
        }
    });

    rx
}

pub fn provide_key_value_pairs(
    key_value_pairs: Option<Vec<String>>,
    json: bool,
    subsys: SubsystemHandle,
) -> mpsc::Receiver<(Key, Value)> {
    let (tx, rx) = mpsc::channel(1);

    if let Some(key_value_pairs) = key_value_pairs {
        spawn(async move {
            for kvp in key_value_pairs {
                if let ControlFlow::Break(_) = provide_key_value_pair(json, kvp, &tx).await {
                    break;
                }
            }
        });
    } else {
        spawn(async move {
            let mut lines = BufReader::new(tokio::io::stdin()).lines();
            loop {
                select! {
                    _ = subsys.on_shutdown_requested() => break,
                    recv = lines.next_line() => if let Ok(Some(kvp)) = recv {
                        if let ControlFlow::Break(_) = provide_key_value_pair(json, kvp, &tx).await {
                            break;
                        }
                    } else {
                        break;
                    }
                }
            }
        });
    }

    rx
}

async fn provide_key_value_pair(
    json: bool,
    kvp: String,
    tx: &mpsc::Sender<(String, Value)>,
) -> ControlFlow<()> {
    if json {
        match serde_json::from_str::<KeyValuePair>(&kvp) {
            Ok(KeyValuePair { key, value }) => {
                if tx.send((key, value)).await.is_err() {
                    return ControlFlow::Break(());
                }
            }
            Err(e) => {
                eprintln!("Error parsing json: {e}");
            }
        }
    } else {
        if let Some(index) = kvp.find('=') {
            let key = kvp[..index].to_owned();
            let value = kvp[index + 1..].to_owned();
            if tx.send((key, json!(value))).await.is_err() {
                return ControlFlow::Break(());
            }
        } else {
            eprintln!("no key/value pair (e.g. 'a=b'): {}", kvp);
        }
    }
    ControlFlow::Continue(())
}

pub fn print_message(msg: &SM, json: bool) {
    match msg {
        SM::PState(msg) => print_pstate(&msg, json),
        SM::State(msg) => print_state(&msg, json),
        SM::Err(msg) => print_err(&msg, json),
        SM::LsState(msg) => print_ls(&msg, json),
        _ => (),
    }
}

fn print_pstate(msg: &PState, json: bool) {
    if json {
        print_msg_as_json(&msg);
    } else {
        println!("{msg}");
    }
}

fn print_state(msg: &State, json: bool) {
    if json {
        print_msg_as_json(&msg);
    } else {
        println!("{msg}");
    }
}

fn print_ls(msg: &LsState, json: bool) {
    if json {
        print_msg_as_json(&msg);
    } else {
        println!("{msg}");
    }
}

fn print_err(msg: &Err, json: bool) {
    if json {
        print_msg_as_json(&msg);
    } else {
        eprintln!("{msg}");
    }
}

fn print_msg_as_json(msg: impl Serialize) {
    match serde_json::to_string(&msg) {
        Ok(json) => println!("{json}"),
        Err(e) => {
            eprintln!("Error converting message to json: {e}");
        }
    }
}