From 2b4e84c0f1788d6cf3319b3cb558c714a39085c7 Mon Sep 17 00:00:00 2001 From: Alex Parrill Date: Tue, 30 Apr 2024 16:51:35 -0400 Subject: [PATCH] Implement FromStr for Expression and Licensee 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. --- src/expression.rs | 9 ++++++++- src/licensee.rs | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/expression.rs b/src/expression.rs index dfef32a..7daa54d 100644 --- a/src/expression.rs +++ b/src/expression.rs @@ -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; @@ -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::parse(s) + } +} + impl PartialEq for Expression { fn eq(&self, o: &Self) -> bool { // The expressions can be semantically the same but not diff --git a/src/licensee.rs b/src/licensee.rs index e0db102..6ec77aa 100644 --- a/src/licensee.rs +++ b/src/licensee.rs @@ -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::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