falcon_cli/error.rs
1// Copyright 2025 Aquila Labs of Alberta, Canada <matt@cicero.sh>
2// Licensed under either the Apache License, Version 2.0 OR the MIT License, at your option.
3// You may not use this file except in compliance with one of the Licenses.
4// Apache License text: https://www.apache.org/licenses/LICENSE-2.0
5// MIT License text: https://opensource.org/licenses/MIT
6
7use std::fmt;
8
9/// Error types that can occur during CLI command processing.
10///
11/// This enum represents all possible errors that can be returned by CLI commands,
12/// including missing parameters, invalid flags, and generic errors.
13#[derive(Debug)]
14pub enum CliError {
15 /// Required parameters were not provided or are invalid.
16 MissingParams,
17 /// A required flag was not provided.
18 MissingFlag(String),
19 /// A parameter at a specific position failed validation.
20 /// Contains the position (0-indexed) and an error message describing the issue.
21 InvalidParam(usize, String),
22 /// A generic error with a custom message.
23 Generic(String),
24}
25
26impl std::error::Error for CliError {}
27
28impl fmt::Display for CliError {
29 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30 match self {
31 CliError::MissingParams => write!(f, "Missing / invalid parameters.."),
32 CliError::MissingFlag(flag) => write!(f, "Missing required flag, {}", flag),
33 CliError::InvalidParam(pos, msg) => {
34 write!(f, "Invalid parameter at position {}: {}", pos, msg)
35 }
36 CliError::Generic(msg) => write!(f, "{}", msg),
37 }
38 }
39}
40
41impl From<std::io::Error> for CliError {
42 fn from(err: std::io::Error) -> Self {
43 CliError::Generic(err.to_string())
44 }
45}