Skip to content

Commit

Permalink
Add cli example and test for serde
Browse files Browse the repository at this point in the history
  • Loading branch information
evenorog committed Sep 12, 2023
1 parent cbb0139 commit 278776d
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 32 deletions.
15 changes: 3 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand Down
8 changes: 4 additions & 4 deletions EXCEPTION-TEMPLATE
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}
}}
Expand Down
8 changes: 4 additions & 4 deletions LICENSE-TEMPLATE
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}
}}
Expand Down
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ fn build_licenses_from_json(input: &Path, output: &Path) -> Result<(), Box<dyn E
f,
include_str!("LICENSE-TEMPLATE"),
ident = license.ident(),
name = license.name,
id = license.license_id,
name = license.name,
text = license.license_text,
header = license.standard_license_header,
osi = license.is_osi_approved,
Expand Down Expand Up @@ -182,8 +182,8 @@ fn build_exceptions_from_json(input: &Path, output: &Path) -> Result<(), Box<dyn
f,
include_str!("EXCEPTION-TEMPLATE"),
ident = exception.ident(),
name = exception.name,
id = exception.license_exception_id,
name = exception.name,
text = exception.license_exception_text,
deprecated = exception.is_deprecated_license_id,
comments = exception.license_comments,
Expand Down
11 changes: 11 additions & 0 deletions examples/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use license::License;
use std::io;

fn main() {
let stdin = io::stdin();
for id in stdin.lines() {
if let Ok(license) = id.unwrap().parse::<&dyn License>() {
println!("{}", license.text());
}
}
}
44 changes: 34 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down
13 changes: 13 additions & 0 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 278776d

Please sign in to comment.