From b753519a3e93c55e898a7dda2a11a3f74de9f586 Mon Sep 17 00:00:00 2001 From: John Turner Date: Sun, 30 Nov 2025 22:44:22 +0000 Subject: [PATCH] add parse method to Parseable trait for easy parsing --- src/lib.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7cef9a8..b7b01a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,12 +21,24 @@ )] #![feature(impl_trait_in_assoc_type)] -use mon::{Parser, input::Input}; +use mon::{ + Parser, + input::{Input, InputIter}, +}; pub trait Parseable<'a, I: Input + 'a> { type Parser: Parser; fn parser() -> Self::Parser; + + fn parse(input: I) -> Result + where + Self: Sized, + { + Self::parser() + .parse_finished(InputIter::new(input)) + .map_err(|e| e.rest()) + } } /// Strongly typed atom and cpv representations. @@ -34,10 +46,9 @@ pub trait Parseable<'a, I: Input + 'a> { /// Create atoms from parsers: /// ``` /// use gentoo_utils::{Parseable, atom::Atom}; -/// use mon::{Parser, input::InputIter}; /// -/// let it = InputIter::new("=app-editors/emacs-31.0-r1"); -/// let emacs = Atom::parser().parse_finished(it).unwrap(); +/// let emacs = Atom::parse("=app-editors/emacs-31.0-r1") +/// .expect("failed to parse atom"); /// /// assert_eq!(emacs.to_string(), "=app-editors/emacs-31.0-r1"); /// ```` @@ -45,15 +56,9 @@ pub trait Parseable<'a, I: Input + 'a> { /// Compare versions: /// ``` /// use gentoo_utils::{Parseable, atom::Cpv}; -/// use mon::{Parser, input::InputIter}; /// -/// let a = Cpv::parser() -/// .parse_finished(InputIter::new("foo/bar-1.0")) -/// .unwrap(); -/// -/// let b = Cpv::parser() -/// .parse_finished(InputIter::new("foo/bar-2.0")) -/// .unwrap(); +/// let a = Cpv::parse("foo/bar-1.0").unwrap(); +/// let b = Cpv::parse("foo/bar-2.0").unwrap(); /// /// assert!(a < b); /// ```