jirust_cli/utils/
json_printer.rs

1use super::PrintableData;
2
3/// Print the data in JSON format
4///
5/// # Arguments
6///
7/// * `data` - The data to be printed
8///     * Project: A vector of Project structs
9///     * Version: A vector of Version structs
10///     * IssueType: A vector of IssueType structs
11///     * IssueTypeFields: A vector of IssueTypeFields structs
12///     * IssueCreated: A vector of CreatedIssue structs
13///     * IssueData: A vector of IssueBean structs
14///     * IssueTransition: A vector of IssueTransition structs
15///     * TransitionedIssue: A vector of TransitionedIssue structs
16///     * VersionRelatedWork: A vector of VersionRelatedWork structs
17///     * Generic: A vector of Generic JSON values
18///
19/// # Example
20///
21/// ```
22/// use jira_v3_openapi::models::Version;
23/// use jirust_cli::utils::{PrintableData, json_printer::print_json};
24///
25/// let versions: Vec<Version> = vec![Version::new()];
26///
27/// print_json(PrintableData::Version { versions });
28/// ```
29pub fn print_json(data: PrintableData) {
30    match data {
31        PrintableData::Generic { data } => {
32            if let Ok(output) = serde_json::to_string_pretty(&data) {
33                println!("{output}");
34            } else {
35                println!("Failed to serialize data");
36            }
37        }
38        PrintableData::IssueTransitions { transitions } => {
39            if let Ok(output) = serde_json::to_string_pretty(&transitions) {
40                println!("{output}");
41            } else {
42                println!("Failed to serialize data");
43            }
44        }
45        PrintableData::IssueCreated { issues } => {
46            if let Ok(output) = serde_json::to_string_pretty(&issues) {
47                println!("{output}");
48            } else {
49                println!("Failed to serialize data");
50            }
51        }
52        PrintableData::IssueData { issues } => {
53            if let Ok(output) = serde_json::to_string_pretty(&issues) {
54                println!("{output}");
55            } else {
56                println!("Failed to serialize data");
57            }
58        }
59        PrintableData::IssueType { issue_types } => {
60            if let Ok(output) = serde_json::to_string_pretty(&issue_types) {
61                println!("{output}");
62            } else {
63                println!("Failed to serialize data");
64            }
65        }
66        PrintableData::IssueTypeField { issue_type_fields } => {
67            if let Ok(output) = serde_json::to_string_pretty(&issue_type_fields) {
68                println!("{output}");
69            } else {
70                println!("Failed to serialize data");
71            }
72        }
73        PrintableData::Project { projects } => {
74            if let Ok(output) = serde_json::to_string_pretty(&projects) {
75                println!("{output}");
76            } else {
77                println!("Failed to serialize data");
78            }
79        }
80        PrintableData::TransitionedIssue { issues } => {
81            let printable = issues
82                .iter()
83                .map(|(issue, transitioned, assigned, fix_version)| {
84                    serde_json::json!({
85                        "issue": issue,
86                        "transitioned": transitioned,
87                        "assigned": assigned,
88                        "fixVersion": fix_version,
89                    })
90                })
91                .collect::<Vec<_>>();
92            if let Ok(output) = serde_json::to_string_pretty(&printable) {
93                println!("{output}");
94            } else {
95                println!("Failed to serialize data");
96            }
97        }
98        PrintableData::Version { versions } => {
99            if let Ok(output) = serde_json::to_string_pretty(&versions) {
100                println!("{output}");
101            } else {
102                println!("Failed to serialize data");
103            }
104        }
105        PrintableData::VersionRelatedWork {
106            version_related_work_items,
107        } => {
108            if let Ok(output) = serde_json::to_string_pretty(&version_related_work_items) {
109                println!("{output}");
110            } else {
111                println!("Failed to serialize data");
112            }
113        }
114    }
115}