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