Cotton code
66 lines · 2.7 KB · RustRaw
//! **An `io::Error` never reaches a person.** One place turns one into a//! sentence, and the raw detail goes to the log.//!//! The walk of 15 Sep 2026, finding on `/messages/import`: a share that could//! not be staged raised a toast reading *"the recording could not be copied//! into this device's store: Permission denied (os error 13)"*. The second half//! is libc talking. It names nothing the person owns, suggests nothing they can//! do, and is the one part of that sentence they cannot act on — so it belongs//! in the log beside the path, which is where somebody debugging it will look//! anyway.//!//! What replaces it is the little that IS worth saying: the file has gone, the//! device refused, the disk is full. Every other kind says nothing extra rather//! than inventing a cause — the caller's own sentence already said what failed.//!//! Every message out of here is the caller's sentence, optionally with one//! plain clause after it. The CALLER owns the sentence, because only it knows//! whether the thing that would not copy was a recording, a picture or an//! export.use std::io::ErrorKind;use std::path::Path;/// `ENOSPC`. There is no stable `ErrorKind` for a full disk on every platform/// this crate builds for, and the number is the same everywhere it matters.const NO_SPACE_LEFT: i32 = 28;/// The sentence to show, for a `sentence` the caller wrote and an `error` only/// the log should see.////// The raw error and the path are logged HERE rather than at each call site, so/// dropping `: {error}` from a message cannot also drop the detail.pub fn io_refusal(sentence: impl Into<String>, path: &Path, error: &std::io::Error) -> String {    let sentence = sentence.into();    tracing::warn!(        target: "cotton_ffi::phone",        path = %path.display(),        error = %error,        kind = ?error.kind(),        "{sentence}"    );    match plain_reason(error) {        Some(reason) => format!("{sentence}: {reason}"),        None => sentence,    }}/// The clause to add after the caller's sentence, or `None` when nothing can be/// said that the person did not already know.////// Deliberately short of exhaustive: a kind nobody can act on differently is/// better as silence than as a guess, and the log has the real one.pub fn plain_reason(error: &std::io::Error) -> Option<&'static str> {    if error.raw_os_error() == Some(NO_SPACE_LEFT) {        return Some("there is no room left on this device");    }    match error.kind() {        ErrorKind::NotFound => Some("the file is no longer there"),        ErrorKind::PermissionDenied => Some("this device would not allow it"),        _ => None,    }}#[cfg(test)]#[path = "io_trouble_tests.rs"]mod tests;