gh_workflow/
toolchain.rs

1//! The typed version of the `setup-rust-toolchain` action in a GitHub workflow.
2//! Docs: <https://github.com/actions-rust-lang/setup-rust-toolchain>
3
4use std::fmt::{Display, Formatter};
5
6use derive_setters::Setters;
7
8use crate::{Input, RustFlags, Step, Use};
9
10#[derive(Clone)]
11pub enum Version {
12    Stable,
13    Nightly,
14    Custom((u64, u64, u64)),
15}
16
17impl Display for Version {
18    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
19        match self {
20            Version::Stable => write!(f, "stable"),
21            Version::Nightly => write!(f, "nightly"),
22            Version::Custom(s) => write!(f, "{}.{}.{}", s.0, s.1, s.2),
23        }
24    }
25}
26
27impl Version {
28    pub fn new(major: u64, minor: u64, patch: u64) -> Self {
29        Version::Custom((major, minor, patch))
30    }
31}
32
33#[derive(Clone, Debug)]
34pub enum Component {
35    Clippy,
36    Rustfmt,
37    RustDoc,
38}
39
40impl Display for Component {
41    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
42        let val = match self {
43            Component::Clippy => "clippy",
44            Component::Rustfmt => "rustfmt",
45            Component::RustDoc => "rust-doc",
46        };
47        write!(f, "{val}")
48    }
49}
50
51#[derive(Clone)]
52pub enum Arch {
53    X86_64,
54    Aarch64,
55    Arm,
56    Wasm32,
57}
58
59impl Display for Arch {
60    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
61        let val = match self {
62            Arch::X86_64 => "x86_64",
63            Arch::Aarch64 => "aarch64",
64            Arch::Arm => "arm",
65            Arch::Wasm32 => "wasm32",
66        };
67        write!(f, "{val}")
68    }
69}
70
71#[derive(Clone)]
72pub enum Vendor {
73    Unknown,
74    Apple,
75    PC,
76}
77
78impl Display for Vendor {
79    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
80        let val = match self {
81            Vendor::Unknown => "unknown",
82            Vendor::Apple => "apple",
83            Vendor::PC => "pc",
84        };
85        write!(f, "{val}")
86    }
87}
88
89#[derive(Clone)]
90pub enum System {
91    Unknown,
92    Windows,
93    Linux,
94    Darwin,
95}
96
97impl Display for System {
98    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
99        let val = match self {
100            System::Unknown => "unknown",
101            System::Windows => "windows",
102            System::Linux => "linux",
103            System::Darwin => "darwin",
104        };
105        write!(f, "{val}")
106    }
107}
108
109#[derive(Clone)]
110pub enum Abi {
111    Unknown,
112    Gnu,
113    Msvc,
114    Musl,
115}
116
117impl Display for Abi {
118    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
119        let val = match self {
120            Abi::Unknown => "unknown",
121            Abi::Gnu => "gnu",
122            Abi::Msvc => "msvc",
123            Abi::Musl => "musl",
124        };
125        write!(f, "{val}")
126    }
127}
128
129#[derive(Clone, Setters)]
130pub struct Target {
131    pub arch: Arch,
132    pub vendor: Vendor,
133    pub system: System,
134    pub abi: Option<Abi>,
135}
136
137///
138/// A Rust representation for the inputs of the setup-rust action.
139/// More information can be found [here](https://github.com/actions-rust-lang/setup-rust-toolchain/blob/main/action.yml).
140/// NOTE: The public API should be close to the original action as much as
141/// possible.
142#[derive(Default, Clone, Setters)]
143#[setters(strip_option, into)]
144pub struct Toolchain {
145    pub toolchain: Vec<Version>,
146    #[setters(skip)]
147    pub target: Option<Target>,
148    pub components: Vec<Component>,
149    pub cache: Option<bool>,
150    pub cache_directories: Vec<String>,
151    pub cache_workspaces: Vec<String>,
152    pub cache_on_failure: Option<bool>,
153    pub cache_key: Option<String>,
154    pub matcher: Option<bool>,
155    pub rust_flags: Option<RustFlags>,
156    pub override_default: Option<bool>,
157}
158
159impl Toolchain {
160    pub fn add_version(mut self, version: Version) -> Self {
161        self.toolchain.push(version);
162        self
163    }
164
165    pub fn add_component(mut self, component: Component) -> Self {
166        self.components.push(component);
167        self
168    }
169
170    pub fn add_stable(mut self) -> Self {
171        self.toolchain.push(Version::Stable);
172        self
173    }
174
175    pub fn add_nightly(mut self) -> Self {
176        self.toolchain.push(Version::Nightly);
177        self
178    }
179
180    pub fn add_clippy(mut self) -> Self {
181        self.components.push(Component::Clippy);
182        self
183    }
184
185    pub fn add_fmt(mut self) -> Self {
186        self.components.push(Component::Rustfmt);
187        self
188    }
189
190    pub fn target(mut self, arch: Arch, vendor: Vendor, system: System, abi: Option<Abi>) -> Self {
191        self.target = Some(Target { arch, vendor, system, abi });
192        self
193    }
194}
195
196impl From<Toolchain> for Step<Use> {
197    fn from(value: Toolchain) -> Self {
198        let mut step = Step::uses("actions-rust-lang", "setup-rust-toolchain", "v1")
199            .name("Setup Rust Toolchain");
200
201        let toolchain = value
202            .toolchain
203            .iter()
204            .map(|t| match t {
205                Version::Stable => "stable".to_string(),
206                Version::Nightly => "nightly".to_string(),
207                Version::Custom((major, minor, patch)) => {
208                    format!("{major}.{minor}.{patch}")
209                }
210            })
211            .reduce(|acc, a| format!("{acc}, {a}"));
212
213        let mut input = Input::default();
214
215        if let Some(toolchain) = toolchain {
216            input = input.add("toolchain", toolchain);
217        }
218
219        if let Some(target) = value.target {
220            let target = format!(
221                "{}-{}-{}{}",
222                target.arch,
223                target.vendor,
224                target.system,
225                target.abi.map(|v| v.to_string()).unwrap_or_default(),
226            );
227
228            input = input.add("target", target);
229        }
230
231        if !value.components.is_empty() {
232            let components = value
233                .components
234                .iter()
235                .map(|c| c.to_string())
236                .reduce(|acc, a| format!("{acc}, {a}"))
237                .unwrap_or_default();
238
239            input = input.add("components", components);
240        }
241
242        if let Some(cache) = value.cache {
243            input = input.add("cache", cache);
244        }
245
246        if !value.cache_directories.is_empty() {
247            let cache_directories = value.cache_directories.join("\n");
248            input = input.add("cache-directories", cache_directories);
249        }
250
251        if !value.cache_workspaces.is_empty() {
252            let cache_workspaces = value
253                .cache_workspaces
254                .iter()
255                .fold("".to_string(), |acc, a| format!("{acc}\n{a}"));
256
257            input = input.add("cache-workspaces", cache_workspaces);
258        }
259
260        if let Some(cache_on_failure) = value.cache_on_failure {
261            input = input.add("cache-on-failure", cache_on_failure);
262        }
263
264        if let Some(cache_key) = value.cache_key {
265            input = input.add("cache-key", cache_key);
266        }
267
268        if let Some(matcher) = value.matcher {
269            input = input.add("matcher", matcher);
270        }
271
272        if let Some(rust_flags) = value.rust_flags {
273            input = input.add("rust-flags", rust_flags.to_string());
274        }
275
276        if let Some(override_default) = value.override_default {
277            input = input.add("override", override_default);
278        }
279
280        if !input.is_empty() {
281            step = step.with(input);
282        }
283        step
284    }
285}