File: no-std.rs

package info (click to toggle)
rust-err-derive 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 164 kB
  • sloc: makefile: 4
file content (32 lines) | stat: -rw-r--r-- 900 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#![cfg(not(feature = "std"))]
extern crate alloc;

use alloc::format;
use alloc::string::{String, ToString};
use err_derive::Error;

#[derive(Debug, Error)]
pub enum FormatError {
    #[error(
        display = "invalid header (expected: {:?}, got: {:?})",
        expected,
        found
    )]
    InvalidHeader { expected: String, found: String },
    #[error(display = "missing attribute: {:?}", _0)]
    MissingAttribute(String),
}

#[derive(Debug, Error)]
pub enum LoadingError {
    #[error(display = "could not decode file")]
    FormatError(#[error(source)] FormatError),
    #[error(display = "could not find file: {:?}", path)]
    NotFound { path: String },
}

/// Verify that our error types implement `From` and `Display`
pub fn test_impl() {
    let loading_err: LoadingError = FormatError::MissingAttribute("attr".to_string()).into();
    let _ = format!("Error: {}", loading_err);
}