Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Commit

Permalink
Refactor rust & reader
Browse files Browse the repository at this point in the history
Move rust into a package, details of template making to be worked out later.

Refactor reader to something sensible.

Finish standard_whitespace, and start tests for identical_whitespace.
  • Loading branch information
Riolku committed Oct 4, 2022
1 parent e97b8b0 commit e93bcca
Show file tree
Hide file tree
Showing 16 changed files with 440 additions and 158 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.pyc
__pycache__/
.venv
target
7 changes: 7 additions & 0 deletions rust_utils/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions rust_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "dmoj_rust_utils"
version = "0.1.0"
edition = "2021"

[dependencies]
48 changes: 0 additions & 48 deletions rust_utils/checker.rs

This file was deleted.

18 changes: 0 additions & 18 deletions rust_utils/identical_whitespace.rs

This file was deleted.

Binary file removed rust_utils/reader
Binary file not shown.
89 changes: 0 additions & 89 deletions rust_utils/reader.rs

This file was deleted.

28 changes: 28 additions & 0 deletions rust_utils/src/checker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
mod codes {
pub const AC: i32 = 0;
pub const WA: i32 = 1;
pub const PE: i32 = 2;
pub const IE: i32 = 3;
pub const PARTIAL: i32 = 7;
}

use std::process;

pub fn assert_or_code(cond: bool, code: i32) {
if !cond {
exit(code);
}
}

pub fn assert_or_wa(cond: bool) {
assert_or_code(cond, codes::WA);
}

pub fn assert_or_pe(cond: bool) {
assert_or_code(cond, codes::PE);
}

pub fn exit(code: i32) -> ! {
super::pre_error();
process::exit(code);
}
68 changes: 68 additions & 0 deletions rust_utils/src/identical_whitespace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use super::reader::{
self, AsciiStream, ExactWhitespaceTokenizer, Tokenizer, TokenizerResult, WRONG_WHITESPACE,
};
use std::io::BufRead;

pub struct Handler<S: AsciiStream> {
src: S,
}
pub fn new(src: impl BufRead) -> Handler<impl AsciiStream> {
Handler {
src: reader::to_ascii_stream(src),
}
}

impl<S> Tokenizer for Handler<S>
where
S: AsciiStream,
{
fn init(&mut self) -> TokenizerResult<()> {
Ok(())
}
fn expect_space(&mut self) -> TokenizerResult<()> {
match self.src.next() {
Some(' ') => Ok(()),
_ => Err(WRONG_WHITESPACE),
}
}
fn expect_newline(&mut self) -> TokenizerResult<()> {
match self.src.next() {
Some('\n') => Ok(()),
_ => Err(WRONG_WHITESPACE),
}
}
fn expect_eof(&mut self) -> TokenizerResult<()> {
match self.src.next() {
None => Ok(()),
_ => Err(WRONG_WHITESPACE),
}
}
fn read_token(&mut self) -> TokenizerResult<String> {
let mut token = String::new();
while self.src.peek().map_or(false, |c| !c.is_ascii_whitespace()) {
token.push(self.src.next().unwrap());
}
if token.len() == 0 {
Err(WRONG_WHITESPACE)
} else {
Ok(token)
}
}
}

impl<S> ExactWhitespaceTokenizer for Handler<S>
where
S: AsciiStream,
{
fn read_line(&mut self) -> TokenizerResult<String> {
let mut line = String::new();
while self.src.peek().map_or(false, |c| *c != '\n') {
line.push(self.src.next().unwrap());
}
if self.src.next().is_none() {
Err(WRONG_WHITESPACE)
} else {
Ok(line)
}
}
}
8 changes: 8 additions & 0 deletions rust_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod checker;
mod identical_whitespace;
mod panic_error_handler;
mod reader;
mod standard_whitespace;
mod validating_reader;

fn pre_error() {}
18 changes: 18 additions & 0 deletions rust_utils/src/panic_error_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::reader::ErrorHandler;

pub struct Handler;

impl ErrorHandler for Handler {
fn new() -> Self {
Self {}
}
fn out_of_range(&self) -> ! {
panic!("out of range")
}
fn parse_error(&self) -> ! {
panic!("parse error")
}
fn wrong_whitespace(&self) -> ! {
panic!("wrong whitespace")
}
}
Loading

0 comments on commit e93bcca

Please sign in to comment.