CodeSampleX

Sample

thiserror 2.0.20: Define a library error type with thiserror and keep the source chain intact

Verified sample for cargo thiserror 2.0.20: Define a library error type with thiserror and keep the source chain intact. The contract ran on rust 1 · linux…

sha256:9a1d6344cb2cb3d1c49ea5c0109c80a69519cf2325dc01c5bbc82e5bddcb98cd

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
3
Signing keys that built it
3
Declared environment rust 1 linux x64 rust 1 rust cargo

Verification-run environments

Environment Contract Stages Run
rust 1 · linux alpine/x64 · docker ed25519:a2ec939a4c60e243 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-13
rust 1 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-17
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-09-08

Case

HOW
Goal
Define a library error type with thiserror and keep the source chain intact
Packages
Symbols
  • thiserror::Error
  • source
  • From
Environment
rust 1
Created
2026-08-13T18:45:30Z

Contract

  1. define an error enum with a transparent variant and a formatted one
  2. assert Display renders the declared message
  3. assert #[from] converts the underlying error automatically
  4. assert the source chain survives so callers can inspect the cause

Files

  • Cargo.lock
  • Cargo.toml
  • csx.json
  • src/main.rs

Download the source artifact (tar.gz)

Source

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "csx-sample-thiserror"
version = "1.0.0"
dependencies = [
 "thiserror",
]

[[package]]
name = "proc-macro2"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
 "unicode-ident",
]

[[package]]
name = "quote"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
 "proc-macro2",
]

[[package]]
name = "syn"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53e9bae58849f64dfa4f5d5ae372c8341f7305f82a3868709269343628b659a3"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-ident",
]

[[package]]
name = "thiserror"
version = "2.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec86235f5fcc2a73650310756d2ac5b138a5780bbbdfae3eeccec992c435ba4f"
dependencies = [
 "thiserror-impl",
]

[[package]]
name = "thiserror-impl"
version = "2.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc04cd3e1236dd4a98afca4569f2deb3f120e5422a4023be2cb683f8486292af"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
Cargo.toml
[package]
name = "csx-sample-thiserror"
version = "1.0.0"
edition = "2021"
license = "MIT-0"

[dependencies]
thiserror = "2.0.20"
csx.json
{"case":{"caseId":"case:sha256:d29597a6a668589fe9e905432cd2439c8e687a32e0c17a216ec82feed3426b9a","constraints":{"runtime":"rust"},"contract":["define an error enum with a transparent variant and a formatted one","assert Display renders the declared message","assert #[from] converts the underlying error automatically","assert the source chain survives so callers can inspect the cause"],"goal":"Define a library error type with thiserror and keep the source chain intact","kind":"HOW","packages":["pkg:cargo/thiserror@2.0.20"],"schemaVersion":1,"symbols":["thiserror::Error","source","From"]},"contractCommand":["cargo","run","--offline","--quiet"],"environment":{"arch":"x64","ecosystem":"cargo","executionContext":"rust","language":"rust","os":"linux","packageManager":"cargo","runtime":"rust","runtimeVersion":"1","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/thiserror@2.0.20"],"schemaVersion":1,"symbols":["thiserror::Error","source","From"],"verifierAdapter":"cargo@1"}
src/main.rs
use std::error::Error as StdError;
use thiserror::Error;

/// thiserror generates the Display and Error impls from the attributes; it
/// adds no runtime dependency to callers, which is why it belongs in a
/// library while anyhow belongs in an application.
///
/// #[from] generates the From impl AND marks that field as the error's
/// source, so `?` converts automatically and the cause chain stays walkable.
/// Formatting the underlying error into the message instead — write!("io
/// error: {e}") — produces the same text and silently breaks source(), so
/// callers can no longer downcast to find what actually failed.
#[derive(Error, Debug)]
enum LoadError {
    #[error("sample {id} is not cached")]
    NotCached { id: String },

    #[error("reading the manifest failed")]
    Manifest(#[from] std::num::ParseIntError),

    #[error(transparent)]
    Passthrough(#[from] std::fmt::Error),
}

fn parse_size(text: &str) -> Result<u64, LoadError> {
    Ok(text.parse::<u64>()?)
}

fn main() {
    let missing = LoadError::NotCached { id: "sha256:abc".into() };
    assert_eq!(missing.to_string(), "sample sha256:abc is not cached");
    assert!(missing.source().is_none());

    let err = parse_size("not-a-number").unwrap_err();
    assert_eq!(err.to_string(), "reading the manifest failed");

    // The cause survives and is still downcastable to the original type.
    let source = err.source().expect("a source is attached");
    assert!(source.downcast_ref::<std::num::ParseIntError>().is_some());
    assert!(source.to_string().contains("invalid digit"));

    // transparent forwards both Display and source to the inner error.
    let inner = LoadError::Passthrough(std::fmt::Error);
    assert_eq!(inner.to_string(), std::fmt::Error.to_string());

    assert_eq!(parse_size("4096").unwrap(), 4096);

    println!("CONTRACT PASS: thiserror kept the message and the source chain");
}

Origin Seeder

anonymous