monorepo/sgf/src/lib.rs

71 lines
1.6 KiB
Rust
Raw Normal View History

mod date;
pub use date::Date;
mod go;
2023-07-26 23:33:50 +00:00
pub use go::{GameTree, GameType, Rank};
mod tree;
2023-07-26 23:33:50 +00:00
use tree::parse_collection;
2023-06-22 14:04:52 +00:00
use thiserror::Error;
2023-07-26 23:33:50 +00:00
#[derive(Debug)]
pub enum Error<'a> {
InvalidField,
InvalidBoardSize,
Incomplete,
InvalidSgf(nom::error::VerboseError<&'a str>),
}
2023-06-22 14:04:52 +00:00
#[derive(Debug, PartialEq, Error)]
pub enum ParseError {
#[error("An unknown error was found")]
NomError(nom::error::Error<String>),
2023-06-22 14:04:52 +00:00
}
2023-06-23 03:58:16 +00:00
impl From<nom::error::Error<&str>> for ParseError {
fn from(err: nom::error::Error<&str>) -> Self {
Self::NomError(nom::error::Error {
input: err.input.to_owned(),
code: err.code.clone(),
})
2023-06-23 03:58:16 +00:00
}
}
2023-07-26 23:33:50 +00:00
pub enum Game {
Go(go::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)?;
Ok(trees
.into_iter()
.map(|t| match t.sequence[0].find_prop("GM") {
Some(prop) if prop.values == vec!["1".to_owned()] => Game::Go(go::Game {}),
_ => Game::Unsupported(t),
})
.collect::<Vec<Game>>())
}
/*
impl From<(&str, VerboseErrorKind)> for
impl From<nom::error::VerboseError<&str>> for ParseError {
fn from(err: nom::error::VerboseError<&str>) -> Self {
Self::NomErrors(
err.errors
.into_iter()
.map(|err| ParseError::from(err))
.collect(),
)
/*
Self::NomError(nom::error::Error {
input: err.input.to_owned(),
code: err.code.clone(),
})
*/
}
}
*/