jirust_cli/
utils.rs

1//! Shared utilities for file security scanning, printing, formatting, and changelog handling.
2/// Utilities for scanning attachment files for malware
3#[cfg(feature = "attachment_scan")]
4pub mod cached_scanner;
5/// Utilities to extract release notes from changelog files.
6pub mod changelog_extractor;
7/// JSON output helpers.
8pub mod json_printer;
9/// Reusable macros.
10pub mod macros;
11/// Pretty-table renderers.
12pub mod table_printer;
13
14use jira_v3_openapi::models::{
15    CreatedIssue, FieldCreateMetadata, IssueBean, IssueTransition, IssueTypeIssueCreateMetadata,
16    Project, Version, VersionRelatedWork,
17};
18use serde::Serialize;
19use serde_json::Value;
20
21use crate::args::commands::{OutputTypes, OutputValues};
22
23/// Enum to hold the different types of data that can be printed in a table
24///
25/// # Variants
26///
27/// * `IssueType` - Jira Issue types available in a project data
28/// * `IssueTypeField` - Fields available for a specific issue type in a project data
29/// * `Project` - Projects available in Jira data
30/// * `Version` - Versions available in a project data
31/// * `IssueCreated` - Issues created in a project data
32/// * `IssueTransition` - Issues transitions in a project data
33/// * `TransitionedIssue` - Issues transitioned data
34/// * `VersionRelatedWork` - Version related work items data
35#[derive(Serialize)]
36pub enum PrintableData {
37    /// Generic JSON data returned from Jira endpoints.
38    Generic {
39        /// Raw JSON payloads to print.
40        data: Vec<Value>,
41    },
42    /// Issues created by a command.
43    IssueCreated {
44        /// Created issue payloads.
45        issues: Vec<CreatedIssue>,
46    },
47    /// Issue data retrieved from Jira.
48    IssueData {
49        /// Issues to print.
50        issues: Vec<IssueBean>,
51    },
52    /// Available transitions for an issue.
53    IssueTransitions {
54        /// Transition details.
55        transitions: Vec<IssueTransition>,
56    },
57    /// Issue types associated to a project.
58    IssueType {
59        /// Project issue types.
60        issue_types: Vec<IssueTypeIssueCreateMetadata>,
61    },
62    /// Fields for a given issue type.
63    IssueTypeField {
64        /// Field metadata grouped by issue type.
65        issue_type_fields: Vec<FieldCreateMetadata>,
66    },
67    /// Jira projects.
68    Project {
69        /// Project list to print.
70        projects: Vec<Project>,
71    },
72    /// Issues transitioned by a workflow automation.
73    TransitionedIssue {
74        /// Tuple of (issue key, transition, assignee, fixVersion).
75        issues: Vec<(String, String, String, String)>,
76    },
77    /// Versions associated to a project.
78    Version {
79        /// Version list to print.
80        versions: Vec<Version>,
81    },
82    /// Work items related to a version.
83    VersionRelatedWork {
84        /// Related work entries.
85        version_related_work_items: Vec<VersionRelatedWork>,
86    },
87}
88
89/// Output verbosity level used when printing table data.
90#[derive(Clone)]
91pub enum OutputType {
92    /// Print all available columns.
93    Full,
94    /// Print a subset of useful columns.
95    Basic,
96    /// Print only a single row payload.
97    Single,
98}
99
100impl From<OutputTypes> for OutputType {
101    fn from(output_type: OutputTypes) -> Self {
102        match output_type {
103            OutputTypes::Full => OutputType::Full,
104            OutputTypes::Basic => OutputType::Basic,
105            OutputTypes::Single => OutputType::Single,
106        }
107    }
108}
109
110/// Prints the given data using the requested format and verbosity.
111///
112/// # Arguments
113/// * `data` - Data container to print.
114/// * `output_format` - Output format (table or json).
115/// * `output_type` - Verbosity level for table output.
116pub fn print_data(data: PrintableData, output_format: OutputValues, output_type: OutputType) {
117    match output_format {
118        OutputValues::Json => {
119            json_printer::print_json(data);
120        }
121        OutputValues::Table => match output_type {
122            OutputType::Full => {
123                table_printer::print_table_full(data);
124            }
125            OutputType::Basic => {
126                table_printer::print_table_basic(data);
127            }
128            OutputType::Single => {
129                table_printer::print_table_single(data);
130            }
131        },
132    }
133}