CodeSampleX

示例

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

已验证示例 — cargo thiserror 2.0.20: Define a library error type with thiserror and keep the source chain intact. contract 在 rust 1 · linux alpine/x64 · docker…

sha256:9a1d6344cb2cb3d1c49ea5c0109c80a69519cf2325dc01c5bbc82e5bddcb98cd

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
3
构建过它的签名密钥
3
声明的环境 rust 1 linux x64 rust 1 rust cargo

验证运行环境

环境 契约 阶段 运行日期
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

案例

HOW
目标
Define a library error type with thiserror and keep the source chain intact
包
符号
  • thiserror::Error
  • source
  • From
环境
rust 1
创建时间
2026-08-13T18:45:30Z

契约

  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

文件

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

下载源代码构件 (tar.gz)

源代码

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");
}

原始种子者

anonymous