1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
/********************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
use std::error::Error;
pub use crate::up_core_api::ucode::UCode;
pub use crate::up_core_api::ustatus::UStatus;
impl UStatus {
/// Creates a status representing a success.
///
/// # Examples
///
/// ```rust
/// use up_rust::{UCode, UStatus};
///
/// let status = UStatus::ok();
/// assert_eq!(status.code.unwrap(), UCode::OK);
/// ```
pub fn ok() -> Self {
UStatus {
code: UCode::OK.into(),
..Default::default()
}
}
/// Creates a status representing a failure.
///
/// # Examples
///
/// ```rust
/// use up_rust::UStatus;
///
/// let status = UStatus::fail("something went wrong");
/// assert_eq!(status.message.unwrap(), "something went wrong");
/// ```
pub fn fail<M: Into<String>>(msg: M) -> Self {
UStatus {
code: UCode::UNKNOWN.into(),
message: Some(msg.into()),
..Default::default()
}
}
/// Creates a status representing a failure.
///
/// # Examples
///
/// ```rust
/// use up_rust::{UCode, UStatus};
///
/// let status = UStatus::fail_with_code(UCode::DATA_LOSS, "something went wrong");
/// assert_eq!(status.code.unwrap(), UCode::DATA_LOSS);
/// assert_eq!(status.message.unwrap(), "something went wrong");
/// ```
pub fn fail_with_code<M: Into<String>>(code: UCode, msg: M) -> Self {
UStatus {
code: code.into(),
message: Some(msg.into()),
..Default::default()
}
}
/// Checks if this status represents a failure.
///
/// # Examples
///
/// ```rust
/// use up_rust::UStatus;
///
/// let failed_status = UStatus::fail("something went wrong");
/// assert!(failed_status.is_failed());
///
/// let succeeded_status = UStatus::ok();
/// assert!(!succeeded_status.is_failed());
/// ```
pub fn is_failed(&self) -> bool {
self.get_code() != UCode::OK
}
/// Checks if this status represents a success.
///
/// # Examples
///
/// ```rust
/// use up_rust::UStatus;
///
/// let succeeded_status = UStatus::ok();
/// assert!(succeeded_status.is_success());
///
/// let failed_status = UStatus::fail("something went wrong");
/// assert!(!failed_status.is_success());
/// ```
pub fn is_success(&self) -> bool {
self.get_code() == UCode::OK
}
/// Gets this status' error message.
///
/// # Returns
///
/// an empty string if this instance has been created without a message,
/// i.e. not using one of its factory functions.
///
/// # Examples
///
/// ```rust
/// use up_rust::UStatus;
///
/// let failed_status = UStatus::fail("my error message");
/// assert_eq!(failed_status.get_message(), "my error message");
///
/// let succeeded_status = UStatus::ok();
/// assert!(succeeded_status.get_message().is_empty());
/// ```
pub fn get_message(&self) -> String {
match self.message.as_ref() {
Some(msg) => msg.to_owned(),
None => String::default(),
}
}
/// Gets this status' error code.
///
/// # Returns
///
/// [`UCode::UNKNOWN`] if this status has been created without providing an error code.
///
/// # Examples
///
/// ```rust
/// use up_rust::{UCode, UStatus};
///
/// let status = UStatus::fail("my error message");
/// assert_eq!(status.get_code(), UCode::UNKNOWN);
///
/// let status_with_code = UStatus::fail_with_code(UCode::INTERNAL, "my error message");
/// assert_eq!(status_with_code.get_code(), UCode::INTERNAL);
/// ```
pub fn get_code(&self) -> UCode {
self.code.enum_value_or_default()
}
}
impl Error for UStatus {}
#[cfg(test)]
mod tests {
use super::*;
use protobuf::{Enum, EnumOrUnknown};
#[test]
fn test_is_failed() {
assert!(!UStatus {
..Default::default()
}
.is_failed());
UCode::VALUES.iter().for_each(|code| {
let ustatus = UStatus {
code: EnumOrUnknown::from(*code),
..Default::default()
};
assert_eq!(ustatus.is_failed(), *code != UCode::OK);
});
}
#[test]
fn test_is_success() {
assert!(UStatus {
..Default::default()
}
.is_success());
UCode::VALUES.iter().for_each(|code| {
let ustatus = UStatus {
code: EnumOrUnknown::from(*code),
..Default::default()
};
assert_eq!(ustatus.is_success(), *code == UCode::OK);
});
}
}