surrealcs_kernel/utils/
generic.rs

1//! Houses utils that do not have a home yet.
2use nanoservices_utils::errors::{NanoServiceError, NanoServiceErrorStatus};
3
4/// The error message for when a condition is not met.
5pub static KEY_ALREADY_EXISTS_ERR: &str = "Key already exists";
6
7/// Checks if the key already exists.
8///
9/// # Arguments
10/// * `error`: The error to check
11///
12/// # Returns
13/// * `bool`: True if the condition is not met, false otherwise
14pub fn check_key_already_exists(error: &NanoServiceError) -> bool {
15	error.status == NanoServiceErrorStatus::BadRequest && error.message == KEY_ALREADY_EXISTS_ERR
16}
17
18/// Constructs a condition not met error.
19///
20/// # Returns
21/// * `NanoServiceError`: The condition not met error
22pub fn construct_key_already_exists_error() -> NanoServiceError {
23	NanoServiceError::new(KEY_ALREADY_EXISTS_ERR.to_string(), NanoServiceErrorStatus::BadRequest)
24}
25
26/// The error message for when a condition is not met.
27pub static CONDITION_NOT_MET_ERR: &str = "Condition not met";
28
29/// Checks if the condition is not met.
30///
31/// # Arguments
32/// * `error`: The error to check
33///
34/// # Returns
35/// * `bool`: True if the condition is not met, false otherwise
36pub fn check_condition_not_met(error: &NanoServiceError) -> bool {
37	error.status == NanoServiceErrorStatus::BadRequest && error.message == CONDITION_NOT_MET_ERR
38}
39
40/// Constructs a condition not met error.
41///
42/// # Returns
43/// * `NanoServiceError`: The condition not met error
44pub fn construct_condition_not_met_error() -> NanoServiceError {
45	NanoServiceError::new(CONDITION_NOT_MET_ERR.to_string(), NanoServiceErrorStatus::BadRequest)
46}
47
48#[cfg(test)]
49mod tests {
50
51	use super::*;
52
53	#[test]
54	fn test_check_condition_not_met_true() {
55		let error = NanoServiceError::new(
56			CONDITION_NOT_MET_ERR.to_string(),
57			NanoServiceErrorStatus::BadRequest,
58		);
59		assert!(check_condition_not_met(&error));
60	}
61
62	#[test]
63	fn test_check_condition_not_met_false() {
64		let error = NanoServiceError::new(
65			"Some other error".to_string(),
66			NanoServiceErrorStatus::BadRequest,
67		);
68		assert!(!check_condition_not_met(&error));
69	}
70
71	#[test]
72	fn test_construct_condition_not_met_error() {
73		let error = construct_condition_not_met_error();
74		assert_eq!(error.status, NanoServiceErrorStatus::BadRequest);
75		assert_eq!(error.message, CONDITION_NOT_MET_ERR);
76	}
77}