fjall_cli/types/
output_kind.rs1use crate::OutputAffixes;
2use errgonomic::handle;
3use fjall::Slice;
4use std::io;
5use std::io::Write;
6use thiserror::Error;
7
8#[derive(clap::ValueEnum, Copy, Clone, Debug, Default)]
9#[clap(rename_all = "kebab")]
10pub enum OutputKind {
11 Key,
12 Value,
13 #[default]
14 KeyValue,
15}
16
17use OutputKind::*;
18
19impl OutputKind {
20 pub fn write(&self, writer: &mut impl Write, key: &Slice, value: &Slice, affixes: &OutputAffixes) -> Result<(), OutputKindWriteError> {
21 use OutputKindWriteError::*;
22 let item_prefix = affixes.item_prefix;
23 let key_prefix = affixes.key_prefix;
24 let value_prefix = affixes.value_prefix;
25 let item_suffix = affixes.item_suffix.as_ref();
26 let key_suffix = affixes.key_suffix.as_ref();
27 let value_suffix = affixes.value_suffix.as_ref();
28 match self {
29 Key => {
30 if let Some(prefix) = item_prefix {
31 let bytes = prefix.write(key);
32 handle!(writer.write_all(&bytes), WriteAllFailed);
33 }
34 if let Some(prefix) = key_prefix {
35 let bytes = prefix.write(key);
36 handle!(writer.write_all(&bytes), WriteAllFailed);
37 }
38 handle!(writer.write_all(key.as_ref()), WriteAllFailed);
39 if let Some(suffix) = key_suffix {
40 handle!(writer.write_all(suffix.as_bytes()), WriteAllFailed);
41 }
42 if let Some(suffix) = item_suffix {
43 handle!(writer.write_all(suffix.as_bytes()), WriteAllFailed);
44 }
45 }
46 Value => {
47 if let Some(prefix) = item_prefix {
48 let bytes = prefix.write(value);
49 handle!(writer.write_all(&bytes), WriteAllFailed);
50 }
51 if let Some(prefix) = value_prefix {
52 let bytes = prefix.write(value);
53 handle!(writer.write_all(&bytes), WriteAllFailed);
54 }
55 handle!(writer.write_all(value.as_ref()), WriteAllFailed);
56 if let Some(suffix) = value_suffix {
57 handle!(writer.write_all(suffix.as_bytes()), WriteAllFailed);
58 }
59 if let Some(suffix) = item_suffix {
60 handle!(writer.write_all(suffix.as_bytes()), WriteAllFailed);
61 }
62 }
63 KeyValue => {
64 if let Some(prefix) = item_prefix {
65 let bytes = prefix.write(key);
66 handle!(writer.write_all(&bytes), WriteAllFailed);
67 }
68 if let Some(prefix) = key_prefix {
69 let bytes = prefix.write(key);
70 handle!(writer.write_all(&bytes), WriteAllFailed);
71 }
72 handle!(writer.write_all(key.as_ref()), WriteAllFailed);
73 if let Some(suffix) = key_suffix {
74 handle!(writer.write_all(suffix.as_bytes()), WriteAllFailed);
75 }
76 if let Some(prefix) = value_prefix {
77 let bytes = prefix.write(value);
78 handle!(writer.write_all(&bytes), WriteAllFailed);
79 }
80 handle!(writer.write_all(value.as_ref()), WriteAllFailed);
81 if let Some(suffix) = value_suffix {
82 handle!(writer.write_all(suffix.as_bytes()), WriteAllFailed);
83 }
84 if let Some(suffix) = item_suffix {
85 handle!(writer.write_all(suffix.as_bytes()), WriteAllFailed);
86 }
87 }
88 }
89 Ok(())
90 }
91}
92
93#[derive(Error, Debug)]
94pub enum OutputKindWriteError {
95 #[error("failed to write output")]
96 WriteAllFailed { source: io::Error },
97}