//! Small TOML reader for Cargo manifests (std only): tables, dotted and quoted keys, arrays of
//! tables, strings (basic, literal, multi-line), integers, floats, booleans, arrays and inline
//! tables. Every entry keeps its line number for findings. Not an entry point; `mod toml;`.
#![allow(dead_code)]
#[derive(Debug, Clone)]
pub enum Val {
Str(String),
Int(i64),
Float(f64),
Bool(bool),
Arr(Vec<Val>),
Tbl(Table),
}
#[derive(Debug, Clone, Default)]
pub struct Table {
pub entries: Vec<(String, Val, usize)>,
pub line: usize,
}
impl Val {
pub fn as_str(&self) -> Option<&str> {
if let Val::Str(s) = self { Some(s) } else { None }
}
pub fn as_bool(&self) -> Option<bool> {
if let Val::Bool(b) = self { Some(*b) } else { None }
}
pub fn as_table(&self) -> Option<&Table> {
if let Val::Tbl(t) = self { Some(t) } else { None }
}
pub fn as_array(&self) -> Option<&[Val]> {
if let Val::Arr(a) = self { Some(a) } else { None }
}
}
impl Table {
pub fn get(&self, key: &str) -> Option<&Val> {
self.entries.iter().find(|(k, _, _)| k == key).map(|(_, v, _)| v)
}
pub fn line_of(&self, key: &str) -> usize {
self.entries.iter().find(|(k, _, _)| k == key).map_or(self.line, |(_, _, l)| *l)
}
pub fn table(&self, key: &str) -> Option<&Table> {
self.get(key).and_then(Val::as_table)
}
/// Follow a dotted path of tables: `path(&["workspace", "package"])`.
pub fn path(&self, keys: &[&str]) -> Option<&Val> {
let (last, init) = keys.split_last()?;
let mut t = self;
for k in init {
t = t.table(k)?;
}
t.get(last)
}
pub fn str_at(&self, keys: &[&str]) -> Option<&str> {
self.path(keys).and_then(Val::as_str)
}
fn get_mut(&mut self, key: &str) -> Option<&mut Val> {
self.entries.iter_mut().find(|(k, _, _)| k == key).map(|(_, v, _)| v)
}
/// Get or create the sub-table `key` (for headers and dotted keys).
fn child(&mut self, key: &str, line: usize) -> Result<&mut Table, String> {
if self.get(key).is_none() {
self.entries.push((key.to_string(), Val::Tbl(Table { entries: vec![], line }), line));
}
match self.get_mut(key) {
Some(Val::Tbl(t)) => Ok(t),
Some(Val::Arr(a)) => match a.last_mut() {
Some(Val::Tbl(t)) => Ok(t),
_ => Err(format!("line {line}: `{key}` is not a table")),
},
_ => Err(format!("line {line}: `{key}` is not a table")),
}
}
}
/// Parse a whole document.
pub fn parse(text: &str) -> Result<Table, String> {
let mut p = P { s: text.as_bytes(), i: 0, line: 1 };
let mut root = Table { entries: vec![], line: 1 };
let mut current: Vec<String> = Vec::new();
loop {
p.skip_ws_nl();
let Some(c) = p.peek() else { break };
if c == b'[' {
let array = p.s.get(p.i + 1) == Some(&b'[');
p.i += if array { 2 } else { 1 };
let keys = p.key_path(b']')?;
p.expect(b']')?;
if array {
p.expect(b']')?;
}
let line = p.line;
let (last, init) = keys.split_last().ok_or(format!("line {line}: empty table header"))?;
let mut t = &mut root;
for k in init {
t = t.child(k, line)?;
}
if array {
match t.get_mut(last) {
Some(Val::Arr(a)) => a.push(Val::Tbl(Table { entries: vec![], line })),
None => t.entries.push((last.clone(), Val::Arr(vec![Val::Tbl(Table { entries: vec![], line })]), line)),
_ => return Err(format!("line {line}: `{last}` is not an array of tables")),
}
} else {
t.child(last, line)?;
}
current = keys;
p.end_of_line()?;
} else {
let line = p.line;
let keys = p.key_path(b'=')?;
p.skip_ws();
p.expect(b'=')?;
let val = p.value()?;
let mut t = &mut root;
for k in ¤t {
t = t.child(k, line)?;
}
let (last, init) = keys.split_last().ok_or(format!("line {line}: empty key"))?;
for k in init {
t = t.child(k, line)?;
}
t.entries.push((last.clone(), val, line));
p.end_of_line()?;
}
}
Ok(root)
}
struct P<'a> {
s: &'a [u8],
i: usize,
line: usize,
}
impl P<'_> {
fn peek(&self) -> Option<u8> {
self.s.get(self.i).copied()
}
fn err<T>(&self, what: &str) -> Result<T, String> {
Err(format!("line {}: {what}", self.line))
}
fn expect(&mut self, c: u8) -> Result<(), String> {
self.skip_ws();
if self.peek() == Some(c) {
self.i += 1;
Ok(())
} else {
self.err(&format!("expected '{}'", c as char))
}
}
fn skip_ws(&mut self) {
while matches!(self.peek(), Some(b' ' | b'\t')) {
self.i += 1;
}
}
/// Whitespace, newlines and comments.
fn skip_ws_nl(&mut self) {
loop {
match self.peek() {
Some(b' ' | b'\t' | b'\r') => self.i += 1,
Some(b'\n') => { self.i += 1; self.line += 1; }
Some(b'#') => while !matches!(self.peek(), None | Some(b'\n')) { self.i += 1 },
_ => return,
}
}
}
fn end_of_line(&mut self) -> Result<(), String> {
self.skip_ws();
if self.peek() == Some(b'#') {
while !matches!(self.peek(), None | Some(b'\n')) {
self.i += 1;
}
}
match self.peek() {
None | Some(b'\n') | Some(b'\r') => Ok(()),
_ => self.err("unexpected text after value"),
}
}
/// `a.b."c d".'e'` up to (not including) `stop`.
fn key_path(&mut self, stop: u8) -> Result<Vec<String>, String> {
let mut keys = Vec::new();
loop {
self.skip_ws();
let key = match self.peek() {
Some(b'"') => self.basic_string()?,
Some(b'\'') => self.literal_string()?,
_ => {
let start = self.i;
while matches!(self.peek(), Some(c) if c.is_ascii_alphanumeric() || c == b'_' || c == b'-') {
self.i += 1;
}
if start == self.i {
return self.err("expected a key");
}
String::from_utf8_lossy(&self.s[start..self.i]).into_owned()
}
};
keys.push(key);
self.skip_ws();
match self.peek() {
Some(b'.') => self.i += 1,
Some(c) if c == stop => return Ok(keys),
_ => return self.err(&format!("expected '.' or '{}' after key", stop as char)),
}
}
}
fn value(&mut self) -> Result<Val, String> {
self.skip_ws();
match self.peek() {
Some(b'"') => self.basic_string().map(Val::Str),
Some(b'\'') => self.literal_string().map(Val::Str),
Some(b'[') => {
self.i += 1;
let mut items = Vec::new();
loop {
self.skip_ws_nl();
if self.peek() == Some(b']') {
self.i += 1;
return Ok(Val::Arr(items));
}
items.push(self.value()?);
self.skip_ws_nl();
match self.peek() {
Some(b',') => self.i += 1,
Some(b']') => {}
_ => return self.err("expected ',' or ']' in array"),
}
}
}
Some(b'{') => {
self.i += 1;
let mut t = Table { entries: vec![], line: self.line };
loop {
self.skip_ws();
if self.peek() == Some(b'}') {
self.i += 1;
return Ok(Val::Tbl(t));
}
let line = self.line;
let keys = self.key_path(b'=')?;
self.expect(b'=')?;
let val = self.value()?;
let (last, init) = keys.split_last().ok_or("empty key")?;
let mut sub = &mut t;
for k in init {
sub = sub.child(k, line)?;
}
sub.entries.push((last.clone(), val, line));
self.skip_ws();
match self.peek() {
Some(b',') => self.i += 1,
Some(b'}') => {}
_ => return self.err("expected ',' or '}' in inline table"),
}
}
}
Some(b't') if self.s[self.i..].starts_with(b"true") => { self.i += 4; Ok(Val::Bool(true)) }
Some(b'f') if self.s[self.i..].starts_with(b"false") => { self.i += 5; Ok(Val::Bool(false)) }
Some(c) if c == b'-' || c == b'+' || c.is_ascii_digit() => {
let start = self.i;
while matches!(self.peek(), Some(c) if c.is_ascii_alphanumeric() || matches!(c, b'.' | b'-' | b'+' | b'_' | b':')) {
self.i += 1;
}
let text: String = String::from_utf8_lossy(&self.s[start..self.i]).replace('_', "");
if let Ok(n) = text.parse::<i64>() {
Ok(Val::Int(n))
} else if let Ok(f) = text.parse::<f64>() {
Ok(Val::Float(f))
} else {
Ok(Val::Str(text)) // dates and other scalars: keep the text
}
}
_ => self.err("expected a value"),
}
}
fn basic_string(&mut self) -> Result<String, String> {
let multi = self.s[self.i..].starts_with(b"\"\"\"");
self.i += if multi { 3 } else { 1 };
if multi && self.peek() == Some(b'\n') {
self.i += 1;
self.line += 1;
}
let mut out = String::new();
loop {
let Some(c) = self.peek() else { return self.err("unterminated string") };
if multi && self.s[self.i..].starts_with(b"\"\"\"") {
self.i += 3;
return Ok(out);
}
self.i += 1;
match c {
b'"' if !multi => return Ok(out),
b'\n' if !multi => return self.err("newline in string"),
b'\n' => { self.line += 1; out.push('\n'); }
b'\\' => {
let Some(e) = self.peek() else { return self.err("bad escape") };
self.i += 1;
match e {
b'n' => out.push('\n'),
b't' => out.push('\t'),
b'r' => out.push('\r'),
b'"' => out.push('"'),
b'\\' => out.push('\\'),
b'u' | b'U' => {
let n = if e == b'u' { 4 } else { 8 };
let hex = String::from_utf8_lossy(self.s.get(self.i..self.i + n).unwrap_or_default()).into_owned();
self.i += n;
out.push(u32::from_str_radix(&hex, 16).ok().and_then(char::from_u32).unwrap_or('\u{fffd}'));
}
b'\n' if multi => { self.line += 1; self.skip_ws_nl(); }
_ => return self.err("bad escape"),
}
}
_ => {
let start = self.i - 1;
let len = match c { 0..=0x7f => 1, 0xc0..=0xdf => 2, 0xe0..=0xef => 3, _ => 4 };
out.push_str(&String::from_utf8_lossy(&self.s[start..(start + len).min(self.s.len())]));
self.i = start + len;
}
}
}
}
fn literal_string(&mut self) -> Result<String, String> {
let multi = self.s[self.i..].starts_with(b"'''");
self.i += if multi { 3 } else { 1 };
let close: &[u8] = if multi { b"'''" } else { b"'" };
let start = self.i;
while self.i < self.s.len() && !self.s[self.i..].starts_with(close) {
if self.s[self.i] == b'\n' {
if !multi {
return self.err("newline in string");
}
self.line += 1;
}
self.i += 1;
}
if self.i >= self.s.len() {
return self.err("unterminated string");
}
let text = String::from_utf8_lossy(&self.s[start..self.i]).into_owned();
self.i += close.len();
Ok(text)
}
}
rust-borrow-checker-error-explainer
Explain and fix Rust borrow-checker, ownership, move and lifetime compiler errors: E0382 use of moved value, E0499 cannot borrow as mutable more than once, E0502 also borrowed as immutable, E0505, E0506, E0503, E0507 cannot move out of index/Rc/reference, E0515 return reference to local, E0597 does not live long enough, E0716 temporary value dropped while borrowed, E0373 closure may outlive (spawn needs move), E0521 borrowed data escapes, E0384, E0596, E0594, E0381, E0713, 'lifetime may not live long enough', RefCell 'already borrowed: BorrowMutError' panics. Ranks fixes from restructuring (split borrows, entry API, split_at_mut, get_disjoint_mut, mem::take, Option::take, indices, thread::scope) down to clone and Rc<RefCell>/Arc<Mutex>; never unsafe. Covers NLL limits (get-or-insert), iterator invalidation, closure captures, self-referential structs. Ships std-only Rust triage tools.
rust-error-handling-thiserror-anyhow-designer
Design and review Rust error handling with thiserror 2 and anyhow 1: library vs application split, typed error enums with #[from], #[source], #[error(transparent)] and #[non_exhaustive], ErrorKind patterns, context via .context()/.with_context(), bail!/ensure!, source() chains, {:#} and {:?} reports, RUST_BACKTRACE/RUST_LIB_BACKTRACE, Box<dyn Error + Send + Sync>, main -> anyhow::Result or ExitCode, unwrap/expect/panic policy, Option to Result, collecting iterators of Results, axum IntoResponse mapping, retryable errors, tracing, async Send bounds, testing with matches! and downcast_ref, no_std core::error::Error, and snafu, eyre/color-eyre, miette, error-stack.
rust-lifetime-annotation-advisor
Decide when Rust lifetime annotations are needed and which ones: the three elision rules, structs holding references (struct Parser<'a>), owned vs borrowed (String, &str, Cow<'a, str>, Arc<str>), one vs several lifetimes, 'a: 'b bounds, T: 'static misconceptions, Box<dyn Trait + 'a> default object lifetimes, impl Trait + '_ and edition 2024 capture rules with use<..>, variance and PhantomData, HRTB for<'a> Fn(&'a T), GATs and lending iterators, async fn and tokio::spawn 'static, self-referential structs (ouroboros, self_cell, indices, arenas), Rc/Arc to escape lifetime soup. Ships std-only Rust checkers for redundant, missing, unused and over-restrictive lifetimes.
rust-tokio-async-service-builder
Build and review production async Rust services on Tokio 1.x and axum 0.8: runtime choice (#[tokio::main] multi_thread vs current_thread, Builder), axum routing with /{id} paths, State, extractors, IntoResponse error types, axum::serve with_graceful_shutdown, tower/tower-http middleware (TraceLayer, TimeoutLayer, CorsLayer, ConcurrencyLimitLayer, RequestBodyLimitLayer), Arc<AppState>, std vs tokio Mutex, JoinSet, TaskTracker, CancellationToken, select! cancel safety, bounded mpsc, oneshot, broadcast, watch, actors, Semaphore, timeouts, retries, spawn_blocking, sqlx PgPool, reqwest Client reuse, tracing, tests with start_paused and oneshot, Dockerfile. Fixes 'future cannot be sent between threads safely', E0373, Rc or MutexGuard held across await, 'Cannot start a runtime from within a runtime', 'Path segments must not start with :'. Ships std-only Rust scanners and a compiling axum 0.8 scaffold.
rust-trait-design-reviewer
Review and design Rust traits and generic APIs: generics vs impl Trait vs dyn Trait (static vs dynamic dispatch), dyn compatibility / object safety (generic methods, Self returns, where Self: Sized, error[E0038] not dyn compatible / cannot be made into an object), associated types vs generic parameters, blanket impls, coherence and orphan rule (E0117, E0119, E0210), newtype, extension, sealed and marker traits, supertraits and trait upcasting (1.86), async fn in traits (1.75) and the Send bound problem (trait_variant, async-trait), RPITIT, GATs, From/Into/TryFrom/AsRef/Borrow, std trait checklist (Debug, Clone, PartialEq, Hash, Default, Display, Error, Send/Sync, serde feature), #[must_use], #[non_exhaustive], semver hazards, Deref abuse, builders, typestate, PhantomData, Box<dyn Error + Send + Sync>, Fn traits, mockall.
rust-unsafe-block-auditor
Audit unsafe Rust for soundness: inventory every unsafe block, unsafe fn, unsafe impl/trait, extern block and unsafe attribute, check // SAFETY: comments and # Safety docs, and classify each as sound, needs-justification or likely UB, proposing safe alternatives first. Covers the five unsafe superpowers, aliasing (Stacked/Tree Borrows), invalid values, MaybeUninit vs mem::zeroed/uninitialized, transmute, slice::from_raw_parts, Vec::set_len/from_raw_parts, get_unchecked, CString::as_ptr dangling, unsafe impl Send/Sync, Pin projection, FFI (extern "C", repr(C), C-unwind, Box::into_raw/from_raw), strict provenance, static mut and &raw, edition 2024 (unsafe extern, #[unsafe(no_mangle)], unsafe_op_in_unsafe_fn, env::set_var), Miri, sanitizers, cargo-fuzz, loom, cargo-geiger and clippy safety lints.