atlas_coverage_core/
settings.rs

1use util;
2use globset::{Glob, GlobSetBuilder, GlobSet};
3use serde::Deserializer;
4use serde::de::Visitor;
5use std::fmt;
6use serde::de::SeqAccess;
7use std::path::Path;
8use std::error::Error;
9
10#[derive(Debug, Deserialize)]
11pub struct Settings {
12    pub public_url_base: String,
13    pub dist_path: String,
14
15    pub dist_coverage_path: String,
16    pub dist_coverage_url: String,
17
18    pub sources: Sources,
19
20    pub reify_against_lcov: Option<String>,
21}
22
23#[derive(Debug, Deserialize)]
24pub struct Sources {
25    pub base: String,
26    #[serde(deserialize_with="deserialize_globset")]
27    pub dirs: GlobSet,
28    #[serde(deserialize_with="deserialize_globset")]
29    pub excludes: GlobSet,
30}
31
32fn deserialize_globset<'de, D>(deserializer: D) -> Result<GlobSet, D::Error>
33    where D: Deserializer<'de>,
34{
35    struct GlobSetVisitor{};
36
37    impl<'de> Visitor<'de> for GlobSetVisitor {
38        type Value = GlobSet;
39
40        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
41            write!(formatter, "expected a list of glob strings")
42        }
43
44        fn visit_seq<S>(self, mut seq: S) -> Result<GlobSet, S::Error> where S: SeqAccess<'de>,
45        {
46            let mut builder = GlobSetBuilder::new();
47
48            while let Some(value) = seq.next_element::<String>()? {
49                builder.add(Glob::new(&value).unwrap());
50            }
51
52            Ok(builder.build().unwrap())
53        }
54    }
55
56    // Create the visitor and ask the deserializer to drive it. The
57    // deserializer will call visitor.visit_seq() if a seq is present in
58    // the input data.
59    deserializer.deserialize_seq(GlobSetVisitor{})
60}
61
62pub fn from_path(settings_path: impl AsRef<Path>) -> Result<Settings, Box<Error>> {
63    Ok(util::deserialize_object(settings_path)?)
64}
65
66pub fn from_root() -> Result<Settings, Box<Error>> {
67    use std::env;
68
69    let path = env::current_dir()?;
70    let settings_path = path.join("settings.json");
71    from_path(settings_path)
72}