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
use failure::Error;
use std::fmt;
use std::str::FromStr;

/// Represents access level for the to-be publish package. Passed to `wasm-pack publish` as a flag, e.g. `--access=public`.
#[derive(Debug)]
pub enum Access {
    /// Access is granted to all. All unscoped packages *must* be public.
    Public,
    /// Access is restricted, granted via npm permissions. Must be a scoped package.
    Restricted,
}

impl FromStr for Access {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Error> {
        match s {
      "public" => Ok(Access::Public),
      "restricted" => Ok(Access::Restricted),
      "private" => Ok(Access::Restricted),
      _ => bail!("{} is not a supported access level. See https://docs.npmjs.com/cli/access for more information on npm package access levels.", s),
    }
    }
}

impl fmt::Display for Access {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let printable = match *self {
            Access::Public => "--access=public",
            Access::Restricted => "--access=restricted",
        };
        write!(f, "{}", printable)
    }
}