Skip to main content

jira_cli/api/
mod.rs

1pub mod client;
2pub mod types;
3
4pub use client::JiraClient;
5pub use types::*;
6
7use std::fmt;
8
9/// Authentication method used when connecting to Jira.
10///
11/// `Basic` uses HTTP Basic auth with email and API token (Jira Cloud default).
12/// `Pat` uses a Bearer token (Personal Access Token), typically for Jira Data Center / Server.
13#[derive(Debug, Clone, PartialEq, Default)]
14pub enum AuthType {
15    #[default]
16    Basic,
17    Pat,
18}
19
20#[derive(Debug)]
21pub enum ApiError {
22    /// Bad credentials or forbidden.
23    Auth(String),
24    /// Resource not found.
25    NotFound(String),
26    /// Invalid user input (bad key format, missing required value, etc.).
27    InvalidInput(String),
28    /// HTTP 429 rate limit.
29    RateLimit,
30    /// Non-2xx response from the Jira API.
31    Api { status: u16, message: String },
32    /// Network / TLS error.
33    Http(reqwest::Error),
34    /// Any other error.
35    Other(String),
36}
37
38impl fmt::Display for ApiError {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            ApiError::Auth(msg) => write!(
42                f,
43                "Authentication failed: {msg}\nCheck JIRA_TOKEN or run `jira config show` to verify credentials."
44            ),
45            ApiError::NotFound(msg) => write!(f, "Not found: {msg}"),
46            ApiError::InvalidInput(msg) => write!(f, "Invalid input: {msg}"),
47            ApiError::RateLimit => write!(f, "Rate limited by Jira. Please wait and try again."),
48            ApiError::Api { status, message } => write!(f, "API error {status}: {message}"),
49            ApiError::Http(e) => write!(f, "HTTP error: {e}"),
50            ApiError::Other(msg) => write!(f, "{msg}"),
51        }
52    }
53}
54
55impl std::error::Error for ApiError {
56    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
57        match self {
58            ApiError::Http(e) => Some(e),
59            _ => None,
60        }
61    }
62}
63
64impl From<reqwest::Error> for ApiError {
65    fn from(e: reqwest::Error) -> Self {
66        ApiError::Http(e)
67    }
68}