gh_workflow/
rust_flag.rs

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
//! A type-safe representation of the Rust toolchain.

use std::fmt::{Display, Formatter};

use indexmap::IndexMap;
use serde_json::Value;

use crate::Env;

#[derive(Clone)]
pub enum RustFlags {
    Lint(String, Lint),
    Combine(Box<RustFlags>, Box<RustFlags>),
}
#[derive(Clone)]
pub enum Lint {
    Allow,
    Warn,
    Deny,
    Forbid,
    Codegen,
    Experiment,
}

impl core::ops::Add for RustFlags {
    type Output = RustFlags;

    fn add(self, rhs: Self) -> Self::Output {
        RustFlags::Combine(Box::new(self), Box::new(rhs))
    }
}

impl RustFlags {
    pub fn allow<S: ToString>(name: S) -> Self {
        RustFlags::Lint(name.to_string(), Lint::Allow)
    }

    pub fn warn<S: ToString>(name: S) -> Self {
        RustFlags::Lint(name.to_string(), Lint::Warn)
    }

    pub fn deny<S: ToString>(name: S) -> Self {
        RustFlags::Lint(name.to_string(), Lint::Deny)
    }

    pub fn forbid<S: ToString>(name: S) -> Self {
        RustFlags::Lint(name.to_string(), Lint::Forbid)
    }

    pub fn codegen<S: ToString>(name: S) -> Self {
        RustFlags::Lint(name.to_string(), Lint::Codegen)
    }
}

impl Display for RustFlags {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            RustFlags::Lint(name, lint) => match lint {
                Lint::Allow => write!(f, "-A{}", name),
                Lint::Warn => write!(f, "-W{}", name),
                Lint::Deny => write!(f, "-D{}", name),
                Lint::Forbid => write!(f, "-F{}", name),
                Lint::Codegen => write!(f, "-C{}", name),
                Lint::Experiment => write!(f, "-Z{}", name),
            },
            RustFlags::Combine(lhs, rhs) => write!(f, "{} {}", lhs, rhs),
        }
    }
}

impl From<RustFlags> for Env {
    fn from(value: RustFlags) -> Self {
        let mut env = IndexMap::default();
        env.insert("RUSTFLAGS".to_string(), Value::from(value.to_string()));
        Env::from(env)
    }
}