summaryrefslogtreecommitdiff
path: root/src/repo/profile/mod.rs
blob: 3edd1702991b4b1a0cf95b6d7da6e129974ce875 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use std::{
    collections::HashMap,
    fs::{self, File},
    io::{self, Read},
    path::{Path, PathBuf},
};

use get::Get;
use itertools::Itertools;

use crate::{atom::Atom, useflag::UseFlag};

mod make_defaults;
mod package;
mod package_use;
mod packages;
mod parsers;
mod useflags;

#[derive(Debug, Clone)]
enum LineBasedFileExpr<T> {
    Comment,
    Expr(T),
}

#[derive(Debug, Clone)]
enum FlagOperation {
    Add(UseFlag),
    Remove(UseFlag),
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("{0}: io error: {1}")]
    Io(PathBuf, io::Error),
    #[error("error evaluating make.defaults settings: {0}")]
    MakeDefaults(#[from] make_defaults::Error),
    #[error("error evaluating packages settings: {0}")]
    Packages(#[from] packages::Error),
    #[error("error evaluating package settings: {0}")]
    Package(#[from] package::Error),
    #[error("error evaluating package.use settings: {0}")]
    PackageUse(#[from] package_use::Error),
    #[error("error evaluating use settings: {0}")]
    Use(#[from] useflags::Error),
}

#[derive(Debug, Clone, Get)]
pub struct Profile {
    #[get(kind = "deref")]
    path: PathBuf,
    #[get(kind = "deref")]
    parents: Vec<Profile>,
    make_defaults: HashMap<String, String>,
    #[get(kind = "deref")]
    packages: Vec<Atom>,
    #[get(kind = "deref")]
    package_mask: Vec<Atom>,
    #[get(kind = "deref")]
    package_provided: Vec<Atom>,
    package_use: HashMap<Atom, Vec<UseFlag>>,
    package_use_force: HashMap<Atom, Vec<UseFlag>>,
    package_use_mask: HashMap<Atom, Vec<UseFlag>>,
    package_use_stable_force: HashMap<Atom, Vec<UseFlag>>,
    package_use_stable_mask: HashMap<Atom, Vec<UseFlag>>,
    #[get(kind = "deref")]
    use_force: Vec<UseFlag>,
    #[get(kind = "deref")]
    use_mask: Vec<UseFlag>,
    #[get(kind = "deref")]
    use_stable_force: Vec<UseFlag>,
    #[get(kind = "deref")]
    use_stable_mask: Vec<UseFlag>,
}

impl Profile {
    pub(super) fn evaluate<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
        let parents_path = path.as_ref().join("parent");

        let parents = match fs::read_to_string(&parents_path) {
            Ok(parents) => parents
                .lines()
                .map(|line| path.as_ref().join(line))
                .map(Profile::evaluate)
                .collect::<Result<_, _>>()?,
            Err(e) if matches!(e.kind(), io::ErrorKind::NotFound) => Vec::new(),
            Err(e) => return Err(Error::Io(parents_path, e)),
        };

        let make_defaults = make_defaults::evaluate(&parents, &path)?;

        let packages = packages::evaluate(&parents, &path)?;

        let package_mask = package::evaluate(&parents, package::Kind::Mask, &path)?;
        let package_provided = package::evaluate(&parents, package::Kind::Provided, &path)?;

        let package_use = package_use::evaluate(&parents, package_use::Kind::Use, &path)?;
        let package_use_force = package_use::evaluate(&parents, package_use::Kind::Force, &path)?;
        let package_use_mask = package_use::evaluate(&parents, package_use::Kind::Mask, &path)?;
        let package_use_stable_force =
            package_use::evaluate(&parents, package_use::Kind::StableForce, &path)?;
        let package_use_stable_mask =
            package_use::evaluate(&parents, package_use::Kind::StableMask, &path)?;

        let use_force = useflags::evaluate(&parents, useflags::Kind::Force, &path)?;
        let use_mask = useflags::evaluate(&parents, useflags::Kind::Mask, &path)?;
        let use_stable_force = useflags::evaluate(&parents, useflags::Kind::StableForce, &path)?;
        let use_stable_mask = useflags::evaluate(&parents, useflags::Kind::StableMask, &path)?;

        Ok(Self {
            path: path.as_ref().to_path_buf(),
            parents,
            make_defaults,
            packages,
            package_mask,
            package_provided,
            package_use,
            package_use_force,
            package_use_mask,
            package_use_stable_force,
            package_use_stable_mask,
            use_force,
            use_mask,
            use_stable_force,
            use_stable_mask,
        })
    }
}

fn read_config_files<P: AsRef<Path>>(path: P) -> Result<String, io::Error> {
    let metadata = fs::metadata(&path)?;

    if metadata.is_file() {
        fs::read_to_string(&path)
    } else if metadata.is_dir() {
        let mut buffer = String::new();
        let paths = fs::read_dir(&path)?
            .collect::<Result<Vec<_>, _>>()?
            .into_iter()
            .map(|entry| entry.path())
            .filter(|path| path.starts_with("."))
            .sorted()
            .collect::<Vec<_>>();

        for path in &paths {
            let mut file = File::open(path)?;

            file.read_to_string(&mut buffer)?;
        }

        Ok(buffer)
    } else {
        let path = fs::canonicalize(&path)?;

        read_config_files(path)
    }
}