Skip to main content

bitbucket_server_rs/api/
build_status.rs

1//! # Build Status Common Types
2//!
3//! This module contains common types and utilities used by the build status API endpoints.
4//! These types are shared between the GET and POST operations for build status.
5
6use serde::{Deserialize, Serialize};
7
8/// Represents the state of a build in Bitbucket Server.
9///
10/// This enum maps to the build status states supported by Bitbucket Server's API.
11/// When serialized, it uses uppercase strings as required by the API.
12#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
13pub enum BuildStatusState {
14    /// The build status is unknown or not set
15    #[default]
16    #[serde(rename = "UNKNOWN")]
17    Unknown,
18    
19    /// The build completed successfully
20    #[serde(rename = "SUCCESSFUL")]
21    Successful,
22    
23    /// The build failed
24    #[serde(rename = "FAILED")]
25    Failed,
26    
27    /// The build is currently in progress
28    #[serde(rename = "INPROGRESS")]
29    InProgress,
30    
31    /// The build was cancelled
32    #[serde(rename = "CANCELLED")]
33    Cancelled,
34}
35
36impl From<String> for BuildStatusState {
37    /// Converts a string to a BuildStatusState.
38    ///
39    /// # Arguments
40    ///
41    /// * `value` - The string representation of the build status state
42    ///
43    /// # Returns
44    ///
45    /// The corresponding BuildStatusState, or Unknown if the string doesn't match any known state
46    fn from(value: String) -> Self {
47        match value.as_str() {
48            "UNKNOWN" => BuildStatusState::Unknown,
49            "SUCCESSFUL" => BuildStatusState::Successful,
50            "FAILED" => BuildStatusState::Failed,
51            "INPROGRESS" => BuildStatusState::InProgress,
52            "CANCELLED" => BuildStatusState::Cancelled,
53            _ => BuildStatusState::Unknown,
54        }
55    }
56}
57
58/// Represents test results associated with a build.
59///
60/// This struct contains counts of test results in different states.
61#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
62pub struct TestResults {
63    /// Number of failed tests
64    pub failed: u32,
65    
66    /// Number of successful tests
67    pub successful: u32,
68    
69    /// Number of skipped tests
70    pub skipped: u32,
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn it_can_convert_string_to_state() {
79        let state = BuildStatusState::from("SUCCESSFUL".to_string());
80        assert_eq!(state, BuildStatusState::Successful);
81
82        let state = BuildStatusState::from("FAILED".to_string());
83        assert_eq!(state, BuildStatusState::Failed);
84
85        let state = BuildStatusState::from("INPROGRESS".to_string());
86        assert_eq!(state, BuildStatusState::InProgress);
87
88        let state = BuildStatusState::from("CANCELLED".to_string());
89        assert_eq!(state, BuildStatusState::Cancelled);
90
91        let state = BuildStatusState::from("UNKNOWN".to_string());
92        assert_eq!(state, BuildStatusState::Unknown);
93
94        let state = BuildStatusState::from("InVaLiD".to_string());
95        assert_eq!(state, BuildStatusState::Unknown);
96    } // end of it_can_convert_string_to_state
97
98}