mielin_cli/types/
operation.rs

1//! Operation result data structures
2
3use crate::output::MultiFormatDisplay;
4use comfy_table::{presets::UTF8_FULL, Cell, Color, ContentArrangement, Table};
5use serde::Serialize;
6
7#[derive(Debug, Serialize)]
8pub struct OperationResult {
9    pub success: bool,
10    pub message: String,
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub id: Option<String>,
13}
14
15impl MultiFormatDisplay for OperationResult {
16    fn to_table(&self) -> Table {
17        let mut table = Table::new();
18        table
19            .load_preset(UTF8_FULL)
20            .set_content_arrangement(ContentArrangement::Dynamic);
21
22        let status_color = if self.success {
23            Color::Green
24        } else {
25            Color::Red
26        };
27        let status = if self.success { "Success" } else { "Failed" };
28
29        table.add_row(vec![
30            Cell::new("Status").fg(Color::Cyan),
31            Cell::new(status).fg(status_color),
32        ]);
33        table.add_row(vec![
34            Cell::new("Message").fg(Color::Cyan),
35            Cell::new(&self.message),
36        ]);
37        if let Some(ref id) = self.id {
38            table.add_row(vec![Cell::new("ID").fg(Color::Cyan), Cell::new(id)]);
39        }
40
41        table
42    }
43
44    fn to_quiet(&self) -> String {
45        if self.success {
46            self.id.clone().unwrap_or_default()
47        } else {
48            String::new()
49        }
50    }
51}