puffin_build/library/build/
config.rs

1use std::collections::HashMap;
2use std::fs;
3use std::path::Path;
4
5use serde::{Deserialize, Serialize};
6
7use super::builder::Builder;
8use super::options::Options;
9use super::sources::Sources;
10use crate::error::Result;
11use crate::utils::Value;
12
13#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
14pub struct Config {
15    builder: Builder,
16    sources: Sources,
17
18    #[serde(default)]
19    #[serde(flatten)]
20    options: Options,
21}
22
23impl Config {
24    pub fn new(
25        sources: impl Into<Sources>,
26        builder: impl Into<Builder>,
27        options: impl Into<Options>,
28    ) -> Self {
29        Self {
30            builder: builder.into(),
31            sources: sources.into(),
32            options: options.into(),
33        }
34    }
35
36    pub fn preset(vendor: impl AsRef<str>, name: impl AsRef<str>) -> Option<Config> {
37        // TODO gracefully handle error cases rather than using `unwrap`
38
39        // FIXME don't use harcoded path to configuration files (CARGO_MANIFEST_DIR, ...)
40        let configs_str = fs::read_to_string(
41            Path::new(env!("CARGO_MANIFEST_DIR"))
42                .join("vendors")
43                .join(vendor.as_ref())
44                .join("presets.toml"),
45        )
46        .ok()?;
47
48        toml::from_str::<HashMap<String, Config>>(&configs_str)
49            .ok()?
50            .get(name.as_ref())
51            .cloned()
52    }
53
54    pub fn build(&self, out_dir: impl AsRef<Path>, cput: bool) -> Result<()> {
55        let sources_dir = out_dir.as_ref().join("src");
56        let sources = self.sources.download(sources_dir)?;
57        self.builder.build(
58            sources,
59            out_dir,
60            self.sources.version(),
61            &self.options,
62            cput,
63        )
64    }
65
66    pub fn option(&mut self, name: impl Into<String>, value: impl Into<Value>) -> &mut Self {
67        self.options.insert(name, value);
68        self
69    }
70}