aboutsummaryrefslogtreecommitdiff
path: root/bin/src/utils.rs
blob: a3d51b467ffa9ba690b6ac826631f5fa95f8b94a (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
use std::collections::HashMap;

use lib::{Lint, LINTS};
use rnix::SyntaxKind;

pub fn lint_map_of(
    lints: &[&'static Box<dyn Lint>],
) -> HashMap<SyntaxKind, Vec<&'static Box<dyn Lint>>> {
    let mut map = HashMap::new();
    for lint in lints.iter() {
        let lint = *lint;
        let matches = lint.match_kind();
        for m in matches {
            map.entry(m)
                .and_modify(|v: &mut Vec<_>| v.push(lint))
                .or_insert_with(|| vec![lint]);
        }
    }
    map
}

pub fn lint_map() -> HashMap<SyntaxKind, Vec<&'static Box<dyn Lint>>> {
    lint_map_of(&*LINTS)
}

pub fn get_version_info() -> Option<String> {
    use std::process::Command;
    let program = Command::new("nix")
        .arg("--version")
        .output()
        .expect("failed to execute");
    std::str::from_utf8(&program.stdout)
        .ok()?
        .split(' ')
        .nth(2)
        .map(ToOwned::to_owned)
}