Back to Groups
Development

Rust: CLI imports and bounded input

Rust 1.70+, edition 2021: CLI options, bounded line imports, checked size arguments and exclusive report files with no external crates. Review names and integration points before use.

workflowscoderustrust-developerstandard-library
JU
Jules
0likes12snippets

Try it here

Loading snippet…

Your typing stays in this tab. Nothing is saved or sent.
Snippets (12)
;rs-read-cap
Read up to a checked byte limit
use std::io::{self, Read}; pub fn read_bounded(reader: impl Read, max_bytes: usize) -> io::Result<V
;rs-writeall
Handle partial writes and flush errors
pub fn write_and_flush( writer: &mut impl std::io::Write, bytes: &[u8] ) -> std::io::Result<()>
;rs-unique
Keep the first occurrence of each value
pub fn unique_in_order<T>(values: impl IntoIterator<Item = T>) -> Vec<T> where T: Eq + std::hash
;rs-port
Parse a nonzero TCP or UDP port setting
pub fn parse_port(raw: &str) -> Result<u16, &'static str> { let port = raw.trim().parse::<u16>()
;rs-page
Compute a page slice without arithmetic overflow
pub fn page_range( page: usize, page_size: usize, total: usize ) -> Option<std::ops::Range<usize
;rs-prefix
Take a UTF-8 prefix within a byte budget
pub fn utf8_prefix(text: &str, max_bytes: usize) -> &str { let mut end = max_bytes.min(text.len(
;rs-settings
Parse a small key=value settings file
pub fn parse_settings(text: &str) -> Result<std::collections::BTreeMap<String, String>, &'static str
;rs-hex
Decode an even-length ASCII hex string
pub fn decode_hex(text: &str) -> Result<Vec<u8>, &'static str> { fn digit(byte: u8) -> Option<u8
;rs-newreport
Create a report file without replacing an existing path
pub fn write_new_report(path: &std::path::Path, bytes: &[u8]) -> std::io::Result<()> { use std::
;rs-lines
Process text lines without an unbounded line allocation
pub fn process_bounded_lines<R, F>(mut reader: R, max_line_bytes: usize, mut process: F) -> std::io:
;rs-cliargs
Parse a small import command's required and optional flags
#[derive(Debug, PartialEq)] pub struct ImportOptions { pub input: std::path::PathBuf, pub ma
;rs-bytesize
Parse a byte-budget argument with checked binary units
pub fn parse_byte_budget(raw: &str) -> Result<usize, &'static str> { let text = raw.trim();