Skip to content

Commit

Permalink
Implement FromStr for Expression and Licensee (#69)
Browse files Browse the repository at this point in the history
Their `from_str` implementations forward to their `parse` methods.
This allows the types to more easily integrate into other parts
of the Rust ecosystem, like clap and serde_with::DeserializeFromStr.
  • Loading branch information
ColonelThirtyTwo authored May 2, 2024
1 parent 9f95309 commit 9dace43
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/expression.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod minimize;
mod parser;

use crate::LicenseReq;
use crate::{error::ParseError, LicenseReq};
pub use minimize::MinimizeError;
use smallvec::SmallVec;
use std::fmt;
Expand Down Expand Up @@ -231,6 +231,13 @@ impl fmt::Display for Expression {
}
}

impl std::str::FromStr for Expression {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse(s)
}
}

impl PartialEq for Expression {
fn eq(&self, o: &Self) -> bool {
// The expressions can be semantically the same but not
Expand Down
8 changes: 8 additions & 0 deletions src/licensee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ impl fmt::Display for Licensee {
}
}

impl std::str::FromStr for Licensee {
type Err = ParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse(s)
}
}

impl Licensee {
/// Creates a licensee from its component parts. Note that use of SPDX's
/// `or_later` is completely ignored for licensees as it only applies
Expand Down

0 comments on commit 9dace43

Please sign in to comment.