#single-file #package #cargo #online #formatted #io #oj

app cargo-oj

Cargo-OJ: package a crate into single file, minimized and formatted

3 releases (breaking)

0.3.0 Mar 28, 2024
0.2.0 Nov 6, 2023
0.1.0 Sep 30, 2023

#102 in Cargo plugins

Download history 5/week @ 2024-02-19 5/week @ 2024-02-26 5/week @ 2024-03-11 145/week @ 2024-03-25 57/week @ 2024-04-01

207 downloads per month

MIT license

31KB
519 lines

cargo-oj: Solution packer for Online Judges

What does it do?

All online judges (OJs) require you to write and submit a single file. But keeping all the I/O and algorithm snippets and copy-pasting them into the main file is not only cumbersome, but also against the spirit of modular code.

cargo-oj lets you keep your code organized in a module structure inside a library crate, and it will happily pack your lib.rs and its children into a main.rs, removing all the unnecessary bloat (which is dead code) along the way.

How to use it?

  • Create a library crate.

  • Write your solution in lib.rs, which should contain a pub fn main() {...}. Optionally put

    #![allow(dead_code)]
    #![allow(unused_imports)]
    

    at the top of lib.rs. This will eliminate warnings about unused items (mostly data structures and algorithms) in your IDE. cargo-oj strips these declarations before testing for dead code.

  • Write your own library as child modules of lib.rs. Mark all items that you will directly use as pub(crate). This lets you use those items in different modules within the same crate (especially lib.rs), while letting rustc emit unused warnings when appropriate.

  • When your solution is complete, run cargo oj at the crate root folder. src/bin/main.rs will be created. Then you can:

    • run it locally via cargo run --bin main --release, and/or
    • submit the file using cargo-boj or other tools.

Limitations

  • Use of third party crates is not supported.
  • Use of #[path = "filepath"] attributes for module file paths is not supported. modname.rs, modname/mod.rs, and inline modules are supported.
  • It takes a few seconds to eliminate all unnecessary trait Trait and impl Trait for T items, even in relatively small module structure (see ps folder for example). This is due to the limitations of rustc's dead code warnings and the complexity of trait resolution.

Installation

cargo install --path=.

Usage

cargo oj

Dependencies

~3–11MB
~95K SLoC