##// END OF EJS Templates
rust-config: fix fallback to default not parsing the default value...
Raphaël Gomès -
r51874:58390f59 default
parent child Browse files
Show More
@@ -140,6 +140,39 b" impl<'a> TryFrom<&'a DefaultConfigItem> "
140 }
140 }
141 }
141 }
142
142
143 impl<'a> TryFrom<&'a DefaultConfigItem> for Option<&'a [u8]> {
144 type Error = HgError;
145
146 fn try_from(
147 value: &'a DefaultConfigItem,
148 ) -> Result<Option<&'a [u8]>, Self::Error> {
149 match &value.default {
150 Some(default) => {
151 let err = HgError::abort(
152 format!(
153 "programming error: wrong query on config item '{}.{}'",
154 value.section,
155 value.name
156 ),
157 exit_codes::ABORT,
158 Some(format!(
159 "asked for bytes, type of default is '{}', \
160 which cannot be interpreted as bytes",
161 default.type_str()
162 )),
163 );
164 match default {
165 DefaultConfigItemType::Primitive(p) => {
166 Ok(p.as_str().map(str::as_bytes))
167 }
168 _ => Err(err),
169 }
170 }
171 None => Ok(None),
172 }
173 }
174 }
175
143 impl TryFrom<&DefaultConfigItem> for Option<bool> {
176 impl TryFrom<&DefaultConfigItem> for Option<bool> {
144 type Error = HgError;
177 type Error = HgError;
145
178
@@ -422,7 +422,30 b' impl Config {'
422 return Ok(None);
422 return Ok(None);
423 }
423 }
424 match self.get_default(section, item)? {
424 match self.get_default(section, item)? {
425 Some(default) => Ok(default.try_into()?),
425 Some(default) => {
426 // Defaults are TOML values, so they're not in the same
427 // shape as in the config files.
428 // First try to convert directly to the expected type
429 let as_t = default.try_into();
430 match as_t {
431 Ok(t) => Ok(t),
432 Err(e) => {
433 // If it fails, it means that...
434 let as_bytes: Result<Option<&[u8]>, _> =
435 default.try_into();
436 match as_bytes {
437 Ok(bytes_opt) => {
438 if let Some(bytes) = bytes_opt {
439 // ...we should be able to parse it
440 return Ok(parse(bytes));
441 }
442 Err(e)
443 }
444 Err(_) => Err(e),
445 }
446 }
447 }
448 }
426 None => {
449 None => {
427 self.print_devel_warning(section, item)?;
450 self.print_devel_warning(section, item)?;
428 Ok(None)
451 Ok(None)
@@ -779,7 +802,6 b' mod tests {'
779 let config = Config::load_from_explicit_sources(vec![])
802 let config = Config::load_from_explicit_sources(vec![])
780 .expect("expected valid config");
803 .expect("expected valid config");
781 let ret = config.get_byte_size(b"cmdserver", b"max-log-size");
804 let ret = config.get_byte_size(b"cmdserver", b"max-log-size");
782 // FIXME should be `is_ok`
805 assert!(ret.is_ok(), "{:?}", ret);
783 assert!(ret.is_err(), "{:?}", ret);
784 }
806 }
785 }
807 }
General Comments 0
You need to be logged in to leave comments. Login now