envision 0.10.1

A ratatui framework for collaborative TUI development with headless testing support
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
//! Error types for the Envision framework.
//!
//! This module provides structured error types for different failure modes
//! in Envision applications. For user-defined async errors that don't fit
//! these categories, [`BoxedError`] remains available.
//!
//! # Example
//!
//! ```rust
//! use envision::error::EnvisionError;
//!
//! fn setup() -> Result<(), EnvisionError> {
//!     // IO errors convert automatically
//!     let _file = std::fs::read_to_string("config.toml")
//!         .map_err(EnvisionError::from)?;
//!     Ok(())
//! }
//! ```

use std::fmt;

/// A boxed error type for user-defined async errors.
///
/// This type alias is provided for ergonomic use in async command handlers
/// where the specific error type is not known at compile time.
pub type BoxedError = Box<dyn std::error::Error + Send + Sync + 'static>;

/// Structured error type for the Envision framework.
///
/// Represents the different categories of errors that can occur when
/// using Envision. Each variant provides structured context about the
/// failure mode, enabling callers to match on specific fields.
///
/// # Example
///
/// ```rust
/// use envision::error::EnvisionError;
///
/// let err = EnvisionError::config("theme", "invalid theme name");
/// assert_eq!(
///     err.to_string(),
///     "configuration error: field `theme`: invalid theme name"
/// );
/// ```
#[derive(Debug)]
pub enum EnvisionError {
    /// An I/O error occurred (terminal, file system, etc.).
    Io(std::io::Error),

    /// A rendering error occurred.
    Render {
        /// The component that failed to render.
        component: &'static str,
        /// Details about the rendering failure.
        detail: String,
    },

    /// A configuration error occurred.
    Config {
        /// The configuration field that caused the error.
        field: String,
        /// The reason the configuration is invalid.
        reason: String,
    },

    /// A subscription error occurred.
    Subscription {
        /// The type of subscription that failed.
        subscription_type: &'static str,
        /// Details about the subscription failure.
        detail: String,
    },

    /// A catch-all variant for wrapping arbitrary errors.
    ///
    /// Use this when an error does not fit into the other structured
    /// categories. It wraps any error that implements
    /// `std::error::Error + Send + Sync + 'static`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::error::EnvisionError;
    ///
    /// let err = EnvisionError::other("something went wrong");
    /// assert_eq!(err.to_string(), "other error: something went wrong");
    /// ```
    Other(BoxedError),
}

impl EnvisionError {
    /// Creates a rendering error.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::error::EnvisionError;
    ///
    /// let err = EnvisionError::render("ProgressBar", "width must be positive");
    /// assert_eq!(
    ///     err.to_string(),
    ///     "render error: component `ProgressBar`: width must be positive"
    /// );
    /// ```
    pub fn render(component: &'static str, detail: impl Into<String>) -> Self {
        EnvisionError::Render {
            component,
            detail: detail.into(),
        }
    }

    /// Creates a configuration error.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::error::EnvisionError;
    ///
    /// let err = EnvisionError::config("theme", "unknown theme name");
    /// assert_eq!(
    ///     err.to_string(),
    ///     "configuration error: field `theme`: unknown theme name"
    /// );
    /// ```
    pub fn config(field: impl Into<String>, reason: impl Into<String>) -> Self {
        EnvisionError::Config {
            field: field.into(),
            reason: reason.into(),
        }
    }

    /// Creates a subscription error.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::error::EnvisionError;
    ///
    /// let err = EnvisionError::subscription("tick", "interval too small");
    /// assert_eq!(
    ///     err.to_string(),
    ///     "subscription error: type `tick`: interval too small"
    /// );
    /// ```
    pub fn subscription(subscription_type: &'static str, detail: impl Into<String>) -> Self {
        EnvisionError::Subscription {
            subscription_type,
            detail: detail.into(),
        }
    }

    /// Creates an `Other` error from any error type.
    ///
    /// This is a convenience constructor that wraps an arbitrary error
    /// into the [`Other`](EnvisionError::Other) variant, avoiding the
    /// need for manual `.map_err()` calls.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::error::EnvisionError;
    ///
    /// let err = EnvisionError::other("something went wrong");
    /// assert_eq!(err.to_string(), "other error: something went wrong");
    ///
    /// // Works with any error type
    /// let io_err = std::io::Error::other("disk full");
    /// let err = EnvisionError::other(io_err);
    /// assert!(err.to_string().contains("disk full"));
    /// ```
    pub fn other(err: impl Into<BoxedError>) -> Self {
        EnvisionError::Other(err.into())
    }
}

impl fmt::Display for EnvisionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            EnvisionError::Io(err) => write!(f, "I/O error: {}", err),
            EnvisionError::Render { component, detail } => {
                write!(f, "render error: component `{}`: {}", component, detail)
            }
            EnvisionError::Config { field, reason } => {
                write!(f, "configuration error: field `{}`: {}", field, reason)
            }
            EnvisionError::Subscription {
                subscription_type,
                detail,
            } => {
                write!(
                    f,
                    "subscription error: type `{}`: {}",
                    subscription_type, detail
                )
            }
            EnvisionError::Other(err) => write!(f, "other error: {}", err),
        }
    }
}

impl std::error::Error for EnvisionError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            EnvisionError::Io(err) => Some(err),
            EnvisionError::Other(err) => Some(err.as_ref()),
            EnvisionError::Render { .. }
            | EnvisionError::Config { .. }
            | EnvisionError::Subscription { .. } => None,
        }
    }
}

impl From<std::io::Error> for EnvisionError {
    fn from(err: std::io::Error) -> Self {
        EnvisionError::Io(err)
    }
}

/// A [`Result`] type alias using [`EnvisionError`].
///
/// This is the standard result type returned by most Envision operations
/// (runtime construction, rendering, ticking, etc.).
///
/// # Example
///
/// ```rust
/// use envision::error::Result;
///
/// fn setup() -> Result<()> {
///     // io::Error converts automatically via From
///     Ok(())
/// }
/// ```
pub type Result<T> = std::result::Result<T, EnvisionError>;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn io_error_display() {
        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
        let err = EnvisionError::from(io_err);
        assert_eq!(err.to_string(), "I/O error: file not found");
    }

    #[test]
    fn render_error_display() {
        let err = EnvisionError::render("ProgressBar", "failed to draw widget");
        assert_eq!(
            err.to_string(),
            "render error: component `ProgressBar`: failed to draw widget"
        );
    }

    #[test]
    fn config_error_display() {
        let err = EnvisionError::config("theme", "invalid theme name");
        assert_eq!(
            err.to_string(),
            "configuration error: field `theme`: invalid theme name"
        );
    }

    #[test]
    fn subscription_error_display() {
        let err = EnvisionError::subscription("tick", "interval too small");
        assert_eq!(
            err.to_string(),
            "subscription error: type `tick`: interval too small"
        );
    }

    #[test]
    fn io_error_from_conversion() {
        let io_err = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "access denied");
        let err: EnvisionError = io_err.into();
        assert!(matches!(err, EnvisionError::Io(_)));
    }

    #[test]
    fn io_error_source() {
        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "not found");
        let err = EnvisionError::from(io_err);
        assert!(std::error::Error::source(&err).is_some());
    }

    #[test]
    fn render_error_no_source() {
        let err = EnvisionError::render("Widget", "bad render");
        assert!(std::error::Error::source(&err).is_none());
    }

    #[test]
    fn config_error_no_source() {
        let err = EnvisionError::config("key", "bad config");
        assert!(std::error::Error::source(&err).is_none());
    }

    #[test]
    fn subscription_error_no_source() {
        let err = EnvisionError::subscription("tick", "bad sub");
        assert!(std::error::Error::source(&err).is_none());
    }

    #[test]
    fn debug_format() {
        let err = EnvisionError::config("key", "test");
        let debug = format!("{:?}", err);
        assert!(debug.contains("Config"));
        assert!(debug.contains("test"));
    }

    #[test]
    fn boxed_error_alias_works() {
        fn returns_boxed() -> std::result::Result<(), BoxedError> {
            Err("test error".into())
        }
        assert!(returns_boxed().is_err());
    }

    #[test]
    fn render_error_fields_accessible() {
        let err = EnvisionError::render("Table", "column overflow");
        match err {
            EnvisionError::Render { component, detail } => {
                assert_eq!(component, "Table");
                assert_eq!(detail, "column overflow");
            }
            _ => panic!("expected Render variant"),
        }
    }

    #[test]
    fn config_error_fields_accessible() {
        let err = EnvisionError::config("tick_rate", "must be positive");
        match err {
            EnvisionError::Config { field, reason } => {
                assert_eq!(field, "tick_rate");
                assert_eq!(reason, "must be positive");
            }
            _ => panic!("expected Config variant"),
        }
    }

    #[test]
    fn subscription_error_fields_accessible() {
        let err = EnvisionError::subscription("interval", "already running");
        match err {
            EnvisionError::Subscription {
                subscription_type,
                detail,
            } => {
                assert_eq!(subscription_type, "interval");
                assert_eq!(detail, "already running");
            }
            _ => panic!("expected Subscription variant"),
        }
    }

    #[test]
    fn other_from_string_error() {
        let err = EnvisionError::Other("something went wrong".into());
        assert!(matches!(err, EnvisionError::Other(_)));
    }

    #[test]
    fn other_from_custom_error_type() {
        #[derive(Debug)]
        struct CustomError;

        impl fmt::Display for CustomError {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "custom error occurred")
            }
        }

        impl std::error::Error for CustomError {}

        let err = EnvisionError::Other(Box::new(CustomError));
        assert!(matches!(err, EnvisionError::Other(_)));
        assert_eq!(err.to_string(), "other error: custom error occurred");
    }

    #[test]
    fn other_error_display() {
        let err = EnvisionError::Other("unexpected failure".into());
        assert_eq!(err.to_string(), "other error: unexpected failure");
    }

    #[test]
    fn other_error_source() {
        let io_err = std::io::Error::other("inner error");
        let err = EnvisionError::Other(Box::new(io_err));
        let source = std::error::Error::source(&err);
        assert!(source.is_some());
        assert_eq!(source.unwrap().to_string(), "inner error");
    }

    #[test]
    fn other_convenience_constructor() {
        let err = EnvisionError::other("convenience test");
        assert!(matches!(err, EnvisionError::Other(_)));
        assert_eq!(err.to_string(), "other error: convenience test");
    }

    #[test]
    fn other_convenience_constructor_with_io_error() {
        let io_err = std::io::Error::other("disk full");
        let err = EnvisionError::other(io_err);
        assert!(matches!(err, EnvisionError::Other(_)));
        assert!(err.to_string().contains("disk full"));
    }
}