Sha256: e21302d7f2cf59161566e7f224c70cd892694f1ea173fb205db0c2cb82c40e2c

Contents?: true

Size: 1.51 KB

Versions: 38

Compression:

Stored size: 1.51 KB

Contents

use crate::codegen_settings::TrappableErrorType;
use crate::names;

use proc_macro2::TokenStream;
use quote::quote;

pub(super) fn define_error(
    name: &witx::Id,
    _v: &witx::Variant,
    e: &TrappableErrorType,
) -> TokenStream {
    let abi_error = names::type_(name);
    let rich_error = e.typename();

    quote! {
        #[derive(Debug)]
        pub struct #rich_error {
            inner: anyhow::Error,
        }

        impl std::fmt::Display for #rich_error {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "{}", self.inner)
            }
        }
        impl std::error::Error for #rich_error {
            fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
                self.inner.source()
            }
        }

        impl #rich_error {
            pub fn trap(inner: anyhow::Error) -> #rich_error {
                Self { inner }
            }
            pub fn downcast(self) -> Result<#abi_error, anyhow::Error> {
                self.inner.downcast()
            }
            pub fn downcast_ref(&self) -> Option<&#abi_error> {
                self.inner.downcast_ref()
            }
            pub fn context(self, s: impl Into<String>) -> Self {
                Self { inner: self.inner.context(s.into()) }
            }
        }

        impl From<#abi_error> for #rich_error {
            fn from(abi: #abi_error) -> #rich_error {
                #rich_error { inner: anyhow::Error::from(abi) }
            }
        }
    }
}

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/wiggle-generate-29.0.0/src/types/error.rs
wasmtime-28.0.0 ./ext/cargo-vendor/wiggle-generate-28.0.0/src/types/error.rs
wasmtime-27.0.0 ./ext/cargo-vendor/wiggle-generate-27.0.0/src/types/error.rs
wasmtime-26.0.0 ./ext/cargo-vendor/wiggle-generate-26.0.0/src/types/error.rs
wasmtime-25.0.2 ./ext/cargo-vendor/wiggle-generate-25.0.2/src/types/error.rs
wasmtime-25.0.1 ./ext/cargo-vendor/wiggle-generate-25.0.1/src/types/error.rs
wasmtime-25.0.0 ./ext/cargo-vendor/wiggle-generate-25.0.0/src/types/error.rs
wasmtime-24.0.0 ./ext/cargo-vendor/wiggle-generate-24.0.0/src/types/error.rs
wasmtime-23.0.2 ./ext/cargo-vendor/wiggle-generate-23.0.2/src/types/error.rs
wasmtime-22.0.0 ./ext/cargo-vendor/wiggle-generate-22.0.0/src/types/error.rs
wasmtime-21.0.1 ./ext/cargo-vendor/wiggle-generate-21.0.1/src/types/error.rs
wasmtime-20.0.2 ./ext/cargo-vendor/wiggle-generate-20.0.2/src/types/error.rs
wasmtime-20.0.0 ./ext/cargo-vendor/wiggle-generate-20.0.0/src/types/error.rs
wasmtime-18.0.3 ./ext/cargo-vendor/wiggle-generate-18.0.3/src/types/error.rs
wasmtime-17.0.1 ./ext/cargo-vendor/wiggle-generate-17.0.1/src/types/error.rs
wasmtime-17.0.0 ./ext/cargo-vendor/wiggle-generate-17.0.0/src/types/error.rs
wasmtime-16.0.0 ./ext/cargo-vendor/wiggle-generate-16.0.0/src/types/error.rs
wasmtime-15.0.1 ./ext/cargo-vendor/wiggle-generate-15.0.1/src/types/error.rs
wasmtime-15.0.0 ./ext/cargo-vendor/wiggle-generate-15.0.0/src/types/error.rs
wasmtime-14.0.4 ./ext/cargo-vendor/wiggle-generate-14.0.4/src/types/error.rs