]> git.proxmox.com Git - rustc.git/blob - vendor/gix/src/config/tree/sections/index.rs
08f7ec1bd41ce42cc7d6d442efc153b0004d0fed
[rustc.git] / vendor / gix / src / config / tree / sections / index.rs
1 use crate::{
2 config,
3 config::tree::{keys, Index, Key, Section},
4 };
5
6 impl Index {
7 /// The `index.threads` key.
8 pub const THREADS: IndexThreads =
9 IndexThreads::new_with_validate("threads", &config::Tree::INDEX, validate::IndexThreads);
10 }
11
12 /// The `index.threads` key.
13 pub type IndexThreads = keys::Any<validate::IndexThreads>;
14
15 mod index_threads {
16 use std::borrow::Cow;
17
18 use crate::{
19 bstr::BStr,
20 config,
21 config::{key::GenericErrorWithValue, tree::index::IndexThreads},
22 };
23
24 impl IndexThreads {
25 /// Parse `value` into the amount of threads to use, with `1` being single-threaded, or `0` indicating
26 /// to select the amount of threads, with any other number being the specific amount of threads to use.
27 pub fn try_into_index_threads(
28 &'static self,
29 value: Cow<'_, BStr>,
30 ) -> Result<usize, config::key::GenericErrorWithValue> {
31 gix_config::Integer::try_from(value.as_ref())
32 .ok()
33 .and_then(|i| i.to_decimal().and_then(|i| i.try_into().ok()))
34 .or_else(|| {
35 gix_config::Boolean::try_from(value.as_ref())
36 .ok()
37 .map(|b| if b.0 { 0 } else { 1 })
38 })
39 .ok_or_else(|| GenericErrorWithValue::from_value(self, value.into_owned()))
40 }
41 }
42 }
43
44 impl Section for Index {
45 fn name(&self) -> &str {
46 "index"
47 }
48
49 fn keys(&self) -> &[&dyn Key] {
50 &[&Self::THREADS]
51 }
52 }
53
54 mod validate {
55 use crate::{bstr::BStr, config::tree::keys};
56
57 pub struct IndexThreads;
58 impl keys::Validate for IndexThreads {
59 fn validate(&self, value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
60 super::Index::THREADS.try_into_index_threads(value.into())?;
61 Ok(())
62 }
63 }
64 }