diff --git a/sgf/src/go.rs b/sgf/src/go.rs index 0597fbb..1f9f273 100644 --- a/sgf/src/go.rs +++ b/sgf/src/go.rs @@ -70,30 +70,15 @@ use crate::{ date::{self, parse_date_field, Date}, - tree::{parse_collection, ParseSizeError, Size}, - Error, + tree::{parse_collection, Size}, + Error, VerboseNomError, }; +use nom::error::VerboseError; use serde::{Deserialize, Serialize}; use typeshare::typeshare; pub struct Game {} -impl<'a> From>> for Error<'a> { - fn from(err: nom::Err>) -> Self { - match err { - nom::Err::Incomplete(_) => Error::Incomplete, - nom::Err::Error(e) => Error::InvalidSgf(e.clone()), - nom::Err::Failure(e) => Error::InvalidSgf(e.clone()), - } - } -} - -impl<'a> From for Error<'a> { - fn from(_: ParseSizeError) -> Self { - Self::InvalidBoardSize - } -} - #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] #[typeshare] pub enum Rank { @@ -236,8 +221,8 @@ enum PropValue { } */ -pub fn parse_sgf<'a>(input: &'a str) -> Result, Error<'a>> { - let (_, trees) = parse_collection::>(input)?; +pub fn parse_sgf(input: &str) -> Result, Error> { + let (_, trees) = parse_collection::>(input)?; let games = trees .into_iter() diff --git a/sgf/src/lib.rs b/sgf/src/lib.rs index 485267f..73f965a 100644 --- a/sgf/src/lib.rs +++ b/sgf/src/lib.rs @@ -10,11 +10,36 @@ use tree::parse_collection; use thiserror::Error; #[derive(Debug)] -pub enum Error<'a> { +pub enum Error { InvalidField, InvalidBoardSize, Incomplete, - InvalidSgf(nom::error::VerboseError<&'a str>), + InvalidSgf(VerboseNomError), +} + +#[derive(Debug)] +pub struct VerboseNomError(nom::error::VerboseError); + +impl From> for VerboseNomError { + fn from(err: nom::error::VerboseError<&str>) -> Self { + VerboseNomError(nom::error::VerboseError { + errors: err + .errors + .into_iter() + .map(|err| (err.0.to_owned(), err.1)) + .collect(), + }) + } +} + +impl From>> for Error { + fn from(err: nom::Err>) -> Self { + match err { + nom::Err::Incomplete(_) => Error::Incomplete, + nom::Err::Error(e) => Error::InvalidSgf(VerboseNomError::from(e)), + nom::Err::Failure(e) => Error::InvalidSgf(VerboseNomError::from(e)), + } + } } #[derive(Debug, PartialEq, Error)] @@ -37,8 +62,8 @@ pub enum Game { Unsupported(tree::Tree), } -pub fn parse_sgf<'a>(input: &'a str) -> Result, Error<'a>> { - let (_, trees) = parse_collection::>(input)?; +pub fn parse_sgf(input: &str) -> Result, Error> { + let (_, trees) = parse_collection::>(input)?; Ok(trees .into_iter() .map(|t| match t.sequence[0].find_prop("GM") { diff --git a/sgf/src/tree.rs b/sgf/src/tree.rs index 317be80..c0d46ff 100644 --- a/sgf/src/tree.rs +++ b/sgf/src/tree.rs @@ -10,6 +10,12 @@ use nom::{ }; use std::num::ParseIntError; +impl From for Error { + fn from(_: ParseSizeError) -> Self { + Self::InvalidBoardSize + } +} + #[derive(Debug)] pub enum ParseSizeError { ParseIntError(ParseIntError),