rushls_common/config/
values.rs1use std::{collections::BTreeMap, fmt, net::SocketAddr, path::Path, str::FromStr, time::Duration};
8
9use bytesize::ByteSize;
10use serde::Deserialize;
11
12use crate::config::{ConfigError, TextSource};
13
14#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
21pub struct OptionalDuration(pub Option<Duration>);
22
23impl fmt::Display for OptionalDuration {
24 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
25 match self.0 {
26 Some(duration) => write!(formatter, "{}", humantime::format_duration(duration)),
27 None => formatter.write_str("off"),
28 }
29 }
30}
31
32pub fn parse_optional_duration(value: &str) -> Result<OptionalDuration, String> {
33 let trimmed = value.trim();
34 if trimmed.eq_ignore_ascii_case("off") || trimmed.eq_ignore_ascii_case("none") {
35 return Ok(OptionalDuration(None));
36 }
37 humantime::parse_duration(trimmed)
38 .map(|duration| OptionalDuration(Some(duration)))
39 .map_err(|error| error.to_string())
40}
41
42#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
48pub struct OptionalBytes(pub Option<ByteSize>);
49
50impl fmt::Display for OptionalBytes {
51 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
52 match self.0 {
53 Some(bytes) => write!(formatter, "{bytes}"),
54 None => formatter.write_str("unlimited"),
55 }
56 }
57}
58
59pub fn parse_optional_bytes(value: &str) -> Result<OptionalBytes, String> {
60 let trimmed = value.trim();
61 if trimmed.eq_ignore_ascii_case("unlimited") {
62 return Ok(OptionalBytes(None));
63 }
64 trimmed.parse().map(|bytes| OptionalBytes(Some(bytes)))
65}
66
67#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
72pub struct OptionalAddress(pub Option<SocketAddr>);
73
74impl fmt::Display for OptionalAddress {
75 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
76 match self.0 {
77 Some(address) => write!(formatter, "{address}"),
78 None => formatter.write_str("off"),
79 }
80 }
81}
82
83pub fn parse_optional_address(value: &str) -> Result<OptionalAddress, String> {
84 let trimmed = value.trim();
85 if trimmed.eq_ignore_ascii_case("off") {
86 return Ok(OptionalAddress(None));
87 }
88 trimmed
89 .parse()
90 .map(|address| OptionalAddress(Some(address)))
91 .map_err(|error| format!("{error}"))
92}
93
94#[derive(Debug, Deserialize)]
101#[serde(transparent)]
102pub struct TomlTable<T>(pub BTreeMap<String, T>);
103
104impl<T> Default for TomlTable<T> {
107 fn default() -> Self {
108 Self(BTreeMap::new())
109 }
110}
111
112impl<T: serde::de::DeserializeOwned> FromStr for TomlTable<T> {
113 type Err = toml::de::Error;
114
115 fn from_str(value: &str) -> Result<Self, Self::Err> {
116 toml::from_str(value).map_err(|mut error| {
117 error.set_input(None);
119 error
120 })
121 }
122}
123
124#[derive(Clone, Debug, Deserialize)]
131#[serde(transparent)]
132pub struct TomlValue<T>(pub T);
133
134impl<T: serde::de::DeserializeOwned> FromStr for TomlValue<T> {
135 type Err = toml::de::Error;
136
137 fn from_str(value: &str) -> Result<Self, Self::Err> {
138 #[derive(Deserialize)]
139 struct Wrapper<T> {
140 value: T,
141 }
142 toml::from_str::<Wrapper<T>>(&format!("value = {value}")).map(|wrapper| Self(wrapper.value))
145 }
146}
147
148pub fn read_credential(
155 label: &str,
156 source: Option<&TextSource>,
157 cli_file: Option<&Path>,
158) -> Result<Option<String>, ConfigError> {
159 match cli_file {
160 Some(path) => TextSource::File(path.to_path_buf()).read(label).map(Some),
161 None => source.map(|source| source.read(label)).transpose(),
162 }
163}