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:
+1
-1
@@ -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
@@ -1,4 +1,4 @@
|
||||
# usaco cat <name>
|
||||
[[ $# -ge 1 ]] || usage
|
||||
dir="$(find_problem "$1")"
|
||||
cat "$dir/main.cpp"
|
||||
cat "$(main_file "$dir")"
|
||||
|
||||
+2
-1
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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'"
|
||||
|
||||
@@ -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
|
||||
@@ -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
@@ -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
@@ -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
@@ -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
|
||||
|
||||
@@ -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
@@ -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
|
||||
|
||||
@@ -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)"
|
||||
|
||||
Reference in New Issue
Block a user