Skip to main content

skill_embed/
scope.rs

1use std::str::FromStr;
2
3use crate::Error;
4
5/// Where skills are installed.
6#[derive(
7    Clone, Copy, Debug, Default, PartialEq, Eq, Hash, strum::Display, strum::IntoStaticStr,
8)]
9#[strum(serialize_all = "lowercase")]
10pub enum Scope {
11    /// The current project directory.
12    #[default]
13    Project,
14    /// The user's home directory.
15    User,
16}
17
18impl Scope {
19    /// The name the `--scope` flag takes.
20    #[must_use]
21    pub fn as_str(self) -> &'static str {
22        self.into()
23    }
24}
25
26/// Parsed by hand rather than derived, so that a mistyped value comes back as
27/// [`Error::UnknownScope`] and a front end never has to name another crate's
28/// error type to match on it.
29impl FromStr for Scope {
30    type Err = Error;
31
32    fn from_str(s: &str) -> Result<Self, Self::Err> {
33        match s {
34            "project" => Ok(Self::Project),
35            "user" => Ok(Self::User),
36            _ => Err(Error::UnknownScope { value: s.to_owned() }),
37        }
38    }
39}