Files
usaco/lib/cmd/new.sh
T
adamfandClaude Opus 5.5 1aee0050a0 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]>
2026-10-19 21:17:50 +01:00

42 lines
1.8 KiB
Bash

# usaco new <name> [io-name] [--url <link>] [--rust|--cpp] | usaco new <leetcode-url|daily>
[[ $# -ge 1 ]] || usage
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
[[ -n "$name" ]] || usage
# 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)"
(cd "$ROOT/solving/$name" && exec ${EDITOR:-nvim} main.cpp)
exit
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" "$lang"
touch "$dir/$io.in"
[[ -n "$url" ]] && echo "$url" > "$dir/.url"
[[ -n "${USACO_CREATED:-}" ]] && printf '%s\n' "$name" > "$USACO_CREATED"
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")