Remove lifetime and borrows from the error type

This commit is contained in:
Savanni D'Gerinel 2023-07-26 20:15:22 -04:00
parent b9808a1862
commit 9533934e05
3 changed files with 40 additions and 24 deletions

View File

@ -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<nom::Err<nom::error::VerboseError<&'a str>>> for Error<'a> {
fn from(err: nom::Err<nom::error::VerboseError<&'a str>>) -> 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<ParseSizeError> 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<Vec<GameTree>, Error<'a>> {
let (_, trees) = parse_collection::<nom::error::VerboseError<&'a str>>(input)?;
pub fn parse_sgf(input: &str) -> Result<Vec<GameTree>, Error> {
let (_, trees) = parse_collection::<nom::error::VerboseError<&str>>(input)?;
let games = trees
.into_iter()

View File

@ -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<String>);
impl From<nom::error::VerboseError<&str>> 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<nom::Err<nom::error::VerboseError<&str>>> for Error {
fn from(err: nom::Err<nom::error::VerboseError<&str>>) -> 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<Vec<Game>, Error<'a>> {
let (_, trees) = parse_collection::<nom::error::VerboseError<&'a str>>(input)?;
pub fn parse_sgf(input: &str) -> Result<Vec<Game>, Error> {
let (_, trees) = parse_collection::<nom::error::VerboseError<&str>>(input)?;
Ok(trees
.into_iter()
.map(|t| match t.sequence[0].find_prop("GM") {

View File

@ -10,6 +10,12 @@ use nom::{
};
use std::num::ParseIntError;
impl From<ParseSizeError> for Error {
fn from(_: ParseSizeError) -> Self {
Self::InvalidBoardSize
}
}
#[derive(Debug)]
pub enum ParseSizeError {
ParseIntError(ParseIntError),