surrealcs_kernel/utils/
generic.rs

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
//! Houses utils that do not have a home yet.
use nanoservices_utils::errors::{NanoServiceError, NanoServiceErrorStatus};

/// The error message for when a condition is not met.
pub static KEY_ALREADY_EXISTS_ERR: &str = "Key already exists";

/// Checks if the key already exists.
///
/// # Arguments
/// * `error`: The error to check
///
/// # Returns
/// * `bool`: True if the condition is not met, false otherwise
pub fn check_key_already_exists(error: &NanoServiceError) -> bool {
	error.status == NanoServiceErrorStatus::BadRequest && error.message == KEY_ALREADY_EXISTS_ERR
}

/// Constructs a condition not met error.
///
/// # Returns
/// * `NanoServiceError`: The condition not met error
pub fn construct_key_already_exists_error() -> NanoServiceError {
	NanoServiceError::new(KEY_ALREADY_EXISTS_ERR.to_string(), NanoServiceErrorStatus::BadRequest)
}

/// The error message for when a condition is not met.
pub static CONDITION_NOT_MET_ERR: &str = "Condition not met";

/// Checks if the condition is not met.
///
/// # Arguments
/// * `error`: The error to check
///
/// # Returns
/// * `bool`: True if the condition is not met, false otherwise
pub fn check_condition_not_met(error: &NanoServiceError) -> bool {
	error.status == NanoServiceErrorStatus::BadRequest && error.message == CONDITION_NOT_MET_ERR
}

/// Constructs a condition not met error.
///
/// # Returns
/// * `NanoServiceError`: The condition not met error
pub fn construct_condition_not_met_error() -> NanoServiceError {
	NanoServiceError::new(CONDITION_NOT_MET_ERR.to_string(), NanoServiceErrorStatus::BadRequest)
}

#[cfg(test)]
mod tests {

	use super::*;

	#[test]
	fn test_check_condition_not_met_true() {
		let error = NanoServiceError::new(
			CONDITION_NOT_MET_ERR.to_string(),
			NanoServiceErrorStatus::BadRequest,
		);
		assert!(check_condition_not_met(&error));
	}

	#[test]
	fn test_check_condition_not_met_false() {
		let error = NanoServiceError::new(
			"Some other error".to_string(),
			NanoServiceErrorStatus::BadRequest,
		);
		assert!(!check_condition_not_met(&error));
	}

	#[test]
	fn test_construct_condition_not_met_error() {
		let error = construct_condition_not_met_error();
		assert_eq!(error.status, NanoServiceErrorStatus::BadRequest);
		assert_eq!(error.message, CONDITION_NOT_MET_ERR);
	}
}