diff --git a/src/manifest/npm/mod.rs b/src/manifest/npm/mod.rs index c8d65821..789cef06 100644 --- a/src/manifest/npm/mod.rs +++ b/src/manifest/npm/mod.rs @@ -7,6 +7,10 @@ pub use self::commonjs::CommonJSPackage; pub use self::esmodules::ESModulesPackage; pub use self::nomodules::NoModulesPackage; +use error::Error; +use std::fmt; +use std::str::FromStr; + #[derive(Serialize)] #[serde(untagged)] pub enum NpmPackage { @@ -14,3 +18,33 @@ pub enum NpmPackage { ESModulesPackage(ESModulesPackage), NoModulesPackage(NoModulesPackage), } + +impl Default for NpmPackage { + fn default() -> NpmPackage { + ESModulesPackage {} + } +} + +impl FromStr for NpmPackage { + type Err = Error; + + fn from_str(s: &str) -> Result { + match s { + "nodejs" => Ok(NpmPackage::CommonJSPackage), + "nomodules" => Ok(NpmPackage::NoModulesPackage), + _ => Err(Error::Unsupported { + message: format!("{} is not a supported target module type.", s), + }), + } + } +} + +impl fmt::Display for NpmPackage { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let printable = match *self { + CommonJSPackage => "--nodejs", + NoModulesPackage => "--no-modules", + }; + write!(f, "{}", printable) + } +}