summaryrefslogtreecommitdiffstats
path: root/src/config/src/utils/get_unsigned_integer.rs
blob: 25dfa617bae2f91754511036d946ee534cb09112 (plain)
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
use git::{Config, ErrorCode};

use crate::{utils::get_optional_string, ConfigError, ConfigErrorCause};

pub(crate) fn get_unsigned_integer(config: Option<&Config>, name: &str, default: u32) -> Result<u32, ConfigError> {
	if let Some(cfg) = config {
		match cfg.get_i32(name) {
			Ok(v) => {
				v.try_into().map_err(|_| {
					ConfigError::new_with_optional_input(
						name,
						get_optional_string(config, name).ok().flatten(),
						ConfigErrorCause::InvalidUnsignedInteger,
					)
				})
			},
			Err(e) if e.code() == ErrorCode::NotFound => Ok(default),
			Err(e) if e.message().contains("failed to parse") => {
				Err(ConfigError::new_with_optional_input(
					name,
					get_optional_string(config, name).ok().flatten(),
					ConfigErrorCause::InvalidUnsignedInteger,
				))
			},
			Err(e) => {
				Err(ConfigError::new_with_optional_input(
					name,
					get_optional_string(config, name).ok().flatten(),
					ConfigErrorCause::UnknownError(String::from(e.message())),
				))
			},
		}
	}
	else {
		Ok(default)
	}
}

#[cfg(test)]
mod tests {
	use claims::assert_ok_eq;
	use testutils::assert_err_eq;

	use super::*;
	use crate::testutils::{invalid_utf, with_git_config};

	#[test]
	fn read_value() {
		with_git_config(&["[test]", "value = 42"], |git_config| {
			assert_ok_eq!(get_unsigned_integer(Some(&git_config), "test.value", 24), 42);
		});
	}

	#[test]
	fn read_value_min() {
		with_git_config(&["[test]", "value = 0"], |git_config| {
			assert_ok_eq!(get_unsigned_integer(Some(&git_config), "test.value", 24), 0);
		});
	}

	#[test]
	#[allow(clippy::as_conversions)]
	fn read_value_max() {
		with_git_config(&["[test]", format!("value = {}", i32::MAX).as_str()], |git_config| {
			assert_ok_eq!(
				get_unsigned_integer(Some(&git_config), "test.value", 24),
				i32::MAX as u32 // git only supports i32s, so use i32 max
			);
		});
	}

	#[test]
	fn read_value_too_small() {
		with_git_config(&["[test]", "value = -1"], |git_config| {
			assert_err_eq!(
				get_unsigned_integer(Some(&git_config), "test.value", 24),
				ConfigError::new("test.value", "-1", ConfigErrorCause::InvalidUnsignedInteger)
			);
		});
	}

	#[test]
	fn read_value_too_small_i32() {
		with_git_config(&["[test]", format!("value = {}", i64::MIN).as_str()], |git_config| {
			assert_err_eq!(
				get_unsigned_integer(Some(&git_config), "test.value", 24),
				ConfigError::new(
					"test.value",
					i64::MIN.to_string().as_str(),
					ConfigErrorCause::InvalidUnsignedInteger
				)
			);
		});
	}

	#[test]
	fn read_value_too_large() {
		with_git_config(&["[test]", format!("value = {}", u64::MAX).as_str()], |git_config| {
			assert_err_eq!(
				get_unsigned_integer(Some(&git_config), "test.value", 24),
				ConfigError::new(
					"test.value",
					u64::MAX.to_string().as_str(),
					ConfigErrorCause::InvalidUnsignedInteger
				)
			);
		});
	}

	#[test]
	fn read_default() {
		with_git_config(&[], |git_config| {
			assert_ok_eq!(get_unsigned_integer(Some(&git_config), "test.value", 24), 24);
		});
	}

	#[test]
	fn read_invalid() {
		with_git_config(&["[test]", "value = invalid"], |git_config| {
			assert_err_eq!(
				get_unsigned_integer(Some(&git_config), "test.value", 24),
				ConfigError::new("test.value", "invalid", ConfigErrorCause::InvalidUnsignedInteger)
			);
		});
	}

	#[test]
	fn read_unexpected_error() {
		with_git_config(&["[test]", "value = invalid"], |git_config| {
			assert_err_eq!(
				get_unsigned_integer(Some(&git_config), "test", 24),
				ConfigError::new_read_error(
					"test",
					ConfigErrorCause::UnknownError(String::from("invalid config item name 'test'"))
				)
			);
		});
	}

	#[test]
	fn read_invalid_non_utf() {
		with_git_config(
			&["[test]", format!("value = {}", invalid_utf()).as_str()],
			|git_config| {
				assert_err_eq!(
					get_unsigned_integer(Some(&git_config), "test.value", 24),
					ConfigError::new_read_error("test.value", ConfigErrorCause::InvalidUnsignedInteger)
				);
			},
		);
	}
}