Add Rust support: usaco lang, lint, and main.rs problems

Brings in the stashed Rust work, merged onto the current CLI. A
problem's language is whichever source it has (main.cpp or main.rs);
'usaco lang' sets the default for new problems (.lang, or $USACO_LANG),
and 'new --rust/--cpp' picks per problem. template.rs is a paste-ready
Rust template whose stdio() uses <io>.in/.out when present and
stdin/stdout otherwise.

run/test now feed <io>.in on stdin and capture stdout when the program
doesn't write <io>.out itself, so Codeforces-style solutions work too.
'reset <name> rust|cpp' switches a problem's language (template is
still an alias), and --debug builds Rust with overflow checks and debug
assertions. 'usaco lint' runs clippy through a generated, gitignored
Cargo.toml that also gives rust-analyzer a project; done/skip/undo
keep it in step. LeetCode imports stay C++.

Also: done stages solved/ and solving/ separately, so a problem that
was never committed from solving/ still gets its "Solve" commit, and
the site lists main.rs ahead of other files like it does main.cpp.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-19 21:17:50 +01:00
co-authored by Claude Opus 5.5
parent e821df15f4
commit 1aee0050a0
24 changed files with 588 additions and 112 deletions
+1 -1
View File
@@ -2,4 +2,4 @@
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
build_problem "$dir"
echo "Built $dir/main"
echo "Built $dir/main ${DIM}($(lang_of "$dir"))${RST}"
+1 -1
View File
@@ -1,4 +1,4 @@
# usaco cat <name>
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
cat "$dir/main.cpp"
cat "$(main_file "$dir")"
+2 -1
View File
@@ -2,5 +2,6 @@
for s in solving solved skipped; do
[[ -d "$ROOT/$s" ]] || continue
find "$ROOT/$s" -mindepth 2 -maxdepth 2 -type f \
\( -name main -o -name a.out -o -name '*.bak' \) -print -delete | sed "s|^$ROOT/|removed |"
\( -name main -o -name a.out -o -name '*.bak' -o -name '*.pdb' \) -print -delete \
| sed "s|^$ROOT/|removed |"
done
+6 -5
View File
@@ -1,16 +1,17 @@
# usaco copy <name> (alias: cp)
# Copies the problem's main.cpp to the clipboard.
# Copies the problem's solution source to the clipboard.
# Wayland: wl-copy; X11: xclip/xsel; last resort OSC52 escape (works over SSH).
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
src="$(main_file "$dir")"
if [[ -n ${WAYLAND_DISPLAY:-} ]] && command -v wl-copy >/dev/null 2>&1; then
wl-copy < "$dir/main.cpp"
wl-copy < "$src"
elif command -v xclip >/dev/null 2>&1; then
xclip -selection clipboard < "$dir/main.cpp"
xclip -selection clipboard < "$src"
elif command -v xsel >/dev/null 2>&1; then
xsel -b --input < "$dir/main.cpp"
xsel -b --input < "$src"
else
# OSC52: terminal-native clipboard, also works through SSH
base64 -w0 < "$dir/main.cpp" | printf '\e]52;c;%s\a' "$(cat)"
base64 -w0 < "$src" | printf '\e]52;c;%s\a' "$(cat)"
fi
+5 -1
View File
@@ -4,8 +4,12 @@ dir="$(find_problem "$1")"; name="$(basename "$dir")"
[[ "$dir" == "$ROOT/solving/$name" ]] || { echo "Error: $name is not in solving/ (it's in ${dir#$ROOT/})" >&2; exit 1; }
[[ -d "$ROOT/solved/$name" ]] && { echo "Error: solved/$name already exists" >&2; exit 1; }
mv "$ROOT/solving/$name" "$ROOT/solved/$name"
sync_cargo_manifest >/dev/null # the bin paths just moved
echo "Moved to solved/$name 🎉"
if git -C "$ROOT" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
git -C "$ROOT" add -A "solved/$name" "solving/$name" 2>/dev/null || true
# separately: one pathspec that matches nothing (a problem never committed
# from solving/) would make git refuse to stage the other
git -C "$ROOT" add -A "solved/$name" 2>/dev/null || true
git -C "$ROOT" add -A "solving/$name" 2>/dev/null || true
git -C "$ROOT" commit -m "Solve $name" >/dev/null && echo "Committed: Solve $name ${DIM}(usaco sync to publish it)${RST}"
fi
+2 -1
View File
@@ -1,5 +1,6 @@
# usaco vim/edit/e <name>
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
src="$(basename "$(main_file "$dir")")"
# run from inside the dir so :!./main etc. work and .in files are at hand
(cd "$dir" && exec ${EDITOR:-nvim} main.cpp)
(cd "$dir" && exec ${EDITOR:-nvim} "$src")
+13 -4
View File
@@ -10,7 +10,7 @@ HISTFILE="$ROOT/.usaco_history"
# commands that take a problem name (the selection is injected when you omit it)
WITH_PROBLEM="run build test done undo skip io cat copy edit open path reset"
# commands that take no problem
NO_PROBLEM="list stats clean sync"
NO_PROBLEM="list stats clean sync lang"
# REPL-only words (used for completion and for 'help')
REPL_WORDS="s use none push pop stack help quit exit"
@@ -70,7 +70,8 @@ ${DIM}selection${RST}
${DIM}problem commands${RST} ${DIM}(name optional — omit it to use the selection; giving one switches to it)${RST}
$WITH_PROBLEM
${DIM}other${RST}
$NO_PROBLEM, new <name>, new <leetcode-url|daily>
$NO_PROBLEM, new <name> [io] [--rust|--cpp], new <leetcode-url|daily>
lint [<name>] clippy the selection, or every rust solution
help [<cmd>] this, or the dispatcher's usage line for <cmd>
!cmd run a shell command in the problem dir
q quit
@@ -83,7 +84,7 @@ _usaco_comp() { # completes REPL commands and problem names
for d in "$ROOT"/solving/*/ "$ROOT"/solved/*/ "$ROOT"/skipped/*/; do names+=("$(basename "$d")"); done
shopt -u nullglob
if [[ $COMP_CWORD -eq 0 ]]; then
COMPREPLY=($(compgen -W "$WITH_PROBLEM $NO_PROBLEM $REPL_WORDS new ${names[*]}" -- "$w"))
COMPREPLY=($(compgen -W "$WITH_PROBLEM $NO_PROBLEM $REPL_WORDS new lint ${names[*]}" -- "$w"))
else
COMPREPLY=($(compgen -W "${names[*]}" -- "$w"))
fi
@@ -141,6 +142,12 @@ while :; do
if USACO_CREATED="$created" "$ROOT/usaco" new "${args[@]}"; then set_current "$(<"$created")"; fi
rm -f "$created"
continue ;;
lint)
# the one command whose problem name is optional: it lints the
# selection if there is one, otherwise every rust solution
[[ ${#args[@]} -eq 0 && -n "$cur" ]] && args=("$cur")
"$ROOT/usaco" lint "${args[@]+"${args[@]}"}" || true
continue ;;
esac
# expand short aliases to full names so the tables below match
@@ -158,7 +165,9 @@ while :; do
if [[ " $WITH_PROBLEM " == *" $c "* ]]; then
# 'test create' keeps its subword first; the name goes after it
pre=(); if [[ "$c" == test && "${args[0]:-}" =~ ^(create|add|new)$ ]]; then pre=("${args[0]}"); args=("${args[@]:1}"); fi
if [[ ${#args[@]} -ge 1 && "${args[0]}" != -* ]]; then # explicit name: use it and switch to it
# 'reset rust' switches the selection's language: a language word isn't a name
lang_arg=""; [[ "$c" == reset ]] && normalize_lang "${args[0]:-}" >/dev/null 2>&1 && lang_arg=1
if [[ ${#args[@]} -ge 1 && "${args[0]}" != -* && -z "$lang_arg" ]]; then # explicit name: use it and switch to it
n="$(real_name "${args[0]}")" || { find_problem "${args[0]}" >/dev/null || true; continue; }
set_current "$n"; args=("${args[@]:1}")
elif [[ -z "$cur" ]]; then
+4 -12
View File
@@ -3,9 +3,10 @@
dir="$(find_problem "$1")"
old="$(detect_io "$dir" || true)"
if [[ $# -lt 2 ]]; then
stdio="$(grep -o 'STDIO("[^"]*")' "$dir/main.cpp" 2>/dev/null | sed 's/STDIO("\(.*\)")/\1/' | head -1 || true)"
wired="$(show_io "$dir" || true)"
echo "lang: $(lang_of "$dir")"
echo "files: ${old:-none}"
echo "STDIO: ${stdio:-none}"
echo "source: ${wired:-none}"
exit 0
fi
io="$2"
@@ -16,14 +17,5 @@ if [[ -n "$old" && "$old" != "$io" ]]; then
done
fi
[[ -e "$dir/$io.in" ]] || { touch "$dir/$io.in"; echo "Created $io.in"; }
# rewire STDIO in main.cpp
if grep -q 'STDIO("' "$dir/main.cpp" 2>/dev/null; then
sed -i "s/STDIO(\"[^\"]*\")/STDIO(\"$io\")/" "$dir/main.cpp"
echo "Updated STDIO(\"$io\") in main.cpp"
elif grep -q '^int main() {$' "$dir/main.cpp" 2>/dev/null; then
sed -i "s/^int main() {$/int main() {\n STDIO(\"$io\");/" "$dir/main.cpp"
echo "Inserted STDIO(\"$io\") in main.cpp"
else
echo "Warning: couldn't find STDIO(...) or 'int main() {' in main.cpp — wire it up manually" >&2
fi
wire_io "$dir" "$io"
echo "io name for $(basename "$dir") is now '$io'"
+11
View File
@@ -0,0 +1,11 @@
# usaco lang [cpp|rust]
if [[ $# -eq 0 ]]; then
echo "default for 'usaco new': $(default_lang)"
[[ -n "${USACO_LANG:-}" ]] && echo "${DIM}(from \$USACO_LANG)${RST}"
exit 0
fi
lang="$(normalize_lang "$1")" || exit 1
printf '%s\n' "$lang" > "$ROOT/.lang"
echo "Default language for new problems: $lang"
[[ -n "${USACO_LANG:-}" ]] && echo "${YEL}note:${RST} \$USACO_LANG=$USACO_LANG overrides this in your shell" >&2
exit 0
+35
View File
@@ -0,0 +1,35 @@
# usaco lint [name...]
# Regenerates the cargo manifest rust-analyzer reads, then runs clippy over the
# rust solutions. With no arguments it lints every one, plus template.rs; names
# select individual problems (unique prefixes work, as everywhere else), and
# the literal name "template" selects template.rs.
command -v cargo >/dev/null || { echo "Error: cargo not found" >&2; exit 1; }
count="$(sync_cargo_manifest)"
if [[ -z "$count" ]]; then
echo "No rust solutions or template.rs yet — nothing to lint."
exit 0
fi
targets=()
for name in "$@"; do
if [[ "$name" == template ]]; then
[[ -f "$ROOT/template.rs" ]] || { echo "Error: template.rs not found" >&2; exit 1; }
targets+=(--bin template)
continue
fi
dir="$(find_problem "$name")" || exit 1
[[ -f "$dir/main.rs" ]] || { echo "Error: $(basename "$dir") is not a rust problem" >&2; exit 1; }
rel="${dir#"$ROOT"/}"
targets+=(--bin "${rel%/*}-${rel##*/}")
done
if [[ ${#targets[@]} -eq 0 ]]; then
what="$count rust solution(s)"
[[ -f "$ROOT/template.rs" ]] && what+=" and template.rs"
echo "${DIM}Linting $what...${RST}"
targets=(--bins)
fi
# Builds live under target/, away from the ./main binaries usaco produces.
(cd "$ROOT" && cargo clippy --quiet "${targets[@]}")
+13 -5
View File
@@ -1,9 +1,12 @@
# usaco new <name> [io-name] [--url <link>] | usaco new <leetcode-url|daily>
# usaco new <name> [io-name] [--url <link>] [--rust|--cpp] | usaco new <leetcode-url|daily>
[[ $# -ge 1 ]] || usage
name=""; io=""; url=""
name=""; io=""; url=""; lang=""
while [[ $# -gt 0 ]]; do
case "$1" in
--url) url="${2:-}"; shift 2 ;;
--lang|-l) lang="$(normalize_lang "${2:-}")" || exit 1; shift 2 ;;
--rust|--rs) lang=rust; shift ;;
--cpp|--c++) lang=cpp; shift ;;
*) if [[ -z "$name" ]]; then name="$1"; elif [[ -z "$io" ]]; then io="$1"; fi; shift ;;
esac
done
@@ -11,7 +14,9 @@ done
# A LeetCode link (or "daily": today's daily question) imports the problem:
# statement, starter code, a generated driver, and the examples as tests.
# Always C++, whatever 'usaco lang' says: the driver is C++.
if [[ "$name" == daily || "$name" =~ ^(https?://)?(www\.)?leetcode\.com/problems/ ]]; then
[[ "${lang:-cpp}" == cpp ]] || { echo "Error: LeetCode imports are C++ only" >&2; exit 1; }
name="$(python3 -I "$ROOT/lib/leetcode.py" "$ROOT" "$name")" || exit 1
[[ -n "${USACO_CREATED:-}" ]] && printf '%s\n' "$name" > "$USACO_CREATED"
echo "Created solving/$name (statement in problem.md; 'usaco t $name' runs the examples)"
@@ -20,14 +25,17 @@ if [[ "$name" == daily || "$name" =~ ^(https?://)?(www\.)?leetcode\.com/problems
fi
io="${io:-$name}"
lang="${lang:-$(default_lang)}"
dir="$ROOT/solving/$name"
if [[ -d "$dir" || -d "$ROOT/solved/$name" || -d "$ROOT/skipped/$name" ]]; then
echo "Error: '$name' already exists" >&2; exit 1
fi
mkdir -p "$dir"
fresh_main "$dir" "$io"
fresh_main "$dir" "$io" "$lang"
touch "$dir/$io.in"
[[ -n "$url" ]] && echo "$url" > "$dir/.url"
[[ -n "${USACO_CREATED:-}" ]] && printf '%s\n' "$name" > "$USACO_CREATED"
echo "Created solving/$name (io: $io.in / $io.out)"
(cd "$dir" && exec ${EDITOR:-nvim} main.cpp)
src="$(src_name "$lang")"
echo "Created solving/$name/$src (io: $io.in / $io.out)"
[[ "$lang" == rust ]] && sync_cargo_manifest >/dev/null
(cd "$dir" && exec ${EDITOR:-nvim} "$src")
+25 -11
View File
@@ -1,25 +1,39 @@
# usaco reset/rm/delete/template/tpl <name> [-y]
# usaco reset/rm/delete/template/tpl <name> [cpp|rust] [-y]
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
dir="$(find_problem "$1")"; shift
name="$(basename "$dir")"
yes=""; lang=""
for a in "$@"; do
if [[ "$a" == -y ]]; then yes=1; else lang="$(normalize_lang "$a")" || exit 1; fi
done
lang="${lang:-$(lang_of "$dir" 2>/dev/null || default_lang)}"
leet=""; [[ -f "$dir/driver.h" ]] && leet=1
if [[ -n "$leet" && "$lang" != cpp ]]; then
echo "Error: $name is a LeetCode import, and those are C++ only" >&2; exit 1
fi
io="$(detect_io "$dir" || true)"
io="${io:-$name}"
if [[ "${2:-}" != -y ]]; then
echo "This resets ${dir#$ROOT/} to a fresh template:"
if [[ -f "$dir/driver.h" ]]; then
echo " main.cpp -> LeetCode's starter code again (old one saved as main.cpp.bak)"
old="$(main_file "$dir" 2>/dev/null || true)"
src="$(src_name "$lang")"
if [[ -z "$yes" ]]; then
echo "This resets ${dir#$ROOT/} to a fresh $lang template:"
if [[ -n "$leet" ]]; then
echo " $src -> LeetCode's starter code again (old one saved as $src.bak)"
else
echo " main.cpp -> re-created from template (old one saved as main.cpp.bak)"
echo " $src -> re-created from template (old one saved as $(basename "${old:-$src}").bak)"
fi
echo " removes compiled binary and $io.out (tests/ and $io.in are kept)"
read -r -p "Reset '$name'? [y/N] " ans
[[ "$ans" == y || "$ans" == Y ]] || { echo "Aborted."; exit 1; }
fi
[[ -f "$dir/main.cpp" ]] && cp "$dir/main.cpp" "$dir/main.cpp.bak"
if [[ -f "$dir/driver.h" ]]; then # a LeetCode import: re-fetch its starter code
[[ -n "$old" && -f "$old" ]] && cp "$old" "$old.bak"
# switching language: drop the old source so lang_of stays unambiguous
[[ -n "$old" && "$(basename "$old")" != "$src" ]] && rm -f "$old"
if [[ -n "$leet" ]]; then # a LeetCode import: re-fetch its starter code
python3 -I "$ROOT/lib/leetcode.py" "$ROOT" "$dir" --main-only >/dev/null || exit 1
else
fresh_main "$dir" "$io"
fresh_main "$dir" "$io" "$lang"
fi
[[ "$lang" == rust || "$old" == *.rs ]] && sync_cargo_manifest >/dev/null
rm -f "$dir/main" "$dir/a.out" "$dir/$io.out"
echo "Reset $name (old solution in main.cpp.bak)"
echo "Reset $name (old solution in $(basename "${old:-$src}").bak)"
+5 -4
View File
@@ -2,11 +2,12 @@
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
build_problem "$dir"
# run from inside the dir so freopen finds the .in file
(cd "$dir" && ./main)
# show the .out file if the program wrote one
io="$(detect_io "$dir" || true)"
if [[ -n "$io" && -f "$dir/$io.out" ]]; then
io="${io:-$(basename "$dir")}"
rc=0; run_solution "$dir" "$io" || rc=$?
if [[ -e "$dir/$io.out" ]]; then
echo "── $io.out ──"
cat "$dir/$io.out"
fi
[[ $rc -eq 0 ]] || echo "${RED}exited $rc${RST}" >&2
exit $rc
+1
View File
@@ -5,4 +5,5 @@ dir="$(find_problem "$1")"; name="$(basename "$dir")"
[[ -d "$ROOT/skipped/$name" ]] && { echo "Error: skipped/$name already exists" >&2; exit 1; }
mkdir -p "$ROOT/skipped"
mv "$ROOT/solving/$name" "$ROOT/skipped/$name"
sync_cargo_manifest >/dev/null # the bin paths just moved
echo "Moved to skipped/$name (usaco undo $name to bring it back)"
+1 -2
View File
@@ -28,9 +28,8 @@ run_case() { # $1 = dir, $2 = io, $3 = tests/N.in path; returns 0 pass, 1 fail,
local dir="$1" io="$2" tin="$3" n ms start exp
n="$(basename "$tin" .in)"
cp "$tin" "$dir/$io.in"
rm -f "$dir/$io.out"
start=$(date +%s%N)
if ! (cd "$dir" && ./main); then
if ! run_solution "$dir" "$io"; then
echo "${RED}✗ case $n CRASHED (nonzero exit)${RST}"
return 1
fi
+1
View File
@@ -5,4 +5,5 @@ from="$(basename "$(dirname "$dir")")"
[[ "$from" != solving ]] || { echo "Error: $name is already in solving/" >&2; exit 1; }
[[ -d "$ROOT/solving/$name" ]] && { echo "Error: solving/$name already exists" >&2; exit 1; }
mv "$ROOT/$from/$name" "$ROOT/solving/$name"
sync_cargo_manifest >/dev/null # the bin paths just moved
echo "Moved back to solving/$name (was $from)"
+183 -8
View File
@@ -5,6 +5,40 @@
CXXFLAGS=(-O2 -std=c++17 -Wall -Wextra -DLOCAL -I"$ROOT")
# --debug (or USACO_DEBUG=1): catch out-of-bounds, overflow and UB at runtime
DEBUG_CXXFLAGS=(-g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer -D_GLIBCXX_DEBUG)
RUSTCFLAGS=(-O --edition 2021)
# --debug for rust: debug info, plus the overflow and debug_assert! checks -O drops
DEBUG_RUSTCFLAGS=(-g --edition 2021 -C debug-assertions -C overflow-checks)
# ----- languages -----
# A problem's language is just whichever source file it has: main.cpp or main.rs.
LANGS="cpp rust"
default_lang() { # language 'usaco new' uses when no flag is given
local l="${USACO_LANG:-}"
[[ -z "$l" && -r "$ROOT/.lang" ]] && l="$(<"$ROOT/.lang")"
normalize_lang "${l:-cpp}" 2>/dev/null || echo cpp
}
normalize_lang() { # accepts cpp/c++/cc/rust/rs, echoes cpp|rust
case "${1,,}" in
cpp|c++|cc|cxx) echo cpp ;;
rust|rs) echo rust ;;
*) echo "Error: unknown language '$1' (use cpp or rust)" >&2; return 1 ;;
esac
}
src_name() { [[ "$1" == rust ]] && echo main.rs || echo main.cpp; }
lang_of() { # $1 = problem dir; echoes cpp|rust
if [[ -f "$1/main.rs" ]]; then echo rust
elif [[ -f "$1/main.cpp" ]]; then echo cpp
else echo "Error: no main.cpp or main.rs in $1" >&2; return 1; fi
}
main_file() { # $1 = problem dir; echoes the path of its source file
local l; l="$(lang_of "$1")" || return 1
echo "$1/$(src_name "$l")"
}
find_problem() { # echoes dir of problem in solving/, solved/, or skipped/; unique prefix ok
local name="$1" s d matches=()
@@ -26,13 +60,102 @@ find_problem() { # echoes dir of problem in solving/, solved/, or skipped/; uni
return 1
}
build_problem() { # $1 = problem dir
if [[ -n "${USACO_DEBUG:-}" ]]; then
echo "${DIM}(debug build: sanitizers on)${RST}" >&2
g++ "${CXXFLAGS[@]}" "${DEBUG_CXXFLAGS[@]}" "$1/main.cpp" -o "$1/main"
else
g++ "${CXXFLAGS[@]}" "$1/main.cpp" -o "$1/main"
# ----- rust project model (for rust-analyzer / clippy) -----
# Problems are bare main.rs files with no manifest, and rust-analyzer only does
# real analysis -- type-aware diagnostics, clippy on save -- inside a cargo
# project. So we generate one package at the root that declares every rust
# solution (and template.rs) as its own [[bin]] target. Nothing builds through cargo: 'usaco
# build/run' still shells out to rustc. The manifest exists purely so the editor
# has something to chew on, which is why it's gitignored and regenerated from a
# directory scan (that also prunes problems that moved or went away).
#
# To change which lints fire, edit the [lints.clippy] table below.
sync_cargo_manifest() {
local s d name entry count=0 bins=""
for s in solving solved skipped; do
[[ -d "$ROOT/$s" ]] || continue
for d in "$ROOT/$s"/*/; do
[[ -f "${d}main.rs" ]] || continue
# Target names can't start with a digit, and problems like '101'
# do; the directory prefix keeps them legal and unique.
name="$s-$(basename "$d")"
printf -v entry '[[bin]]\nname = "%s"\npath = "%smain.rs"\n\n' "$name" "${d#"$ROOT"/}"
bins+="$entry"
count=$((count + 1))
done
done
# template.rs is a target too, so mistakes in it surface in the editor and
# in 'usaco lint template' instead of in every problem stamped out from it.
# Its name has no directory prefix, so it can't collide with a problem.
if [[ -f "$ROOT/template.rs" ]]; then
bins+=$'[[bin]]\nname = "template"\npath = "template.rs"\n\n'
fi
if [[ -z "$bins" ]]; then
rm -f "$ROOT/Cargo.toml"
return 0
fi
cat > "$ROOT/Cargo.toml" <<EOF
# GENERATED by 'usaco lint' -- edits here are lost. See sync_cargo_manifest()
# in lib/common.sh. This file only exists so rust-analyzer can analyse the
# solutions and run clippy on them; builds go through rustc, not cargo.
[package]
name = "usaco"
version = "0.0.0"
edition = "2021"
autobins = false
[lints.clippy]
pedantic = { level = "warn", priority = -1 }
# Casts between usize/i32/i64 are the bread and butter of competitive
# programming. Warning on each one buries the lints that find actual bugs.
cast_possible_truncation = "allow"
cast_possible_wrap = "allow"
cast_sign_loss = "allow"
cast_precision_loss = "allow"
# Library-API hygiene, meaningless for a single-file solution.
missing_panics_doc = "allow"
missing_errors_doc = "allow"
must_use_candidate = "allow"
doc_markdown = "allow"
# '|x| x.to_string()' reads better than the spelled-out method path.
redundant_closure_for_method_calls = "allow"
${bins%$'\n'}
EOF
printf '%s\n' "$count"
}
build_problem() { # $1 = problem dir; compiles to $1/main whatever the language
local dir="$1" lang
lang="$(lang_of "$dir")" || return 1
if [[ -n "${USACO_DEBUG:-}" ]]; then
echo "${DIM}(debug build: $([[ $lang == cpp ]] && echo sanitizers || echo overflow checks) on)${RST}" >&2
fi
case "$lang" in
cpp)
if [[ -n "${USACO_DEBUG:-}" ]]; then
g++ "${CXXFLAGS[@]}" "${DEBUG_CXXFLAGS[@]}" "$dir/main.cpp" -o "$dir/main"
else
g++ "${CXXFLAGS[@]}" "$dir/main.cpp" -o "$dir/main"
fi
;;
rust)
# Keep the editor's project model in step with what's on disk.
sync_cargo_manifest >/dev/null
if [[ -n "${USACO_DEBUG:-}" ]]; then
rustc "${DEBUG_RUSTCFLAGS[@]}" "$dir/main.rs" -o "$dir/main"
else
rustc "${RUSTCFLAGS[@]}" "$dir/main.rs" -o "$dir/main"
fi
;;
esac
}
detect_io() { # echoes the io basename (first *.in in $1), or nothing
@@ -60,8 +183,60 @@ solved_at() { # $1 = solved/<name> dir: epoch it (last) entered solved/
[[ -n "$t" ]] && echo "$t" || last_touched "$1"
}
fresh_main() { # $1 = problem dir, $2 = io name: main.cpp from template with STDIO wired up
sed "s/^int main() {$/int main() {\n STDIO(\"$2\");/" "$ROOT/template.cpp" > "$1/main.cpp"
fresh_main() { # $1 = problem dir, $2 = io name, [$3 = language]: source from template, io wired up
local dir="$1" io="$2" lang="${3:-}"
[[ -n "$lang" ]] || lang="$(lang_of "$dir")" || return 1
case "$lang" in
cpp) sed "s/^int main() {$/int main() {\n STDIO(\"$io\");/" "$ROOT/template.cpp" > "$dir/main.cpp" ;;
rust) sed "s/stdio(\"[^\"]*\")/stdio(\"$io\")/" "$ROOT/template.rs" > "$dir/main.rs" ;;
esac
}
wire_io() { # $1 = problem dir, $2 = io name: point the source's file-io call at <io>
local dir="$1" io="$2" lang src
lang="$(lang_of "$dir")" || return 1
src="$dir/$(src_name "$lang")"
if [[ "$lang" == rust ]]; then
if grep -q 'stdio("' "$src"; then
sed -i "s/stdio(\"[^\"]*\")/stdio(\"$io\")/" "$src"
echo "Updated stdio(\"$io\") in main.rs"
else
echo "Warning: couldn't find stdio(\"...\") in main.rs — wire it up manually" >&2
fi
else
if grep -q 'STDIO("' "$src"; then
sed -i "s/STDIO(\"[^\"]*\")/STDIO(\"$io\")/" "$src"
echo "Updated STDIO(\"$io\") in main.cpp"
elif grep -q '^int main() {$' "$src"; then
sed -i "s/^int main() {$/int main() {\n STDIO(\"$io\");/" "$src"
echo "Inserted STDIO(\"$io\") in main.cpp"
else
echo "Warning: couldn't find STDIO(...) or 'int main() {' in main.cpp — wire it up manually" >&2
fi
fi
}
show_io() { # $1 = problem dir: echo the io name the source is wired to, if any
local lang src; lang="$(lang_of "$1")" || return 1
src="$1/$(src_name "$lang")"
if [[ "$lang" == rust ]]; then
grep -o 'stdio("[^"]*")' "$src" | sed 's/stdio("\(.*\)")/\1/' | head -1
else
grep -o 'STDIO("[^"]*")' "$src" | sed 's/STDIO("\(.*\)")/\1/' | head -1
fi
}
run_solution() { # $1 = dir, $2 = io name: run ./main, leave its output in $1/$2.out
# Works for both styles: a solution that opens <io>.in/.out itself (STDIO / stdio)
# and one that just reads stdin and writes stdout (Codeforces-style).
local dir="$1" io="$2" tmp rc=0
rm -f "$dir/$io.out"
tmp="$(mktemp)"
[[ -e "$dir/$io.in" ]] || touch "$dir/$io.in"
(cd "$dir" && ./main) < "$dir/$io.in" > "$tmp" || rc=$?
[[ -e "$dir/$io.out" ]] || cp "$tmp" "$dir/$io.out"
rm -f "$tmp"
return $rc
}
# colors (disabled when not a tty or NO_COLOR is set)