erra 0.1.0

Zero-dependency, type-preserving error annotation for Result
Documentation
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Validates the zero-allocation static annotation path.
//!
//! These tests run under the default `std` feature but exercise only the
//! code paths that are present in every build configuration, including
//! `--no-default-features` (bare `no_std`, no allocator). They use only
//! `core`-compatible types and patterns throughout.
//!
//! # Why this file exists
//!
//! A true `no_std` binary cannot be executed by the standard `cargo test`
//! harness — the harness itself requires `std`. The correct verification
//! strategy for `no_std` compatibility is therefore two-pronged:
//!
//! 1. **Compile check**: `cargo check --target thumbv6m-none-eabi
//!    --no-default-features` proves the crate compiles for a bare-metal
//!    target with no allocator. This is part of the CI acceptance matrix
//!    but cannot be run as a `#[test]`.
//!
//! 2. **Runtime tests under `std`**: This file runs the same logic that
//!    the static path would execute on a `no_std` target, but inside the
//!    `std` harness. Where a `no_std` binary would write to a UART, we
//!    write into a fixed-size stack buffer via `core::fmt::Write`. This
//!    gives us runtime assertion coverage of every static-path code branch
//!    without requiring a target runner.
//!
//! Every test in this file intentionally avoids:
//! - `String`, `Vec`, `Box`, or any other heap type
//! - `std::io`, `std::fs`, `std::net`, or any `std`-only API
//! - `format!` macro (which allocates) — all formatting goes into the
//!   stack `WriteBuf` defined below
//! - `annotate_with` (requires `alloc`)
//!
//! # Stack write buffer
//!
//! `WriteBuf` is a fixed-capacity `[u8; N]` buffer that implements
//! `core::fmt::Write`. It is used everywhere a formatted string is needed
//! without heap allocation. It is intentionally defined here and not
//! exported — it is test infrastructure, not library code.

use core::fmt::Write;
use erra::{Error, ResultExt};

// ─────────────────────────────────────────────────────────────────────────────
// Stack write buffer — core::fmt::Write without heap allocation
// ─────────────────────────────────────────────────────────────────────────────

/// A fixed-capacity stack-allocated write buffer.
///
/// Capacity is chosen to be large enough for all test outputs while remaining
/// entirely on the stack. A `no_std` embedded implementation would typically
/// write directly to a peripheral register; this buffer serves as the test
/// stand-in.
struct WriteBuf<const N: usize> {
    buf: [u8; N],
    pos: usize,
}

impl<const N: usize> WriteBuf<N> {
    const fn new() -> Self {
        Self {
            buf: [0u8; N],
            pos: 0,
        }
    }

    /// Returns the written portion of the buffer as a `&str`.
    ///
    /// # Panics
    ///
    /// Panics if the written bytes are not valid UTF-8. All writes in this
    /// test file originate from `fmt::Display` / `fmt::Debug` impls that
    /// produce valid UTF-8, so this never triggers in practice.
    fn as_str(&self) -> &str {
        core::str::from_utf8(&self.buf[..self.pos])
            .expect("WriteBuf contains invalid UTF-8")
    }
}

impl<const N: usize> Write for WriteBuf<N> {
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        let bytes = s.as_bytes();
        let end = self.pos + bytes.len();
        if end > N {
            return Err(core::fmt::Error);
        }
        self.buf[self.pos..end].copy_from_slice(bytes);
        self.pos = end;
        Ok(())
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Embedded-style HAL error type
//
// Defined at module scope so it is available to all test functions below,
// including those outside the embedded-pattern section.
// ─────────────────────────────────────────────────────────────────────────────

/// A simplified HAL error type that does not require `std` or heap allocation.
#[derive(Debug, PartialEq, Clone, Copy)]
#[allow(dead_code)]
enum HalError {
    Timeout,
    BusError,
    InvalidAddress,
}

impl core::fmt::Display for HalError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            HalError::Timeout        => f.write_str("hardware timeout"),
            HalError::BusError       => f.write_str("bus error"),
            HalError::InvalidAddress => f.write_str("invalid address"),
        }
    }
}

fn read_sensor_register() -> Result<u8, HalError> {
    Err(HalError::Timeout)
}

fn calibrate_sensor() -> Result<u8, Error<HalError>> {
    read_sensor_register().annotate("reading calibration register 0x42")
}

fn init_device() -> Result<u8, Error<Error<HalError>>> {
    calibrate_sensor().annotate("device initialisation")
}

// ─────────────────────────────────────────────────────────────────────────────
// annotate — Ok path
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn static_annotate_ok_is_passthrough() {
    let result: Result<u32, u32> = Ok(42);
    let v = result.annotate("irrelevant").unwrap();
    assert_eq!(v, 42);
}

#[test]
fn static_annotate_ok_unit_type() {
    let result: Result<(), u32> = Ok(());
    assert!(result.annotate("step").is_ok());
}

#[test]
fn static_annotate_ok_with_static_str_source() {
    let result: Result<u8, &'static str> = Ok(0xFF);
    let v = result.annotate("decode").unwrap();
    assert_eq!(v, 0xFF);
}

// ─────────────────────────────────────────────────────────────────────────────
// annotate — Err path
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn static_annotate_err_wraps_with_correct_context() {
    let result: Result<(), u32> = Err(5);
    let err = result.annotate("static message").unwrap_err();
    assert_eq!(err.context(), "static message");
}

#[test]
fn static_annotate_err_preserves_source() {
    let result: Result<(), u32> = Err(99);
    let err = result.annotate("ctx").unwrap_err();
    assert_eq!(err.source, 99u32);
}

#[test]
fn static_annotate_err_with_u8_source() {
    let result: Result<(), u8> = Err(0xDE);
    let err = result.annotate("parse byte").unwrap_err();
    assert_eq!(err.source, 0xDE_u8);
    assert_eq!(err.context(), "parse byte");
}

#[test]
fn static_annotate_err_with_static_str_source() {
    let result: Result<(), &'static str> = Err("device not ready");
    let err = result.annotate("init sequence").unwrap_err();
    assert_eq!(err.source, "device not ready");
    assert_eq!(err.context(), "init sequence");
}

#[test]
fn static_annotate_err_with_i32_source() {
    let result: Result<(), i32> = Err(-1);
    let err = result.annotate("syscall").unwrap_err();
    assert_eq!(err.source, -1i32);
}

// ─────────────────────────────────────────────────────────────────────────────
// Error::new constructor — static path
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn new_constructor_sets_context() {
    let e = Error::new("register write failed", 0u32);
    assert_eq!(e.context(), "register write failed");
}

#[test]
fn new_constructor_sets_source() {
    let e = Error::new("dma fault", 0xDEAD_BEEFu32);
    assert_eq!(e.source, 0xDEAD_BEEFu32);
}

// ─────────────────────────────────────────────────────────────────────────────
// Display — stack write buffer, no heap
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn display_formats_as_context_colon_source() {
    let e = Error::new("step", 0u8);
    let mut buf = WriteBuf::<64>::new();
    write!(buf, "{e}").expect("WriteBuf must not overflow for this input");
    assert_eq!(buf.as_str(), "step: 0");
}

#[test]
fn display_with_static_str_source() {
    let e = Error::new("boot", "sensor offline");
    let mut buf = WriteBuf::<64>::new();
    write!(buf, "{e}").unwrap();
    assert_eq!(buf.as_str(), "boot: sensor offline");
}

#[test]
fn display_with_u32_source() {
    let e = Error::new("fault code", 0xCAFEu32);
    let mut buf = WriteBuf::<64>::new();
    write!(buf, "{e}").unwrap();
    assert_eq!(buf.as_str(), "fault code: 51966");
}

#[test]
fn display_with_i32_negative_source() {
    let e = Error::new("errno", -22i32);
    let mut buf = WriteBuf::<64>::new();
    write!(buf, "{e}").unwrap();
    assert_eq!(buf.as_str(), "errno: -22");
}

#[test]
fn display_chained_two_layers_no_heap() {
    let err = Err::<(), u8>(7)
        .annotate("inner")
        .annotate("outer")
        .unwrap_err();

    let mut buf = WriteBuf::<128>::new();
    write!(buf, "{err}").unwrap();

    let s = buf.as_str();
    assert!(s.starts_with("outer:"), "outermost context must lead: {s}");
    assert!(s.contains("inner"), "inner context must be present: {s}");
    assert!(s.contains('7'), "root error value must appear: {s}");
}

// ─────────────────────────────────────────────────────────────────────────────
// Debug — stack write buffer, no heap
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn debug_contains_context_field() {
    let e = Error::new("my context", 0u8);
    let mut buf = WriteBuf::<128>::new();
    write!(buf, "{e:?}").unwrap();
    let s = buf.as_str();
    assert!(s.contains("my context"), "Debug missing context: {s}");
}

#[test]
fn debug_contains_source_field() {
    let e = Error::new("ctx", 42u32);
    let mut buf = WriteBuf::<128>::new();
    write!(buf, "{e:?}").unwrap();
    let s = buf.as_str();
    assert!(s.contains("42"), "Debug missing source value: {s}");
}

#[test]
fn debug_names_the_struct() {
    let e = Error::new("ctx", 0u8);
    let mut buf = WriteBuf::<128>::new();
    write!(buf, "{e:?}").unwrap();
    let s = buf.as_str();
    assert!(s.contains("Error"), "Debug output missing struct name: {s}");
}

// ─────────────────────────────────────────────────────────────────────────────
// context() accessor — static path
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn context_accessor_returns_correct_str() {
    let e = Error::new("interrupt handler", 0xFFu8);
    assert_eq!(e.context(), "interrupt handler");
}

#[test]
fn context_accessor_on_annotated_result() {
    let err = Err::<(), u32>(1)
        .annotate("context string")
        .unwrap_err();
    assert_eq!(err.context(), "context string");
}

// ─────────────────────────────────────────────────────────────────────────────
// into_source — static path
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn into_source_recovers_original_value() {
    let e = Error::new("ctx", 0xABu8);
    assert_eq!(e.into_source(), 0xABu8);
}

#[test]
fn into_source_on_annotated_result() {
    let source = Err::<(), u32>(404)
        .annotate("not found")
        .unwrap_err()
        .into_source();
    assert_eq!(source, 404u32);
}

// ─────────────────────────────────────────────────────────────────────────────
// map — static path
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn map_transforms_source_preserves_context_no_heap() {
    let e = Error::new("transform", 2u32);
    let mapped: Error<u64> = e.map(|v| v as u64 * 10);
    assert_eq!(mapped.source, 20u64);
    assert_eq!(mapped.context(), "transform");
}

#[test]
fn map_to_static_str_no_heap() {
    let e = Error::new("classify", 0u8);
    let mapped: Error<&'static str> = e.map(|v| if v == 0 { "zero" } else { "nonzero" });
    assert_eq!(mapped.source, "zero");
    assert_eq!(mapped.context(), "classify");
}

// ─────────────────────────────────────────────────────────────────────────────
// Clone / PartialEq — static path with cloneable E
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn clone_on_static_path_produces_equal_value() {
    let e = Error::new("ctx", 7u8);
    let cloned = e.clone();
    assert_eq!(e, cloned);
}

#[test]
fn partial_eq_on_static_path() {
    let a = Error::new("ctx", 1u32);
    let b = Error::new("ctx", 1u32);
    let c = Error::new("ctx", 2u32);
    assert_eq!(a, b);
    assert_ne!(a, c);
}

// ─────────────────────────────────────────────────────────────────────────────
// ? operator — static path
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn question_mark_propagates_annotated_error() {
    fn inner() -> Result<(), u32> {
        Err(77)
    }

    fn outer() -> Result<(), Error<u32>> {
        inner().annotate("outer step")?;
        Ok(())
    }

    let err = outer().unwrap_err();
    assert_eq!(err.context(), "outer step");
    assert_eq!(err.source, 77u32);
}

#[test]
fn question_mark_passes_through_ok() {
    fn inner() -> Result<u32, u32> {
        Ok(42)
    }

    fn outer() -> Result<u32, Error<u32>> {
        let v = inner().annotate("outer step")?;
        Ok(v)
    }

    assert_eq!(outer().unwrap(), 42);
}

// ─────────────────────────────────────────────────────────────────────────────
// Embedded-style error pattern
//
// Simulates a realistic embedded use case: a flat enum error type, no heap,
// no std, static annotations at call sites, and a match on the source field
// at the handler site.
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn embedded_pattern_context_is_correct_at_each_layer() {
    let err = init_device().unwrap_err();
    assert_eq!(err.context(), "device initialisation");
    assert_eq!(err.source.context(), "reading calibration register 0x42");
    assert_eq!(err.source.source, HalError::Timeout);
}

#[test]
fn embedded_pattern_match_on_source_without_downcast() {
    let err = init_device().unwrap_err();

    let root: HalError = err.into_source().into_source();
    match root {
        HalError::Timeout        => { /* handled */ }
        HalError::BusError       => panic!("unexpected bus error"),
        HalError::InvalidAddress => panic!("unexpected invalid address"),
    }
}

#[test]
fn embedded_pattern_display_no_heap() {
    let err = init_device().unwrap_err();
    let mut buf = WriteBuf::<256>::new();
    write!(buf, "{err}").unwrap();
    let s = buf.as_str();

    assert!(s.contains("device initialisation"), "{s}");
    assert!(s.contains("reading calibration register 0x42"), "{s}");
    assert!(s.contains("hardware timeout"), "{s}");
}

#[test]
fn embedded_pattern_clone_and_eq_on_hal_error() {
    let err = calibrate_sensor().unwrap_err();
    let cloned = err.clone();
    assert_eq!(err, cloned);
}