gix_config/file/
meta.rs

1use std::path::PathBuf;
2
3use crate::{file, file::Metadata, Source};
4
5/// Instantiation
6impl Metadata {
7    /// Return metadata indicating the source of a [`File`][crate::File] is from an API user.
8    pub fn api() -> Self {
9        file::Metadata {
10            path: None,
11            source: Source::Api,
12            level: 0,
13            trust: gix_sec::Trust::Full,
14        }
15    }
16
17    /// Return metadata as derived from the given `path` at `source`, which will also be used to derive the trust level
18    /// by checking its ownership.
19    pub fn try_from_path(path: impl Into<PathBuf>, source: Source) -> std::io::Result<Self> {
20        let path = path.into();
21        gix_sec::Trust::from_path_ownership(&path).map(|trust| Metadata {
22            path: path.into(),
23            source,
24            level: 0,
25            trust,
26        })
27    }
28
29    /// Set the trust level of this instance to the given `trust` and return it.
30    ///
31    /// Useful in conjunction with `Metadata::from(source)`.
32    pub fn with(mut self, trust: gix_sec::Trust) -> Self {
33        self.trust = trust;
34        self
35    }
36
37    /// Set the metadata to be located at the given `path`.
38    pub fn at(mut self, path: impl Into<PathBuf>) -> Self {
39        self.path = Some(path.into());
40        self
41    }
42}
43
44impl Default for Metadata {
45    fn default() -> Self {
46        Metadata::api()
47    }
48}
49
50impl From<Source> for Metadata {
51    fn from(source: Source) -> Self {
52        file::Metadata {
53            path: None,
54            source,
55            level: 0,
56            trust: gix_sec::Trust::Full,
57        }
58    }
59}