Skip to content

Commit

Permalink
feat: 🚚 module separation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jisu-Woniu committed Feb 14, 2024
1 parent df1ad08 commit 4326439
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 285 deletions.
468 changes: 192 additions & 276 deletions Cargo.lock

Large diffs are not rendered by default.

15 changes: 6 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["crates/rsjudge-runner", "xtask"]
members = ["crates/rsjudge-grpc", "crates/rsjudge-rest", "crates/rsjudge-runner", "xtask"]

[workspace.package]
version = "0.1.0"
Expand Down Expand Up @@ -73,26 +73,23 @@ cgroups = "0.1.0"
clap = { version = "4.5.0", features = ["derive"] }
duct = "0.13.7"
nix = { version = "0.27.1", features = ["fs"] }
rsjudge-runner = { version = "0.1.0", path = "crates/rsjudge-runner" }
serde = { version = "1.0.196", features = ["derive"] }
strfmt = "0.2.4"
tokio = { version = "1.36.0", features = ["fs"] }
toml = "0.8.10"
uzers = "0.11.3"

# Optional dependencies
axum = { version = "0.7.4", optional = true }
rsjudge-runner = { version = "0.1.0", path = "crates/rsjudge-runner" }
tonic = { version = "0.11.0", optional = true }
utoipa = { version = "4.2.0", features = ["axum_extras"], optional = true }
utoipa-swagger-ui = { version = "6.0.0", features = ["axum"], optional = true }
rsjudge-grpc = { version = "0.1.0", path = "crates/rsjudge-grpc", optional = true }
rsjudge-rest = { version = "0.1.0", path = "crates/rsjudge-rest", optional = true }

[dev-dependencies]
tokio = { version = "1.36.0", features = ["rt-multi-thread", "macros"] }

[features]
grpc = ["dep:tonic"]
rest = ["dep:axum"]
openapi = ["rest", "dep:utoipa", "dep:utoipa-swagger-ui"]
grpc = ["dep:rsjudge-grpc"]
rest = ["dep:rsjudge-rest"]
default = ["grpc"]

[build-dependencies]
Expand Down
15 changes: 15 additions & 0 deletions crates/rsjudge-grpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "rsjudge-grpc"
version.workspace = true
authors.workspace = true
edition = "2021"
license.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tonic = "0.11.0"

[build-dependencies]
anyhow = "1.0.79"
tonic-build = "0.11.0"
6 changes: 6 additions & 0 deletions crates/rsjudge-grpc/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use tonic_build::compile_protos;

fn main() -> anyhow::Result<()> {
compile_protos("proto/rsjudge/v1/rsjudge.proto")?;
Ok(())
}
8 changes: 8 additions & 0 deletions crates/rsjudge-grpc/proto/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: v1
breaking:
use:
- FILE
lint:
use:
- DEFAULT
- COMMENTS
123 changes: 123 additions & 0 deletions crates/rsjudge-grpc/proto/rsjudge/v1/rsjudge.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
syntax = "proto3";

package rsjudge.v1;

// The judging service definition.
service JudgeService {
// Sends a self-test request.
rpc SelfTest(SelfTestRequest) returns (stream SelfTestResponse);
// Sends a judge request.
rpc Submit(SubmitRequest) returns (stream SubmitResponse);
}

// The request of self-test info, containing user's code and input.
message SelfTestRequest {
// The language of the code.
string language = 1;
// The code to be tested.
string code = 2;
// The input of the program.
string stdin = 3;
}

// The response message containing the self-test results.
message SelfTestResponse {
// The type of the response.
oneof self_test_response_type {
// Message indicating the compile result.
CompileInfo compile_info = 1;
// Message indicating the self-test result.
SelfTestSummary summary = 2;
}
}

// Message indicating the compile result.
message CompileInfo {
// Compiler exit status.
//
// 0 indicates the compiler exited normally.
// For interpreted languages, a non-zero exit status indicates semantic errors found in the code.
// For compiled languages, it means compilation errors.
int32 exit_status = 1;
// Compiler stdout.
string stdout = 2;
// Compiler stderr.
string stderr = 3;
}

// Message indicating the self-test result.
message SelfTestSummary {
// The exit status of the user's program.
//
// 0 indicates the program exited normally.
int32 exit_status = 1;
// The stdout of the program.
string stdout = 2;
// The stderr of the program.
//
// This is not used by the judge system, but may be useful for debugging.
string stderr = 3;
}

// The request containing the user's code and judge info.
//
// We only showed part of the fields.
message SubmitRequest {
// The language of the code.
string language = 1;
// The code to be judged.
string code = 2;
// The test case id.
//
// If the test case is updated, the test case id MUST be changed, so that the judger can request the latest test case.
int32 test_case_id = 3;
}

// The response message containing the judge results.
message SubmitResponse {
// The type of the response.
oneof submit_response_type {
// Message indicating the compile result.
CompileInfo compile_info = 1;
// Message indicating the single case judge result.
//
// Case info may not be sent in order, so case id SHOULD be checked.
CaseInfo case_info = 2;
// Message indicating the judge result of all cases.
CasesSummary cases_summary = 3;
}
}

// Message indicating the single case judge result.
message CaseInfo {
// The ID of the test case, usually starting from 1.
int32 case_id = 1;
// The exit status of the user's program.
int32 exit_status = 2;
// The judge result of the program.
JudgeResult result = 3;
// The score of the case.
int32 score = 4;
}

// Message indicating the judge result of all cases.
message CasesSummary {
// The judge result of the program.
JudgeResult result = 1;
// The score of the program.
int32 score = 2;
}

// The judge result.
enum JudgeResult {
// The judge result is unspecified.
//
// This is considered error and SHOULD NOT happen.
JUDGE_RESULT_UNSPECIFIED = 0;
// The judge result is Accepted (AC).
JUDGE_RESULT_ACCEPTED = 1;
// The judge result is Wrong Answer (WA).
JUDGE_RESULT_WRONG_ANSWER = 2;
// The judge result is Runtime Error (RE).
JUDGE_RESULT_RUNTIME_ERROR = 3;
}
14 changes: 14 additions & 0 deletions crates/rsjudge-grpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
11 changes: 11 additions & 0 deletions crates/rsjudge-rest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "rsjudge-rest"
edition = "2021"
version.workspace = true
authors.workspace = true
license.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axum = "0.7.4"
14 changes: 14 additions & 0 deletions crates/rsjudge-rest/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::Parser;
use toml::Value;

use crate::cli::Args;
mod cli;
Expand All @@ -7,5 +8,10 @@ fn main() -> anyhow::Result<()> {
let args = Args::try_parse()?;
println!("{:?}", args);

println!(
"Config:\n{:#?}",
toml::from_str::<Value>(include_str!("../templates/executors.toml"))?
);

Ok(())
}

0 comments on commit 4326439

Please sign in to comment.