Show More
@@ -267,6 +267,66 impl TryFrom<&DefaultConfigItem> for Opt | |||
|
267 | 267 | } |
|
268 | 268 | } |
|
269 | 269 | |
|
270 | impl TryFrom<&DefaultConfigItem> for Option<i64> { | |
|
271 | type Error = HgError; | |
|
272 | ||
|
273 | fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> { | |
|
274 | match &value.default { | |
|
275 | Some(default) => { | |
|
276 | let err = HgError::abort( | |
|
277 | format!( | |
|
278 | "programming error: wrong query on config item '{}.{}'", | |
|
279 | value.section, | |
|
280 | value.name | |
|
281 | ), | |
|
282 | exit_codes::ABORT, | |
|
283 | Some(format!( | |
|
284 | "asked for 'i64', type of default is '{}'", | |
|
285 | default.type_str() | |
|
286 | )), | |
|
287 | ); | |
|
288 | match default { | |
|
289 | DefaultConfigItemType::Primitive( | |
|
290 | toml::Value::Integer(b), | |
|
291 | ) => Ok(Some(*b)), | |
|
292 | _ => Err(err), | |
|
293 | } | |
|
294 | } | |
|
295 | None => Ok(None), | |
|
296 | } | |
|
297 | } | |
|
298 | } | |
|
299 | ||
|
300 | impl TryFrom<&DefaultConfigItem> for Option<f64> { | |
|
301 | type Error = HgError; | |
|
302 | ||
|
303 | fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> { | |
|
304 | match &value.default { | |
|
305 | Some(default) => { | |
|
306 | let err = HgError::abort( | |
|
307 | format!( | |
|
308 | "programming error: wrong query on config item '{}.{}'", | |
|
309 | value.section, | |
|
310 | value.name | |
|
311 | ), | |
|
312 | exit_codes::ABORT, | |
|
313 | Some(format!( | |
|
314 | "asked for 'f64', type of default is '{}'", | |
|
315 | default.type_str() | |
|
316 | )), | |
|
317 | ); | |
|
318 | match default { | |
|
319 | DefaultConfigItemType::Primitive(toml::Value::Float( | |
|
320 | b, | |
|
321 | )) => Ok(Some(*b)), | |
|
322 | _ => Err(err), | |
|
323 | } | |
|
324 | } | |
|
325 | None => Ok(None), | |
|
326 | } | |
|
327 | } | |
|
328 | } | |
|
329 | ||
|
270 | 330 | /// Allows abstracting over more complex default values than just primitives. |
|
271 | 331 | /// The former `configitems.py` contained some dynamic code that is encoded |
|
272 | 332 | /// in this enum. |
@@ -531,6 +531,54 impl Config { | |||
|
531 | 531 | ) |
|
532 | 532 | } |
|
533 | 533 | |
|
534 | /// Returns an `Err` if the first value found is not a valid unsigned | |
|
535 | /// integer. Otherwise, returns an `Ok(value)` if found, or `None`. | |
|
536 | pub fn get_i64( | |
|
537 | &self, | |
|
538 | section: &[u8], | |
|
539 | item: &[u8], | |
|
540 | ) -> Result<Option<i64>, HgError> { | |
|
541 | self.get_parse( | |
|
542 | section, | |
|
543 | item, | |
|
544 | "valid integer", | |
|
545 | |value| str::from_utf8(value).ok()?.parse().ok(), | |
|
546 | true, | |
|
547 | ) | |
|
548 | } | |
|
549 | ||
|
550 | /// Returns an `Err` if the first value found is not a valid unsigned | |
|
551 | /// integer. Otherwise, returns an `Ok(value)` if found, or `None`. | |
|
552 | pub fn get_u64( | |
|
553 | &self, | |
|
554 | section: &[u8], | |
|
555 | item: &[u8], | |
|
556 | ) -> Result<Option<u64>, HgError> { | |
|
557 | self.get_parse( | |
|
558 | section, | |
|
559 | item, | |
|
560 | "valid integer", | |
|
561 | |value| str::from_utf8(value).ok()?.parse().ok(), | |
|
562 | true, | |
|
563 | ) | |
|
564 | } | |
|
565 | ||
|
566 | /// Returns an `Err` if the first value found is not a valid float | |
|
567 | /// representation. Otherwise, returns an `Ok(value)` if found, or `None`. | |
|
568 | pub fn get_f64( | |
|
569 | &self, | |
|
570 | section: &[u8], | |
|
571 | item: &[u8], | |
|
572 | ) -> Result<Option<f64>, HgError> { | |
|
573 | self.get_parse( | |
|
574 | section, | |
|
575 | item, | |
|
576 | "valid float", | |
|
577 | |value| str::from_utf8(value).ok()?.parse().ok(), | |
|
578 | true, | |
|
579 | ) | |
|
580 | } | |
|
581 | ||
|
534 | 582 | /// Returns an `Err` if the first value found is not a valid file size |
|
535 | 583 | /// value such as `30` (default unit is bytes), `7 MB`, or `42.5 kb`. |
|
536 | 584 | /// Otherwise, returns an `Ok(value_in_bytes)` if found, or `None`. |
@@ -548,6 +596,22 impl Config { | |||
|
548 | 596 | ) |
|
549 | 597 | } |
|
550 | 598 | |
|
599 | /// Same as [`Self::get_byte_size`], but doesn't fall back to the default | |
|
600 | /// `configitem` if not defined in the user config. | |
|
601 | pub fn get_byte_size_no_default( | |
|
602 | &self, | |
|
603 | section: &[u8], | |
|
604 | item: &[u8], | |
|
605 | ) -> Result<Option<u64>, HgError> { | |
|
606 | self.get_parse( | |
|
607 | section, | |
|
608 | item, | |
|
609 | "byte quantity", | |
|
610 | values::parse_byte_size, | |
|
611 | false, | |
|
612 | ) | |
|
613 | } | |
|
614 | ||
|
551 | 615 | /// Returns an `Err` if the first value found is not a valid boolean. |
|
552 | 616 | /// Otherwise, returns an `Ok(option)`, where `option` is the boolean if |
|
553 | 617 | /// found, or `None`. |
@@ -744,6 +808,60 impl Config { | |||
|
744 | 808 | pub fn tweakdefaults(&mut self) { |
|
745 | 809 | self.layers.insert(0, Config::tweakdefaults_layer()); |
|
746 | 810 | } |
|
811 | ||
|
812 | /// Return the resource profile for a dimension (memory, cpu or disk). | |
|
813 | /// | |
|
814 | /// If no dimension is specified, the generic value is returned. | |
|
815 | pub fn get_resource_profile( | |
|
816 | &self, | |
|
817 | dimension: Option<&str>, | |
|
818 | ) -> ResourceProfile { | |
|
819 | let mut value = self.resource_profile_from_item(b"usage", b"resource"); | |
|
820 | if let Some(dimension) = &dimension { | |
|
821 | let sub_value = self.resource_profile_from_item( | |
|
822 | b"usage", | |
|
823 | format!("resources.{}", dimension).as_bytes(), | |
|
824 | ); | |
|
825 | if sub_value != ResourceProfileValue::Default { | |
|
826 | value = sub_value | |
|
827 | } | |
|
828 | } | |
|
829 | ResourceProfile { | |
|
830 | dimension: dimension.map(ToOwned::to_owned), | |
|
831 | value, | |
|
832 | } | |
|
833 | } | |
|
834 | ||
|
835 | fn resource_profile_from_item( | |
|
836 | &self, | |
|
837 | section: &[u8], | |
|
838 | item: &[u8], | |
|
839 | ) -> ResourceProfileValue { | |
|
840 | match self.get(section, item).unwrap_or(b"default") { | |
|
841 | b"default" => ResourceProfileValue::Default, | |
|
842 | b"low" => ResourceProfileValue::Low, | |
|
843 | b"medium" => ResourceProfileValue::Medium, | |
|
844 | b"high" => ResourceProfileValue::High, | |
|
845 | _ => ResourceProfileValue::Default, | |
|
846 | } | |
|
847 | } | |
|
848 | } | |
|
849 | ||
|
850 | /// Corresponds to `usage.resources[.<dimension>]`. | |
|
851 | /// | |
|
852 | /// See `hg help config.usage.resources`. | |
|
853 | #[derive(Debug, Eq, PartialEq, PartialOrd, Ord)] | |
|
854 | pub struct ResourceProfile { | |
|
855 | pub dimension: Option<String>, | |
|
856 | pub value: ResourceProfileValue, | |
|
857 | } | |
|
858 | ||
|
859 | #[derive(Debug, Eq, PartialEq, PartialOrd, Ord)] | |
|
860 | pub enum ResourceProfileValue { | |
|
861 | Default, | |
|
862 | Low, | |
|
863 | Medium, | |
|
864 | High, | |
|
747 | 865 | } |
|
748 | 866 | |
|
749 | 867 | #[cfg(test)] |
General Comments 0
You need to be logged in to leave comments.
Login now