Skip to content

Commit

Permalink
feat(serde): add serde feature (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 authored Nov 30, 2024
1 parent 20bb292 commit a500968
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changes/serde.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eval-stack": patch:feat
---

Add `serde` feature to enable serialization and deserialization.
21 changes: 21 additions & 0 deletions Cargo.lock

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

9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ keywords = ["oj", "online-judge", "acm", "oi"]
[dependencies]
anyhow = "1.0.92"
libc = "0.2.161"
nix = { version = "0.29.0", features = ["user"] }
tokio = { version = "1.41.0", features = ["full"] }

nix = { version = "0.29.0", features = ["user"], optional = true }
serde = { version = "1.0.215", features = ["derive"], optional = true }

[features]
default = ["serde"]
rerun = ["nix"]
serde = ["dep:serde"]
31 changes: 17 additions & 14 deletions src/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,23 @@ where

let mut results = vec![];
for (input_file, expected_output_file) in test_cases {
results.push(
execute(
&workspace,
exec_path.to_string_lossy(),
args,
&options,
TestCase {
input_file: input_file.into(),
expected_output_file: expected_output_file.into(),
},
workspace.join("test.out"),
)
.await?,
);
let result = execute(
&workspace,
exec_path.to_string_lossy(),
args,
&options,
TestCase {
input_file: input_file.into(),
expected_output_file: expected_output_file.into(),
},
workspace.join("test.out"),
)
.await?;
if options.fast_fail && !matches!(result.status, JudgeStatus::Accepted) {
results.push(result);
break;
}
results.push(result);
}

if clean {
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::{path::PathBuf, time::Duration};

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[serde(rename_all = "camelCase")]
pub struct JudgeOptions {
pub time_limit: Duration,
pub memory_limit: u64,
pub fast_fail: bool,
}

pub struct TestCase<I, O>
Expand Down
8 changes: 6 additions & 2 deletions src/judge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use anyhow::Result;

use crate::utils::get_memory_usage;

#[derive(Debug)]
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum JudgeStatus {
Accepted,
WrongAnswer,
Expand All @@ -25,6 +27,8 @@ pub enum JudgeStatus {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[serde(rename_all = "camelCase")]
pub struct JudgeResult {
pub status: JudgeStatus,
pub time_used: Duration,
Expand Down Expand Up @@ -74,7 +78,7 @@ impl Future for Judge {
}
}

while let Some(extra_line) = stdout_lines.next() {
for extra_line in stdout_lines {
if extra_line?.trim_end_matches(|c: char| c.is_whitespace() || c == '\n')
!= ""
{
Expand Down
15 changes: 8 additions & 7 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use std::{
env, fs,
process::{self, Command},
};

use anyhow::Result;
use std::fs;

pub fn get_memory_usage(pid: u32) -> Option<u64> {
let statm_path = format!("/proc/{}/statm", pid);
Expand All @@ -16,7 +11,13 @@ pub fn get_memory_usage(pid: u32) -> Option<u64> {
None
}

pub fn rerun_if_not_root() -> Result<()> {
#[cfg(feature = "rerun")]
pub fn rerun_if_not_root() -> anyhow::Result<()> {
use std::{
env,
process::{self, Command},
};
#[cfg(target_os = "linux")]
if !nix::unistd::getuid().is_root() {
let args: Vec<String> = env::args().collect();
let mut command = Command::new("sudo");
Expand Down
10 changes: 6 additions & 4 deletions src/main.rs → tests/test_judge.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::time::Duration;

use eval_stack::{
case::run_test_cases, compile::Language, config::JudgeOptions, utils::rerun_if_not_root,
};
use anyhow::Result;
#[cfg(feature = "rerun")]
use eval_stack::utils::rerun_if_not_root;
use eval_stack::{case::run_test_cases, compile::Language, config::JudgeOptions};

#[tokio::main]
#[tokio::test]
async fn main() -> Result<()> {
#[cfg(feature = "rerun")]
rerun_if_not_root()?;

let current_dir = std::env::current_dir()?;
Expand All @@ -20,6 +21,7 @@ async fn main() -> Result<()> {
JudgeOptions {
time_limit: Duration::from_secs(1),
memory_limit: 128 * 1024 * 1024,
fast_fail: true,
},
vec![
(tests_path.join("1.in"), tests_path.join("1.out")),
Expand Down

0 comments on commit a500968

Please sign in to comment.