summaryrefslogtreecommitdiffstats
path: root/src/config/src/utils/get_string.rs
blob: 0416323460bfc645f3008508bf7b4c63ea7b9a66 (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
use git::{Config, ErrorCode};

use crate::{ConfigError, ConfigErrorCause};

pub(crate) fn _get_string(config: Option<&Config>, name: &str) -> Result<Option<String>, ConfigError> {
	let Some(cfg) = config
	else {
		return Ok(None);
	};
	match cfg.get_string(name) {
		Ok(v) => Ok(Some(v)),
		Err(e) if e.code() == ErrorCode::NotFound => Ok(None),
		// detecting a UTF-8 error is tricky
		Err(e) if e.message() == "configuration value is not valid utf8" => {
			Err(ConfigError::new_read_error(name, ConfigErrorCause::InvalidUtf))
		},
		Err(e) => {
			Err(ConfigError::new_read_error(
				name,
				ConfigErrorCause::UnknownError(String::from(e.message())),
			))
		},
	}
}

pub(crate) fn get_string(config: Option<&Config>, name: &str, default: &str) -> Result<String, ConfigError> {
	Ok(_get_string(config, name)?.unwrap_or_else(|| String::from(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 = foo"], |git_config| {
			assert_ok_eq!(
				get_string(Some(&git_config), "test.value", "default"),
				String::from("foo")
			);
		});
	}

	#[test]
	fn read_default() {
		with_git_config(&[], |git_config| {
			assert_ok_eq!(
				get_string(Some(&git_config), "test.value", "default"),
				String::from("default")
			);
		});
	}

	#[test]
	fn read_unexpected_error() {
		with_git_config(&["[test]", "value = invalid"], |git_config| {
			assert_err_eq!(
				get_string(Some(&git_config), "test", "default"),
				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_string(Some(&git_config), "test.value", "default"),
					ConfigError::new_read_error("test.value", ConfigErrorCause::InvalidUtf)
				);
			},
		);
	}
}