hadoop_common/conf/configuration.rs
1#[derive(Clone, Copy)]
2pub struct Configuration {}
3
4impl Configuration {
5 /// Get the value of the `name` property, `None` if
6 /// no such property exists. If the key is deprecated, it returns the value of
7 /// the first key which replaces the deprecated key and is not `None`.
8 ///
9 /// Values are processed for [`variable expansion`]
10 /// before being returned.
11 ///
12 /// As a side effect get loads the properties from the sources if called for
13 /// the first time as a lazy init.
14 pub fn get<'a>(&self, _name: &str, default_value: Option<&'a str>) -> Option<&'a str> {
15 // TODO
16 None.or(default_value)
17 }
18
19 /// Get the value of the `name` property as a trimmed `&str`,
20 /// `None` if no such property exists.
21 /// If the key is deprecated, it returns the value of
22 /// the first key which replaces the deprecated key and is not `None`
23 ///
24 /// Values are processed for [`variable expansion`]
25 /// before being returned.
26 pub fn get_trimmed<'a>(&self, name: &str) -> Option<&'a str> {
27 self.get(name, None).map(|v| v.trim())
28 }
29
30 /// Get the value of the `name` property as a trimmed `&str`,
31 /// `default_value` if no such property exists.
32 /// See [`Configuration::get_trimmed`] for more details.
33 pub fn get_trimmed_with_default<'a>(&self, name: &str, default_value: &'a str) -> &'a str {
34 self.get_trimmed(name).unwrap_or(default_value)
35 }
36
37 /// Get the value of the `name` property as an `i32`.
38 /// If no such property exists, the provided default value is returned,
39 /// or if the specified value is not a valid `i32`,
40 /// then an error is thrown.
41 pub fn get_int(&self, name: &str, default_value: i32) -> anyhow::Result<i32> {
42 if let Some(value_string) = self.get_trimmed(name) {
43 Ok(i32::from_str_radix(value_string, 16)
44 .or_else(|_| i32::from_str_radix(value_string, 10))?)
45 } else {
46 Ok(default_value)
47 }
48 }
49
50 /// Get the value of the `name` property as an `i64`.
51 /// If no such property exists, the provided default value is returned,
52 /// or if the specified value is not a valid `i64`,
53 /// then an error is thrown.
54 pub fn get_long(&self, name: &str, default_value: i64) -> anyhow::Result<i64> {
55 if let Some(value_string) = self.get_trimmed(name) {
56 Ok(i64::from_str_radix(value_string, 16)
57 .or_else(|_| i64::from_str_radix(value_string, 10))?)
58 } else {
59 Ok(default_value)
60 }
61 }
62
63 /// Get the value of the `name` property as a `bool`.
64 /// If no such property is specified, or if the specified value is not a valid
65 /// `bool`, then `default_value` is returned.
66 pub fn get_bool(&self, name: &str, default_value: bool) -> bool {
67 match self.get_trimmed(name) {
68 Some(v) if v.to_lowercase() == "true" => true,
69 Some(v) if v.to_lowercase() == "false" => false,
70 _ => default_value,
71 }
72 }
73}