diff --git a/Cargo.toml b/Cargo.toml index 46ab426..59b5807 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,23 +19,14 @@ offline = [] [dependencies] serde = { version = "1", default-features = false, optional = true } +[dev-dependencies] +serde_json = "1" + [build-dependencies] reword = "7" serde = { version = "1", features = ["derive"] } serde_json = "1" -[profile.dev.build-override] -opt-level = 3 - -[profile.release.build-override] -opt-level = 3 - -[profile.test.build-override] -opt-level = 3 - -[profile.bench.build-override] -opt-level = 3 - [badges] maintenance = { status = "actively-developed" } diff --git a/EXCEPTION-TEMPLATE b/EXCEPTION-TEMPLATE index 1e91bf1..ed04e3e 100644 --- a/EXCEPTION-TEMPLATE +++ b/EXCEPTION-TEMPLATE @@ -3,14 +3,14 @@ pub struct {ident}; impl crate::Exception for {ident} {{ - fn name(&self) -> &'static str {{ - {name:?} - }} - fn id(&self) -> &'static str {{ {id:?} }} + fn name(&self) -> &'static str {{ + {name:?} + }} + fn text(&self) -> &'static str {{ {text:?} }} diff --git a/LICENSE-TEMPLATE b/LICENSE-TEMPLATE index 755c254..e40f896 100644 --- a/LICENSE-TEMPLATE +++ b/LICENSE-TEMPLATE @@ -3,14 +3,14 @@ pub struct {ident}; impl crate::License for {ident} {{ - fn name(&self) -> &'static str {{ - {name:?} - }} - fn id(&self) -> &'static str {{ {id:?} }} + fn name(&self) -> &'static str {{ + {name:?} + }} + fn text(&self) -> &'static str {{ {text:?} }} diff --git a/build.rs b/build.rs index 481b3a3..4eef011 100644 --- a/build.rs +++ b/build.rs @@ -138,8 +138,8 @@ fn build_licenses_from_json(input: &Path, output: &Path) -> Result<(), Box Result<(), Box() { + println!("{}", license.text()); + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 4e0874b..dbbb545 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,17 +56,17 @@ pub mod exceptions { } /// Base functionality for all licenses. -pub trait License: Debug { - /// The name of the license. - /// - /// Corresponds to the *Full name* column from [spdx.org/licenses](https://spdx.org/licenses/). - fn name(&self) -> &'static str; - +pub trait License { /// The identifier of the license. /// /// Corresponds to the *Identifier* column from [spdx.org/licenses](https://spdx.org/licenses/). fn id(&self) -> &'static str; + /// The name of the license. + /// + /// Corresponds to the *Full name* column from [spdx.org/licenses](https://spdx.org/licenses/). + fn name(&self) -> &'static str; + /// The license text. fn text(&self) -> &'static str; @@ -94,13 +94,13 @@ pub trait License: Debug { } /// Base functionality for all license exceptions. -pub trait Exception: Debug { - /// The name of the exception. - fn name(&self) -> &'static str; - +pub trait Exception { /// The identifier of the exceptions. fn id(&self) -> &'static str; + /// The name of the exception. + fn name(&self) -> &'static str; + /// The exception text. fn text(&self) -> &'static str; @@ -114,6 +114,30 @@ pub trait Exception: Debug { fn see_also(&self) -> &'static [&'static str]; } +impl Display for &dyn License { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + Display::fmt(self.name(), f) + } +} + +impl Display for &dyn Exception { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + Display::fmt(self.name(), f) + } +} + +impl Debug for &dyn License { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + Debug::fmt(self.id(), f) + } +} + +impl Debug for &dyn Exception { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + Debug::fmt(self.id(), f) + } +} + impl FromStr for &dyn License { type Err = ParseError; diff --git a/src/serde.rs b/src/serde.rs index dee206c..0ae80da 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -72,3 +72,16 @@ impl<'de> Deserialize<'de> for &dyn Exception { deserializer.deserialize_str(ExceptionVisitor) } } + +#[cfg(test)] +mod tests { + use crate::License; + + #[test] + fn serde() { + let mit: &dyn License = "MIT".parse().unwrap(); + let s = serde_json::to_string(&mit).unwrap(); + assert_eq!(s, "\"MIT\""); + let _: &dyn License = serde_json::from_str(&s).unwrap(); + } +}