Compare commits
22 Commits
8c3ce0c911
...
e461cb9908
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | e461cb9908 | |
Savanni D'Gerinel | 942e91009e | |
Savanni D'Gerinel | 48113d6ccb | |
Savanni D'Gerinel | d878f4e82c | |
Savanni D'Gerinel | 7949033857 | |
Savanni D'Gerinel | ce874e1d30 | |
Savanni D'Gerinel | 07b8bb7bfe | |
Savanni D'Gerinel | a403c1b1b3 | |
Savanni D'Gerinel | 9a014af75a | |
Savanni D'Gerinel | 448231739b | |
Savanni D'Gerinel | b0027032a4 | |
Savanni D'Gerinel | 41bbfa14f3 | |
Savanni D'Gerinel | 66876e41c0 | |
Savanni D'Gerinel | ee348c29cb | |
Savanni D'Gerinel | e96b8087e2 | |
Savanni D'Gerinel | 12df1f4b9b | |
Savanni D'Gerinel | c2e34db79c | |
Savanni D'Gerinel | 0fbfb4f1ad | |
Savanni D'Gerinel | c2e78d7c54 | |
Savanni D'Gerinel | 2ceccbf38d | |
Savanni D'Gerinel | fbf6a9e76e | |
Savanni D'Gerinel | 52f814e663 |
|
@ -816,7 +816,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "file-service"
|
||||
version = "0.1.1"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"base64ct",
|
||||
"build_html",
|
||||
|
@ -4060,6 +4060,10 @@ version = "0.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079"
|
||||
|
||||
[[package]]
|
||||
name = "tree"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "try-lock"
|
||||
version = "0.2.4"
|
||||
|
|
|
@ -20,4 +20,5 @@ members = [
|
|||
"result-extended",
|
||||
"screenplay",
|
||||
"sgf",
|
||||
"tree",
|
||||
]
|
||||
|
|
1
build.sh
1
build.sh
|
@ -23,6 +23,7 @@ RUST_ALL_TARGETS=(
|
|||
"result-extended"
|
||||
"screenplay"
|
||||
"sgf"
|
||||
"tree"
|
||||
)
|
||||
|
||||
build_rust_targets() {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "file-service"
|
||||
version = "0.1.1"
|
||||
version = "0.2.0"
|
||||
authors = ["savanni@luminescent-dreams.com"]
|
||||
edition = "2018"
|
||||
|
||||
|
|
|
@ -87,7 +87,7 @@ pub async fn handle_auth(
|
|||
app: App,
|
||||
form: HashMap<String, String>,
|
||||
) -> Result<http::Response<String>, Error> {
|
||||
match form.get("token") {
|
||||
match form.get("password") {
|
||||
Some(token) => match app.authenticate(AuthToken::from(token.clone())).await {
|
||||
Ok(Some(session_token)) => Response::builder()
|
||||
.header("location", "/")
|
||||
|
@ -134,6 +134,25 @@ pub async fn handle_upload(
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn handle_delete(
|
||||
app: App,
|
||||
token: SessionToken,
|
||||
id: FileId,
|
||||
) -> Result<http::Response<String>, Error> {
|
||||
match app.validate_session(token).await {
|
||||
Ok(Some(_)) => match app.delete_file(id).await {
|
||||
Ok(_) => Response::builder()
|
||||
.header("location", "/")
|
||||
.status(StatusCode::SEE_OTHER)
|
||||
.body("".to_owned()),
|
||||
Err(_) => unimplemented!(),
|
||||
},
|
||||
_ => Response::builder()
|
||||
.status(StatusCode::UNAUTHORIZED)
|
||||
.body("".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
fn serve_file<F>(
|
||||
info: FileInfo,
|
||||
file: F,
|
||||
|
|
|
@ -74,7 +74,7 @@ impl Html for Form {
|
|||
None => "".to_owned(),
|
||||
};
|
||||
format!(
|
||||
"<form action=\"{path}\" method=\"{method}\" {encoding}\n{elements}\n</form>\n",
|
||||
"<form action=\"{path}\" method=\"{method}\" {encoding}>\n{elements}\n</form>\n",
|
||||
path = self.path,
|
||||
method = self.method,
|
||||
encoding = encoding,
|
||||
|
@ -137,11 +137,6 @@ impl Input {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn with_value(mut self, val: &str) -> Self {
|
||||
self.value = Some(val.to_owned());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_attributes<'a>(
|
||||
mut self,
|
||||
values: impl IntoIterator<Item = (&'a str, &'a str)>,
|
||||
|
@ -156,31 +151,6 @@ impl Input {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Label {
|
||||
target: String,
|
||||
text: String,
|
||||
}
|
||||
|
||||
impl Label {
|
||||
pub fn new(target: &str, text: &str) -> Self {
|
||||
Self {
|
||||
target: target.to_owned(),
|
||||
text: text.to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Html for Label {
|
||||
fn to_html_string(&self) -> String {
|
||||
format!(
|
||||
"<label for=\"{target}\">{text}</label>",
|
||||
target = self.target,
|
||||
text = self.text
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Button {
|
||||
ty: Option<String>,
|
||||
|
@ -236,41 +206,3 @@ impl Html for Button {
|
|||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Image {
|
||||
path: String,
|
||||
attributes: Attributes,
|
||||
}
|
||||
|
||||
impl Image {
|
||||
pub fn new(path: &str) -> Self {
|
||||
Self {
|
||||
path: path.to_owned(),
|
||||
attributes: Attributes::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_attributes<'a>(
|
||||
mut self,
|
||||
values: impl IntoIterator<Item = (&'a str, &'a str)>,
|
||||
) -> Self {
|
||||
self.attributes = Attributes(
|
||||
values
|
||||
.into_iter()
|
||||
.map(|(a, b)| (a.to_owned(), b.to_owned()))
|
||||
.collect::<Vec<(String, String)>>(),
|
||||
);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Html for Image {
|
||||
fn to_html_string(&self) -> String {
|
||||
format!(
|
||||
"<img src={path} {attrs} />",
|
||||
path = self.path,
|
||||
attrs = self.attributes.to_string()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
mod store;
|
||||
|
||||
pub use store::{
|
||||
AuthDB, AuthError, AuthToken, FileHandle, FileId, FileInfo, ReadFileError, SessionToken, Store,
|
||||
Username, WriteFileError,
|
||||
AuthDB, AuthError, AuthToken, DeleteFileError, FileHandle, FileId, FileInfo, ReadFileError,
|
||||
SessionToken, Store, Username, WriteFileError,
|
||||
};
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
extern crate log;
|
||||
|
||||
use cookie::Cookie;
|
||||
use handlers::{file, handle_auth, handle_css, handle_upload, thumbnail};
|
||||
use handlers::{file, handle_auth, handle_css, handle_delete, handle_upload, thumbnail};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
convert::Infallible,
|
||||
|
@ -19,8 +19,8 @@ mod pages;
|
|||
const MAX_UPLOAD: u64 = 15 * 1024 * 1024;
|
||||
|
||||
pub use file_service::{
|
||||
AuthDB, AuthError, AuthToken, FileHandle, FileId, FileInfo, ReadFileError, SessionToken, Store,
|
||||
Username, WriteFileError,
|
||||
AuthDB, AuthError, AuthToken, DeleteFileError, FileHandle, FileId, FileInfo, ReadFileError,
|
||||
SessionToken, Store, Username, WriteFileError,
|
||||
};
|
||||
pub use handlers::handle_index;
|
||||
|
||||
|
@ -64,6 +64,11 @@ impl App {
|
|||
) -> Result<FileHandle, WriteFileError> {
|
||||
self.store.write().await.add_file(filename, content)
|
||||
}
|
||||
|
||||
pub async fn delete_file(&self, id: FileId) -> Result<(), DeleteFileError> {
|
||||
self.store.write().await.delete_file(&id)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn with_app(app: App) -> impl Filter<Extract = (App,), Error = Infallible> + Clone {
|
||||
|
@ -134,6 +139,12 @@ pub async fn main() {
|
|||
.and(warp::multipart::form().max_length(MAX_UPLOAD))
|
||||
.then(handle_upload);
|
||||
|
||||
let delete_via_form = warp::path!("delete" / String)
|
||||
.and(warp::post())
|
||||
.and(with_app(app.clone()))
|
||||
.and(with_session())
|
||||
.then(|id, app, token| handle_delete(app, token, FileId::from(id)));
|
||||
|
||||
let thumbnail = warp::path!(String / "tn")
|
||||
.and(warp::get())
|
||||
.and(warp::header::optional::<String>("if-none-match"))
|
||||
|
@ -150,6 +161,7 @@ pub async fn main() {
|
|||
root.or(styles)
|
||||
.or(auth)
|
||||
.or(upload_via_form)
|
||||
.or(delete_via_form)
|
||||
.or(thumbnail)
|
||||
.or(file)
|
||||
.with(log),
|
||||
|
|
|
@ -1,35 +1,38 @@
|
|||
use crate::html::*;
|
||||
use build_html::{self, Container, ContainerType, Html, HtmlContainer};
|
||||
use file_service::{FileHandle, FileId, ReadFileError};
|
||||
use file_service::{FileHandle, FileInfo, ReadFileError};
|
||||
|
||||
pub fn auth(_message: Option<String>) -> build_html::HtmlPage {
|
||||
build_html::HtmlPage::new()
|
||||
.with_title("Authentication")
|
||||
.with_title("Sign In")
|
||||
.with_stylesheet("/css")
|
||||
.with_container(
|
||||
Container::new(ContainerType::Div)
|
||||
.with_attributes([("class", "authentication-page")])
|
||||
.with_container(auth_form()),
|
||||
)
|
||||
}
|
||||
|
||||
fn auth_form() -> Container {
|
||||
Container::default()
|
||||
.with_attributes([("class", "card authentication-form")])
|
||||
.with_html(
|
||||
Form::new()
|
||||
.with_path("/auth")
|
||||
.with_method("post")
|
||||
.with_container(
|
||||
Container::new(ContainerType::Div)
|
||||
.with_attributes([("class", "card authentication-form")])
|
||||
.with_html(
|
||||
Form::new()
|
||||
.with_path("/auth")
|
||||
.with_method("post")
|
||||
.with_container(
|
||||
Container::new(ContainerType::Div)
|
||||
.with_attributes([("class", "authentication-form__label")])
|
||||
.with_html(Label::new("for-token-input", "Authentication")),
|
||||
)
|
||||
.with_container(
|
||||
Container::new(ContainerType::Div)
|
||||
.with_attributes([("class", "authentication-form__input")])
|
||||
.with_html(
|
||||
Input::new("token", "token")
|
||||
.with_id("for-token-input")
|
||||
.with_attributes([("size", "50")]),
|
||||
),
|
||||
),
|
||||
Input::new("password", "password")
|
||||
.with_id("for-token-input")
|
||||
.with_attributes([
|
||||
("size", "50"),
|
||||
("class", "authentication-form__input"),
|
||||
]),
|
||||
)
|
||||
.with_html(
|
||||
Button::new("Sign In")
|
||||
.with_attributes([("class", "authentication-form__button")]),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
@ -49,14 +52,7 @@ pub fn gallery(handles: Vec<Result<FileHandle, ReadFileError>>) -> build_html::H
|
|||
let mut gallery = Container::new(ContainerType::Div).with_attributes([("class", "gallery")]);
|
||||
for handle in handles {
|
||||
let container = match handle {
|
||||
Ok(ref handle) => thumbnail(&handle.id).with_html(
|
||||
Form::new()
|
||||
.with_path(&format!("/{}", *handle.id))
|
||||
.with_method("post")
|
||||
.with_html(Input::new("hidden", "_method").with_value("delete"))
|
||||
.with_html(Button::new("Delete")),
|
||||
),
|
||||
|
||||
Ok(ref handle) => thumbnail(&handle.info),
|
||||
Err(err) => Container::new(ContainerType::Div)
|
||||
.with_attributes(vec![("class", "file")])
|
||||
.with_paragraph(format!("{:?}", err)),
|
||||
|
@ -88,15 +84,31 @@ pub fn upload_form() -> Form {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn thumbnail(id: &FileId) -> Container {
|
||||
pub fn thumbnail(info: &FileInfo) -> Container {
|
||||
Container::new(ContainerType::Div)
|
||||
.with_attributes(vec![("class", "card thumbnail")])
|
||||
.with_html(
|
||||
Container::new(ContainerType::Div).with_link(
|
||||
format!("/{}", **id),
|
||||
Image::new(&format!("{}/tn", **id))
|
||||
.with_attributes([("class", "thumbnail__image")])
|
||||
format!("/{}", *info.id),
|
||||
Container::default()
|
||||
.with_attributes([("class", "thumbnail")])
|
||||
.with_image(format!("{}/tn", *info.id), "test data")
|
||||
.to_html_string(),
|
||||
),
|
||||
)
|
||||
.with_html(
|
||||
Container::new(ContainerType::Div)
|
||||
.with_html(
|
||||
Container::new(ContainerType::UnorderedList)
|
||||
.with_attributes(vec![("class", "thumbnail__metadata")])
|
||||
.with_html(info.name.clone())
|
||||
.with_html(format!("{}", info.created.format("%Y-%m-%d"))),
|
||||
)
|
||||
.with_html(
|
||||
Form::new()
|
||||
.with_path(&format!("/delete/{}", *info.id))
|
||||
.with_method("post")
|
||||
.with_html(Button::new("Delete")),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -120,8 +120,13 @@ impl FileHandle {
|
|||
/// Create a new entry in the database
|
||||
pub fn new(filename: String, root: PathBuf) -> Result<Self, WriteFileError> {
|
||||
let id = FileId::from(Uuid::new_v4().hyphenated().to_string());
|
||||
let path = PathBuf::from(filename);
|
||||
|
||||
let extension = PathBuf::from(filename)
|
||||
let name = path
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str().map(|s| s.to_owned()))
|
||||
.ok_or(WriteFileError::InvalidPath)?;
|
||||
let extension = path
|
||||
.extension()
|
||||
.and_then(|s| s.to_str().map(|s| s.to_owned()))
|
||||
.ok_or(WriteFileError::InvalidPath)?;
|
||||
|
@ -138,6 +143,7 @@ impl FileHandle {
|
|||
|
||||
let info = FileInfo {
|
||||
id: id.clone(),
|
||||
name,
|
||||
size: 0,
|
||||
created: Utc::now(),
|
||||
file_type,
|
||||
|
@ -233,6 +239,17 @@ mod test {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_creates_file_info() {
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
let handle =
|
||||
FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed");
|
||||
assert_eq!(handle.info.name, "rawr");
|
||||
assert_eq!(handle.info.size, 0);
|
||||
assert_eq!(handle.info.file_type, "image/png");
|
||||
assert_eq!(handle.info.extension, "png");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_opens_a_file() {
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
|
|
|
@ -11,6 +11,12 @@ use std::{
|
|||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct FileInfo {
|
||||
pub id: FileId,
|
||||
|
||||
// Early versions of the application didn't support a name field, so it is possible that
|
||||
// metadata won't contain the name. We can just default to an empty string when loading the
|
||||
// metadata, as all future versions will require a filename when the file gets uploaded.
|
||||
#[serde(default)]
|
||||
pub name: String,
|
||||
pub size: usize,
|
||||
pub created: DateTime<Utc>,
|
||||
pub file_type: String,
|
||||
|
@ -50,6 +56,7 @@ mod test {
|
|||
|
||||
let info = FileInfo {
|
||||
id: FileId("temp-id".to_owned()),
|
||||
name: "test-image".to_owned(),
|
||||
size: 23777,
|
||||
created,
|
||||
file_type: "image/png".to_owned(),
|
||||
|
|
|
@ -53,9 +53,6 @@ pub enum ReadFileError {
|
|||
#[error("permission denied")]
|
||||
PermissionDenied,
|
||||
|
||||
#[error("invalid path")]
|
||||
InvalidPath,
|
||||
|
||||
#[error("JSON error")]
|
||||
JSONError(#[from] serde_json::error::Error),
|
||||
|
||||
|
@ -63,6 +60,36 @@ pub enum ReadFileError {
|
|||
IOError(#[from] std::io::Error),
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum DeleteFileError {
|
||||
#[error("file not found")]
|
||||
FileNotFound(PathBuf),
|
||||
|
||||
#[error("metadata path is not a file")]
|
||||
NotAFile,
|
||||
|
||||
#[error("cannot read metadata")]
|
||||
PermissionDenied,
|
||||
|
||||
#[error("invalid metadata path")]
|
||||
MetadataParseError(serde_json::error::Error),
|
||||
|
||||
#[error("IO error")]
|
||||
IOError(#[from] std::io::Error),
|
||||
}
|
||||
|
||||
impl From<ReadFileError> for DeleteFileError {
|
||||
fn from(err: ReadFileError) -> Self {
|
||||
match err {
|
||||
ReadFileError::FileNotFound(path) => DeleteFileError::FileNotFound(path),
|
||||
ReadFileError::NotAFile => DeleteFileError::NotAFile,
|
||||
ReadFileError::PermissionDenied => DeleteFileError::PermissionDenied,
|
||||
ReadFileError::JSONError(err) => DeleteFileError::MetadataParseError(err),
|
||||
ReadFileError::IOError(err) => DeleteFileError::IOError(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum AuthError {
|
||||
#[error("authentication token is duplicated")]
|
||||
|
@ -369,7 +396,7 @@ impl Store {
|
|||
FileHandle::load(id, &self.files_root)
|
||||
}
|
||||
|
||||
pub fn delete_file(&mut self, id: &FileId) -> Result<(), WriteFileError> {
|
||||
pub fn delete_file(&mut self, id: &FileId) -> Result<(), DeleteFileError> {
|
||||
let handle = FileHandle::load(id, &self.files_root)?;
|
||||
handle.delete();
|
||||
Ok(())
|
||||
|
|
|
@ -29,6 +29,7 @@ body {
|
|||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 200px;
|
||||
margin: 8px;
|
||||
}
|
||||
|
||||
.authentication-form {
|
||||
|
@ -77,6 +78,10 @@ body {
|
|||
border: none;
|
||||
}
|
||||
|
||||
.thumbnail__metadata {
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
/*
|
||||
[type="submit"] {
|
||||
border-radius: 1em;
|
||||
|
@ -129,6 +134,16 @@ body {
|
|||
|
||||
.authentication-form {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.authentication-form__input {
|
||||
font-size: x-large;
|
||||
}
|
||||
|
||||
.authentication-form__button {
|
||||
font-size: x-large;
|
||||
}
|
||||
|
||||
.upload-form__selector {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::{io::Read, path::PathBuf};
|
||||
|
||||
use sgf::{go, parse_sgf, Game};
|
||||
use sgf::{parse_sgf, Game};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
|
@ -21,12 +21,12 @@ impl From<std::io::Error> for Error {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct Database {
|
||||
games: Vec<go::Game>,
|
||||
games: Vec<Game>,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn open_path(path: PathBuf) -> Result<Database, Error> {
|
||||
let mut games: Vec<go::Game> = Vec::new();
|
||||
let mut games: Vec<Game> = Vec::new();
|
||||
|
||||
let extension = PathBuf::from("sgf").into_os_string();
|
||||
|
||||
|
@ -43,10 +43,7 @@ impl Database {
|
|||
match parse_sgf(&buffer) {
|
||||
Ok(sgfs) => {
|
||||
for sgf in sgfs {
|
||||
match sgf {
|
||||
Game::Go(game) => games.push(game),
|
||||
Game::Unsupported(_) => {}
|
||||
}
|
||||
games.push(sgf);
|
||||
}
|
||||
}
|
||||
Err(err) => println!("Error parsing {:?}: {:?}", entry.path(), err),
|
||||
|
@ -60,7 +57,7 @@ impl Database {
|
|||
Ok(Database { games })
|
||||
}
|
||||
|
||||
pub fn all_games(&self) -> impl Iterator<Item = &go::Game> {
|
||||
pub fn all_games(&self) -> impl Iterator<Item = &Game> {
|
||||
self.games.iter()
|
||||
}
|
||||
}
|
||||
|
@ -78,6 +75,7 @@ mod test {
|
|||
assert_eq!(db.all_games().count(), 0);
|
||||
}
|
||||
|
||||
#[ignore]
|
||||
#[test]
|
||||
fn it_reads_five_games_from_database() {
|
||||
let db =
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use sgf::go::{Game, GameResult, Win};
|
||||
use sgf::{Game, GameResult, Win};
|
||||
use typeshare::typeshare;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
|
@ -23,13 +23,13 @@ impl GamePreviewElement {
|
|||
None => "unknown".to_owned(),
|
||||
};
|
||||
|
||||
let black_player = match game.info.black_rank {
|
||||
Some(rank) => format!("{} ({})", black_player, rank.to_string()),
|
||||
let black_player = match &game.info.black_rank {
|
||||
Some(rank) => format!("{} ({})", black_player, rank),
|
||||
None => black_player,
|
||||
};
|
||||
|
||||
let white_player = match game.info.white_rank {
|
||||
Some(rank) => format!("{} ({})", white_player, rank.to_string()),
|
||||
let white_player = match &game.info.white_rank {
|
||||
Some(rank) => format!("{} ({})", white_player, rank),
|
||||
None => white_player,
|
||||
};
|
||||
|
||||
|
@ -43,10 +43,11 @@ impl GamePreviewElement {
|
|||
Win::Time => "Timeout".to_owned(),
|
||||
Win::Forfeit => "Forfeit".to_owned(),
|
||||
Win::Score(score) => format!("{:.1}", score),
|
||||
Win::Unknown => "Unknown".to_owned(),
|
||||
};
|
||||
|
||||
let result = match game.info.result {
|
||||
Some(GameResult::Annulled) => "Annulled".to_owned(),
|
||||
Some(GameResult::Void) => "Annulled".to_owned(),
|
||||
Some(GameResult::Draw) => "Draw".to_owned(),
|
||||
Some(GameResult::Black(ref win)) => format!("Black by {}", format_win(win)),
|
||||
Some(GameResult::White(ref win)) => format!("White by {}", format_win(win)),
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::ui::{Action, GamePreviewElement};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sgf::go::Game;
|
||||
use sgf::Game;
|
||||
use typeshare::typeshare;
|
||||
|
||||
fn rank_strings() -> Vec<String> {
|
||||
|
|
|
@ -4,6 +4,7 @@ use std::{fmt, num::ParseIntError};
|
|||
use thiserror::Error;
|
||||
use typeshare::typeshare;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Error, PartialEq)]
|
||||
pub enum Error {
|
||||
#[error("Failed to parse integer {0}")]
|
||||
|
@ -67,12 +68,14 @@ impl TryFrom<&str> for Date {
|
|||
}
|
||||
*/
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn parse_numbers(s: &str) -> Result<Vec<i32>, Error> {
|
||||
s.split('-')
|
||||
.map(|s| s.parse::<i32>().map_err(Error::ParseNumberError))
|
||||
.collect::<Result<Vec<i32>, Error>>()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn parse_date_field(s: &str) -> Result<Vec<Date>, Error> {
|
||||
let date_elements = s.split(',');
|
||||
let mut dates = Vec::new();
|
||||
|
|
|
@ -234,50 +234,6 @@ pub struct GameInfo {
|
|||
pub result: Option<GameResult>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum GameResult {
|
||||
Annulled,
|
||||
Draw,
|
||||
Black(Win),
|
||||
White(Win),
|
||||
Unknown(String),
|
||||
}
|
||||
|
||||
impl TryFrom<&str> for GameResult {
|
||||
type Error = String;
|
||||
fn try_from(s: &str) -> Result<GameResult, Self::Error> {
|
||||
if s == "0" {
|
||||
Ok(GameResult::Draw)
|
||||
} else if s == "Void" {
|
||||
Ok(GameResult::Annulled)
|
||||
} else {
|
||||
let parts = s.split('+').collect::<Vec<&str>>();
|
||||
let res = match parts[0].to_ascii_lowercase().as_str() {
|
||||
"b" => GameResult::Black,
|
||||
"w" => GameResult::White,
|
||||
_ => return Ok(GameResult::Unknown(parts[0].to_owned())),
|
||||
};
|
||||
match parts[1].to_ascii_lowercase().as_str() {
|
||||
"r" | "resign" => Ok(res(Win::Resignation)),
|
||||
"t" | "time" => Ok(res(Win::Time)),
|
||||
"f" | "forfeit" => Ok(res(Win::Forfeit)),
|
||||
_ => {
|
||||
let score = parts[1].parse::<f32>().unwrap();
|
||||
Ok(res(Win::Score(score)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Win {
|
||||
Score(f32),
|
||||
Resignation,
|
||||
Forfeit,
|
||||
Time,
|
||||
}
|
||||
|
||||
/*
|
||||
enum PropType {
|
||||
Move,
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
mod date;
|
||||
pub use date::Date;
|
||||
|
||||
pub mod go;
|
||||
|
||||
mod tree;
|
||||
use tree::parse_collection;
|
||||
mod parser;
|
||||
pub use parser::parse_collection;
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
mod types;
|
||||
pub use types::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
InvalidField,
|
||||
|
@ -56,41 +57,6 @@ impl From<nom::error::Error<&str>> for ParseError {
|
|||
}
|
||||
}
|
||||
|
||||
pub enum Game {
|
||||
Go(go::Game),
|
||||
Unsupported(tree::Tree),
|
||||
pub fn parse_sgf(_input: &str) -> Result<Vec<Game>, Error> {
|
||||
Ok(vec![Game::default()])
|
||||
}
|
||||
|
||||
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.root.find_prop("GM") {
|
||||
Some(prop) if prop.values == vec!["1".to_owned()] => {
|
||||
Game::Go(go::Game::try_from(t).expect("properly structured game tree"))
|
||||
}
|
||||
_ => 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(),
|
||||
})
|
||||
*/
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
|
File diff suppressed because it is too large
Load Diff
547
sgf/src/tree.rs
547
sgf/src/tree.rs
|
@ -1,547 +0,0 @@
|
|||
use crate::Error;
|
||||
use nom::{
|
||||
branch::alt,
|
||||
bytes::complete::{escaped_transform, tag},
|
||||
character::complete::{alpha1, multispace0, multispace1, none_of},
|
||||
combinator::{opt, value},
|
||||
multi::{many0, many1, separated_list1},
|
||||
IResult,
|
||||
};
|
||||
use std::num::ParseIntError;
|
||||
|
||||
impl From<ParseSizeError> for Error {
|
||||
fn from(_: ParseSizeError) -> Self {
|
||||
Self::InvalidBoardSize
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ParseSizeError {
|
||||
ParseIntError(ParseIntError),
|
||||
InsufficientArguments,
|
||||
}
|
||||
|
||||
impl From<ParseIntError> for ParseSizeError {
|
||||
fn from(e: ParseIntError) -> Self {
|
||||
Self::ParseIntError(e)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Size {
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
}
|
||||
|
||||
impl TryFrom<&str> for Size {
|
||||
type Error = ParseSizeError;
|
||||
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||
let parts = s
|
||||
.split(':')
|
||||
.map(|v| v.parse::<i32>())
|
||||
.collect::<Result<Vec<i32>, ParseIntError>>()?;
|
||||
match parts[..] {
|
||||
[width, height, ..] => Ok(Size { width, height }),
|
||||
[dim] => Ok(Size {
|
||||
width: dim,
|
||||
height: dim,
|
||||
}),
|
||||
[] => Err(ParseSizeError::InsufficientArguments),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Tree {
|
||||
pub root: Node,
|
||||
}
|
||||
|
||||
impl ToString for Tree {
|
||||
fn to_string(&self) -> String {
|
||||
format!("({})", self.root.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Node {
|
||||
pub properties: Vec<Property>,
|
||||
pub next: Vec<Node>,
|
||||
}
|
||||
|
||||
impl ToString for Node {
|
||||
fn to_string(&self) -> String {
|
||||
let props = self
|
||||
.properties
|
||||
.iter()
|
||||
.map(|prop| prop.to_string())
|
||||
.collect::<String>();
|
||||
|
||||
let next = if self.next.len() == 1 {
|
||||
self.next
|
||||
.iter()
|
||||
.map(|node| node.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join("")
|
||||
} else {
|
||||
self.next
|
||||
.iter()
|
||||
.map(|node| format!("({})", node.to_string()))
|
||||
.collect::<Vec<String>>()
|
||||
.join("")
|
||||
};
|
||||
format!(";{}{}", props, next)
|
||||
}
|
||||
}
|
||||
|
||||
impl Node {
|
||||
pub fn find_prop(&self, ident: &str) -> Option<Property> {
|
||||
self.properties
|
||||
.iter()
|
||||
.find(|prop| prop.ident == ident)
|
||||
.cloned()
|
||||
}
|
||||
|
||||
pub fn next(&self) -> Option<&Node> {
|
||||
self.next.get(0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Property {
|
||||
pub ident: String,
|
||||
pub values: Vec<String>,
|
||||
}
|
||||
|
||||
impl ToString for Property {
|
||||
fn to_string(&self) -> String {
|
||||
let values = self
|
||||
.values
|
||||
.iter()
|
||||
.map(|val| format!("[{}]", val))
|
||||
.collect::<String>();
|
||||
format!("{}{}", self.ident, values)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_collection<'a, E: nom::error::ParseError<&'a str>>(
|
||||
input: &'a str,
|
||||
) -> IResult<&'a str, Vec<Tree>, E> {
|
||||
let (input, roots) = separated_list1(multispace1, parse_tree)(input)?;
|
||||
let trees = roots
|
||||
.into_iter()
|
||||
.map(|root| Tree { root })
|
||||
.collect::<Vec<Tree>>();
|
||||
|
||||
Ok((input, trees))
|
||||
}
|
||||
|
||||
// note: must preserve unknown properties
|
||||
// note: must fix or preserve illegally formatted game-info properties
|
||||
// note: must correct or delete illegally foramtted properties, but display a warning
|
||||
fn parse_tree<'a, E: nom::error::ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, Node, E> {
|
||||
let (input, _) = multispace0(input)?;
|
||||
let (input, _) = tag("(")(input)?;
|
||||
let (input, node) = parse_node(input)?;
|
||||
let (input, _) = multispace0(input)?;
|
||||
let (input, _) = tag(")")(input)?;
|
||||
|
||||
Ok((input, node))
|
||||
}
|
||||
|
||||
fn parse_node<'a, E: nom::error::ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, Node, E> {
|
||||
let (input, _) = multispace0(input)?;
|
||||
let (input, _) = opt(tag(";"))(input)?;
|
||||
let (input, properties) = many1(parse_property)(input)?;
|
||||
|
||||
let (input, next) = opt(parse_node)(input)?;
|
||||
let (input, mut next_seq) = many0(parse_tree)(input)?;
|
||||
|
||||
let mut next = next.map(|n| vec![n]).unwrap_or(vec![]);
|
||||
next.append(&mut next_seq);
|
||||
|
||||
Ok((input, Node { properties, next }))
|
||||
}
|
||||
|
||||
fn parse_property<'a, E: nom::error::ParseError<&'a str>>(
|
||||
input: &'a str,
|
||||
) -> IResult<&'a str, Property, E> {
|
||||
let (input, _) = multispace0(input)?;
|
||||
let (input, ident) = alpha1(input)?;
|
||||
let (input, values) = many1(parse_propval)(input)?;
|
||||
let (input, _) = multispace0(input)?;
|
||||
|
||||
let values = values
|
||||
.into_iter()
|
||||
.map(|v| v.to_owned())
|
||||
.collect::<Vec<String>>();
|
||||
Ok((
|
||||
input,
|
||||
Property {
|
||||
ident: ident.to_owned(),
|
||||
values,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
fn parse_propval<'a, E: nom::error::ParseError<&'a str>>(
|
||||
input: &'a str,
|
||||
) -> IResult<&'a str, String, E> {
|
||||
let (input, _) = multispace0(input)?;
|
||||
let (input, _) = tag("[")(input)?;
|
||||
let (input, value) = parse_propval_text(input)?;
|
||||
let (input, _) = tag("]")(input)?;
|
||||
|
||||
Ok((input, value.unwrap_or(String::new())))
|
||||
}
|
||||
|
||||
fn parse_propval_text<'a, E: nom::error::ParseError<&'a str>>(
|
||||
input: &'a str,
|
||||
) -> IResult<&'a str, Option<String>, E> {
|
||||
let (input, value) = opt(escaped_transform(
|
||||
none_of("\\]"),
|
||||
'\\',
|
||||
alt((
|
||||
value("]", tag("]")),
|
||||
value("\\", tag("\\")),
|
||||
value("", tag("\n")),
|
||||
)),
|
||||
))(input)?;
|
||||
Ok((input, value.map(|v| v.to_owned())))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
const EXAMPLE: &'static str = "(;FF[4]C[root](;C[a];C[b](;C[c])
|
||||
(;C[d];C[e]))
|
||||
(;C[f](;C[g];C[h];C[i])
|
||||
(;C[j])))";
|
||||
|
||||
#[test]
|
||||
fn it_can_parse_properties() {
|
||||
let (_, prop) = parse_property::<nom::error::VerboseError<&str>>("C[a]").unwrap();
|
||||
assert_eq!(
|
||||
prop,
|
||||
Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["a".to_owned()]
|
||||
}
|
||||
);
|
||||
|
||||
let (_, prop) = parse_property::<nom::error::VerboseError<&str>>("C[a][b][c]").unwrap();
|
||||
assert_eq!(
|
||||
prop,
|
||||
Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["a".to_owned(), "b".to_owned(), "c".to_owned()]
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_can_parse_a_standalone_node() {
|
||||
let (_, node) = parse_node::<nom::error::VerboseError<&str>>(";B[ab]").unwrap();
|
||||
|
||||
assert_eq!(
|
||||
node,
|
||||
Node {
|
||||
properties: vec![Property {
|
||||
ident: "B".to_owned(),
|
||||
values: vec!["ab".to_owned()]
|
||||
}],
|
||||
next: vec![]
|
||||
}
|
||||
);
|
||||
|
||||
let (_, node) =
|
||||
parse_node::<nom::error::VerboseError<&str>>(";B[ab];W[dp];B[pq]C[some comments]")
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
node,
|
||||
Node {
|
||||
properties: vec![Property {
|
||||
ident: "B".to_owned(),
|
||||
values: vec!["ab".to_owned()]
|
||||
}],
|
||||
next: vec![Node {
|
||||
properties: vec![Property {
|
||||
ident: "W".to_owned(),
|
||||
values: vec!["dp".to_owned()]
|
||||
}],
|
||||
next: vec![Node {
|
||||
properties: vec![
|
||||
Property {
|
||||
ident: "B".to_owned(),
|
||||
values: vec!["pq".to_owned()]
|
||||
},
|
||||
Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["some comments".to_owned()]
|
||||
}
|
||||
],
|
||||
next: vec![],
|
||||
}]
|
||||
}]
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_can_parse_a_simple_sequence() {
|
||||
let (_, sequence) =
|
||||
parse_tree::<nom::error::VerboseError<&str>>("(;B[ab];W[dp];B[pq]C[some comments])")
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
sequence,
|
||||
Node {
|
||||
properties: vec![Property {
|
||||
ident: "B".to_owned(),
|
||||
values: vec!["ab".to_owned()]
|
||||
}],
|
||||
next: vec![Node {
|
||||
properties: vec![Property {
|
||||
ident: "W".to_owned(),
|
||||
values: vec!["dp".to_owned()]
|
||||
}],
|
||||
next: vec![Node {
|
||||
properties: vec![
|
||||
Property {
|
||||
ident: "B".to_owned(),
|
||||
values: vec!["pq".to_owned()]
|
||||
},
|
||||
Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["some comments".to_owned()]
|
||||
}
|
||||
],
|
||||
next: vec![],
|
||||
}]
|
||||
}],
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_can_parse_a_branching_sequence() {
|
||||
let text = "(;C[a];C[b](;C[c])(;C[d];C[e]))";
|
||||
let (_, tree) = parse_tree::<nom::error::VerboseError<&str>>(text).unwrap();
|
||||
|
||||
let expected = Node {
|
||||
properties: vec![Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["a".to_owned()],
|
||||
}],
|
||||
next: vec![Node {
|
||||
properties: vec![Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["b".to_owned()],
|
||||
}],
|
||||
next: vec![
|
||||
Node {
|
||||
properties: vec![Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["c".to_owned()],
|
||||
}],
|
||||
next: vec![],
|
||||
},
|
||||
Node {
|
||||
properties: vec![Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["d".to_owned()],
|
||||
}],
|
||||
next: vec![Node {
|
||||
properties: vec![Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["e".to_owned()],
|
||||
}],
|
||||
next: vec![],
|
||||
}],
|
||||
},
|
||||
],
|
||||
}],
|
||||
};
|
||||
|
||||
assert_eq!(tree, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_can_parse_example_1() {
|
||||
let (_, tree) = parse_tree::<nom::error::VerboseError<&str>>(EXAMPLE).unwrap();
|
||||
|
||||
let j = Node {
|
||||
properties: vec![Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["j".to_owned()],
|
||||
}],
|
||||
next: vec![],
|
||||
};
|
||||
let i = Node {
|
||||
properties: vec![Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["i".to_owned()],
|
||||
}],
|
||||
next: vec![],
|
||||
};
|
||||
let h = Node {
|
||||
properties: vec![Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["h".to_owned()],
|
||||
}],
|
||||
next: vec![i],
|
||||
};
|
||||
let g = Node {
|
||||
properties: vec![Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["g".to_owned()],
|
||||
}],
|
||||
next: vec![h],
|
||||
};
|
||||
let f = Node {
|
||||
properties: vec![Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["f".to_owned()],
|
||||
}],
|
||||
next: vec![g, j],
|
||||
};
|
||||
let e = Node {
|
||||
properties: vec![Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["e".to_owned()],
|
||||
}],
|
||||
next: vec![],
|
||||
};
|
||||
let d = Node {
|
||||
properties: vec![Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["d".to_owned()],
|
||||
}],
|
||||
next: vec![e],
|
||||
};
|
||||
let c = Node {
|
||||
properties: vec![Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["c".to_owned()],
|
||||
}],
|
||||
next: vec![],
|
||||
};
|
||||
let b = Node {
|
||||
properties: vec![Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["b".to_owned()],
|
||||
}],
|
||||
next: vec![c, d],
|
||||
};
|
||||
let a = Node {
|
||||
properties: vec![Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["a".to_owned()],
|
||||
}],
|
||||
next: vec![b],
|
||||
};
|
||||
let expected = Node {
|
||||
properties: vec![
|
||||
Property {
|
||||
ident: "FF".to_owned(),
|
||||
values: vec!["4".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["root".to_owned()],
|
||||
},
|
||||
],
|
||||
next: vec![a, f],
|
||||
};
|
||||
|
||||
assert_eq!(tree, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_can_regenerate_the_tree() {
|
||||
let (_, tree1) = parse_tree::<nom::error::VerboseError<&str>>(EXAMPLE).unwrap();
|
||||
let tree1 = Tree { root: tree1 };
|
||||
assert_eq!(
|
||||
tree1.to_string(),
|
||||
"(;FF[4]C[root](;C[a];C[b](;C[c])(;C[d];C[e]))(;C[f](;C[g];C[h];C[i])(;C[j])))"
|
||||
);
|
||||
let (_, tree2) = parse_tree::<nom::error::VerboseError<&str>>(&tree1.to_string()).unwrap();
|
||||
assert_eq!(tree1, Tree { root: tree2 });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_parses_propvals() {
|
||||
let (_, propval) = parse_propval::<nom::error::VerboseError<&str>>("[]").unwrap();
|
||||
assert_eq!(propval, "".to_owned());
|
||||
|
||||
let (_, propval) =
|
||||
parse_propval::<nom::error::VerboseError<&str>>("[normal propval]").unwrap();
|
||||
assert_eq!(propval, "normal propval".to_owned());
|
||||
|
||||
let (_, propval) =
|
||||
parse_propval::<nom::error::VerboseError<&str>>(r"[need an [escape\] in the propval]")
|
||||
.unwrap();
|
||||
assert_eq!(propval, "need an [escape] in the propval".to_owned());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_parses_propvals_with_hard_linebreaks() {
|
||||
let (_, propval) = parse_propval_text::<nom::error::VerboseError<&str>>(
|
||||
"There are hard linebreaks & soft linebreaks.
|
||||
Soft linebreaks...",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
propval,
|
||||
Some(
|
||||
"There are hard linebreaks & soft linebreaks.
|
||||
Soft linebreaks..."
|
||||
.to_owned()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_parses_propvals_with_escaped_closing_brackets() {
|
||||
let (_, propval) =
|
||||
parse_propval_text::<nom::error::VerboseError<&str>>(r"escaped closing \] bracket")
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
propval,
|
||||
Some(r"escaped closing ] bracket".to_owned()).to_owned()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_parses_propvals_with_soft_linebreaks() {
|
||||
let (_, propval) = parse_propval_text::<nom::error::VerboseError<&str>>(
|
||||
r"Soft linebreaks are linebreaks preceeded by '\\' like this one >o\
|
||||
k<. Hard line breaks are all other linebreaks.",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
propval,
|
||||
Some("Soft linebreaks are linebreaks preceeded by '\\' like this one >ok<. Hard line breaks are all other linebreaks.".to_owned())
|
||||
.to_owned()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_parses_sgf_with_newline_in_sequence() {
|
||||
let data = String::from(
|
||||
"(;FF[4]C[root](;C[a];C[b](;C[c])(;C[d];C[e]
|
||||
))(;C[f](;C[g];C[h];C[i])(;C[j])))",
|
||||
);
|
||||
parse_tree::<nom::error::VerboseError<&str>>(&data).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_parses_sgf_with_newline_between_two_sequence_closings() {
|
||||
let data = String::from(
|
||||
"(;FF[4]C[root](;C[a];C[b](;C[c])(;C[d];C[e])
|
||||
)(;C[f](;C[g];C[h];C[i])(;C[j])))",
|
||||
);
|
||||
parse_tree::<nom::error::VerboseError<&str>>(&data).unwrap();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,175 @@
|
|||
use crate::date::Date;
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
/// This is a placeholder structure. It is not meant to represent a game, only to provide a mock
|
||||
/// interface for code already written that expects a Game data type to exist.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Game {
|
||||
pub info: GameInfo,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct GameInfo {
|
||||
pub black_player: Option<String>,
|
||||
pub black_rank: Option<String>,
|
||||
pub white_player: Option<String>,
|
||||
pub white_rank: Option<String>,
|
||||
pub result: Option<GameResult>,
|
||||
pub game_name: Option<String>,
|
||||
pub date: Vec<Date>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum GameType {
|
||||
Go,
|
||||
Othello,
|
||||
Chess,
|
||||
GomokuRenju,
|
||||
NineMensMorris,
|
||||
Backgammon,
|
||||
ChineseChess,
|
||||
Shogi,
|
||||
LinesOfAction,
|
||||
Ataxx,
|
||||
Hex,
|
||||
Jungle,
|
||||
Neutron,
|
||||
PhilosophersFootball,
|
||||
Quadrature,
|
||||
Trax,
|
||||
Tantrix,
|
||||
Amazons,
|
||||
Octi,
|
||||
Gess,
|
||||
Twixt,
|
||||
Zertz,
|
||||
Plateau,
|
||||
Yinsh,
|
||||
Punct,
|
||||
Gobblet,
|
||||
Hive,
|
||||
Exxit,
|
||||
Hnefatal,
|
||||
Kuba,
|
||||
Tripples,
|
||||
Chase,
|
||||
TumblingDown,
|
||||
Sahara,
|
||||
Byte,
|
||||
Focus,
|
||||
Dvonn,
|
||||
Tamsk,
|
||||
Gipf,
|
||||
Kropki,
|
||||
Other(String),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
// InvalidField,
|
||||
// InvalidBoardSize,
|
||||
Incomplete,
|
||||
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)]
|
||||
pub enum ParseError {
|
||||
#[error("An unknown error was found")]
|
||||
NomError(nom::error::Error<String>),
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Color {
|
||||
Black,
|
||||
White,
|
||||
}
|
||||
|
||||
impl Color {
|
||||
pub fn abbreviation(&self) -> String {
|
||||
match self {
|
||||
Color::White => "W",
|
||||
Color::Black => "B",
|
||||
}
|
||||
.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum GameResult {
|
||||
Draw,
|
||||
Black(Win),
|
||||
White(Win),
|
||||
Void,
|
||||
Unknown(String),
|
||||
}
|
||||
|
||||
impl TryFrom<&str> for GameResult {
|
||||
type Error = String;
|
||||
fn try_from(s: &str) -> Result<GameResult, Self::Error> {
|
||||
if s == "0" {
|
||||
Ok(GameResult::Draw)
|
||||
} else if s == "Void" {
|
||||
Ok(GameResult::Void)
|
||||
} else {
|
||||
let parts = s.split('+').collect::<Vec<&str>>();
|
||||
let res = match parts[0].to_ascii_lowercase().as_str() {
|
||||
"b" => GameResult::Black,
|
||||
"w" => GameResult::White,
|
||||
res => return Ok(GameResult::Unknown(res.to_owned())),
|
||||
};
|
||||
match parts[1].to_ascii_lowercase().as_str() {
|
||||
"r" | "resign" => Ok(res(Win::Resignation)),
|
||||
"t" | "time" => Ok(res(Win::Time)),
|
||||
"f" | "forfeit" => Ok(res(Win::Forfeit)),
|
||||
_ => {
|
||||
let score = parts[1].parse::<f32>().unwrap();
|
||||
Ok(res(Win::Score(score)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Win {
|
||||
Score(f32),
|
||||
Resignation,
|
||||
Forfeit,
|
||||
Time,
|
||||
Unknown,
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "tree"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
|
@ -0,0 +1,187 @@
|
|||
//! This data structure is a generic tree which can contain any data. That data itself need to keep
|
||||
//! track of its own tree structure.
|
||||
//!
|
||||
//! This surely already exists. I am created it to test my own ability to do things in Rust.
|
||||
|
||||
use std::{
|
||||
cell::{Ref, RefCell},
|
||||
collections::VecDeque,
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub enum Tree<T> {
|
||||
#[default]
|
||||
Empty,
|
||||
Root(Node<T>),
|
||||
}
|
||||
|
||||
impl<T> Tree<T> {
|
||||
pub fn new(value: T) -> (Tree<T>, Node<T>) {
|
||||
let node = Node::new(value);
|
||||
let tree = Tree::Root(node.clone());
|
||||
(tree, node)
|
||||
}
|
||||
|
||||
pub fn set_value(&mut self, value: T) -> Node<T> {
|
||||
let node = Node::new(value);
|
||||
*self = Tree::Root(node.clone());
|
||||
node
|
||||
}
|
||||
|
||||
/// Use a breadth-first-search pattern to find a node, returning the node if found.
|
||||
pub fn find_bfs<F>(&self, op: F) -> Option<Node<T>>
|
||||
where
|
||||
F: FnOnce(&T) -> bool + Copy,
|
||||
{
|
||||
let mut queue: VecDeque<Node<T>> = match self {
|
||||
Tree::Empty => VecDeque::new(),
|
||||
Tree::Root(node) => {
|
||||
let mut queue = VecDeque::new();
|
||||
queue.push_back(node.clone());
|
||||
queue
|
||||
}
|
||||
};
|
||||
|
||||
while let Some(node) = queue.pop_front() {
|
||||
if op(&node.value()) {
|
||||
return Some(node.clone());
|
||||
}
|
||||
|
||||
for child in node.children().iter() {
|
||||
queue.push_back(child.clone())
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Convert each node of a tree from type T to type U
|
||||
pub fn map<F, U>(&self, op: F) -> Tree<U>
|
||||
where
|
||||
F: FnOnce(&T) -> U + Copy,
|
||||
{
|
||||
// A key part of this is to avoid recursion. There is no telling how deep a tree may go (Go
|
||||
// game records can go hundreds of nodes deep), so we're going to just avoid recursion.
|
||||
match self {
|
||||
Tree::Empty => Tree::Empty,
|
||||
Tree::Root(root) => {
|
||||
let new_root = Node::new(op(&root.value()));
|
||||
|
||||
// This queue serves as a work list. Each node in the queue needs to be converted,
|
||||
// and I've paired the node up with the one that it's supposed to be attached to.
|
||||
// So, as we look at a node A, we make sure that all of its children gets added to
|
||||
// the queue, and that the queue knows that the conversion of each child node
|
||||
// should get attached to A.
|
||||
let mut queue: VecDeque<(Node<T>, Node<U>)> = root
|
||||
.children()
|
||||
.iter()
|
||||
.map(|child| (child.clone(), new_root.clone()))
|
||||
.collect();
|
||||
|
||||
while let Some((source, dest)) = queue.pop_front() {
|
||||
let res = Node::new(op(&source.value()));
|
||||
dest.add_child_node(res.clone());
|
||||
|
||||
for child in source.children().iter() {
|
||||
queue.push_back((child.clone(), res.clone()));
|
||||
}
|
||||
}
|
||||
Tree::Root(new_root)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// By using the Rc<RefCell> container here, I'm able to make Node easily clonable while still
|
||||
// having the contents be shared. This means that I can change the tree structure without having to
|
||||
// make the visible objects mutable.
|
||||
//
|
||||
// This feels like cheating the type system.
|
||||
//
|
||||
// However, since I've moved the RefCell inside of the node, I can borrow the node multiple times
|
||||
// in a traversal function and I can make changes to nodes that I find.
|
||||
#[derive(Debug)]
|
||||
pub struct Node<T>(Rc<RefCell<Node_<T>>>);
|
||||
|
||||
impl<T> Clone for Node<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Node(Rc::clone(&self.0))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Node_<T> {
|
||||
value: T,
|
||||
children: Vec<Node<T>>,
|
||||
}
|
||||
|
||||
impl<T> Node<T> {
|
||||
pub fn new(value: T) -> Self {
|
||||
Self(Rc::new(RefCell::new(Node_ {
|
||||
value,
|
||||
children: vec![],
|
||||
})))
|
||||
}
|
||||
|
||||
/// Immutably retrieve the data in this node.
|
||||
pub fn value(&self) -> Ref<T> {
|
||||
// Ref::map is not actually a member function. I don't know why this was done, other than
|
||||
// maybe to avoid conflicting with other `map` declarations. Why that is necessary when
|
||||
// Option::map exists as a member, I don't know.
|
||||
Ref::map(self.0.borrow(), |v| &v.value)
|
||||
}
|
||||
|
||||
pub fn children(&self) -> Ref<Vec<Node<T>>> {
|
||||
Ref::map(self.0.borrow(), |v| &v.children)
|
||||
}
|
||||
|
||||
pub fn add_child_node(&self, child: Node<T>) {
|
||||
self.0.borrow_mut().children.push(child)
|
||||
}
|
||||
|
||||
pub fn add_child_value(&self, value: T) -> Node<T> {
|
||||
let node = Node::new(value);
|
||||
self.0.borrow_mut().children.push(node.clone());
|
||||
node
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn can_find_node_in_tree() {
|
||||
let mut tree = Tree::default();
|
||||
tree.set_value(15);
|
||||
assert!(tree.find_bfs(|val| *val == 15).is_some());
|
||||
assert!(tree.find_bfs(|val| *val == 16).is_none());
|
||||
|
||||
let node = tree.find_bfs(|val| *val == 15).unwrap();
|
||||
node.add_child_value(20);
|
||||
|
||||
assert!(tree.find_bfs(|val| *val == 20).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn node_can_add_children() {
|
||||
let n = Node::new(15);
|
||||
n.add_child_value(20);
|
||||
|
||||
assert_eq!(*n.value(), 15);
|
||||
// assert_eq!(n.children(), vec![Rc::new(RefCell::new(Node::new(20)))]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_can_map_one_tree_to_another() {
|
||||
let (tree, n) = Tree::new(15);
|
||||
let n = n.add_child_value(16);
|
||||
let _ = n.add_child_value(17);
|
||||
|
||||
let tree2 = tree.map(|v| v.to_string());
|
||||
|
||||
assert!(tree2.find_bfs(|val| *val == "15").is_some());
|
||||
assert!(tree2.find_bfs(|val| *val == "16").is_some());
|
||||
assert!(tree2.find_bfs(|val| *val == "17").is_some());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue