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

app cargo-oj

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

7 releases (breaking)

0.7.0 May 22, 2024
0.6.0 May 22, 2024
0.5.0 Apr 29, 2024
0.4.0 Apr 29, 2024
0.1.0 Sep 30, 2023

#164 in Cargo plugins

MIT license

30KB
531 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.5–9.5MB
~97K SLoC