irust_repl/
toolchain.rs

1#[cfg(feature = "serde")]
2use serde::{Deserialize, Serialize};
3use std::{fmt::Display, str::FromStr};
4
5#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6#[derive(Debug, Clone, Copy, Default)]
7pub enum ToolChain {
8    Stable,
9    Beta,
10    Nightly,
11    // cargo with no +argument, it can be different from the above
12    #[default]
13    Default,
14}
15
16impl FromStr for ToolChain {
17    type Err = Box<dyn std::error::Error>;
18    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
19        match s.to_lowercase().as_str() {
20            "stable" => Ok(ToolChain::Stable),
21            "beta" => Ok(ToolChain::Beta),
22            "nightly" => Ok(ToolChain::Nightly),
23            "default" => Ok(ToolChain::Default),
24            _ => Err("Unknown toolchain".into()),
25        }
26    }
27}
28
29impl ToolChain {
30    pub(crate) fn as_arg(&self) -> &str {
31        match self {
32            ToolChain::Stable => "+stable",
33            ToolChain::Beta => "+beta",
34            ToolChain::Nightly => "+nightly",
35            // The caller should not call as_arg for the default toolchain
36            ToolChain::Default => unreachable!(),
37        }
38    }
39}
40
41impl Display for ToolChain {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        match self {
44            ToolChain::Stable => write!(f, "stable"),
45            ToolChain::Beta => write!(f, "beta"),
46            ToolChain::Nightly => write!(f, "nightly"),
47            ToolChain::Default => write!(f, "default"),
48        }
49    }
50}