summaryrefslogtreecommitdiffstats
path: root/vendor/gix/src/config/tree/sections/pack.rs
blob: 941817e5ba410860d13cc50aaaf8e5b5d0c7f3ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::{
    config,
    config::tree::{keys, Key, Pack, Section},
};

impl Pack {
    /// The `pack.threads` key.
    pub const THREADS: keys::UnsignedInteger =
        keys::UnsignedInteger::new_unsigned_integer("threads", &config::Tree::PACK)
            .with_deviation("Leaving this key unspecified uses all available cores, instead of 1");

    /// The `pack.indexVersion` key.
    pub const INDEX_VERSION: IndexVersion =
        IndexVersion::new_with_validate("indexVersion", &config::Tree::PACK, validate::IndexVersion);
}

/// The `pack.indexVersion` key.
pub type IndexVersion = keys::Any<validate::IndexVersion>;

mod index_version {
    use crate::{config, config::tree::sections::pack::IndexVersion};

    impl IndexVersion {
        /// Try to interpret an integer value as index version.
        pub fn try_into_index_version(
            &'static self,
            value: Result<i64, gix_config::value::Error>,
        ) -> Result<gix_pack::index::Version, config::key::GenericError> {
            let value = value.map_err(|err| config::key::GenericError::from(self).with_source(err))?;
            Ok(match value {
                1 => gix_pack::index::Version::V1,
                2 => gix_pack::index::Version::V2,
                _ => return Err(config::key::GenericError::from(self)),
            })
        }
    }
}

impl Section for Pack {
    fn name(&self) -> &str {
        "pack"
    }

    fn keys(&self) -> &[&dyn Key] {
        &[&Self::THREADS, &Self::INDEX_VERSION]
    }
}

mod validate {
    use crate::{bstr::BStr, config::tree::keys};

    pub struct IndexVersion;
    impl keys::Validate for IndexVersion {
        fn validate(&self, value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
            super::Pack::INDEX_VERSION.try_into_index_version(gix_config::Integer::try_from(value).and_then(
                |int| {
                    int.to_decimal()
                        .ok_or_else(|| gix_config::value::Error::new("integer out of range", value))
                },
            ))?;
            Ok(())
        }
    }
}