configmodel/
error.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8use std::ffi::CString;
9use std::fmt;
10use std::io;
11use std::path::PathBuf;
12use std::str;
13
14use thiserror::Error;
15
16/// The error type for parsing config files.
17#[derive(Error, Debug)]
18pub enum Error {
19    /// Unable to convert to a type.
20    #[error("{0}")]
21    Convert(String),
22
23    /// Unable to parse a file due to syntax.
24    #[error("{0:?}:\n{1}")]
25    ParseFile(PathBuf, String),
26
27    /// Unable to parse a flag due to syntax.
28    #[error("malformed --config option: '{0}' (use --config section.name=value)")]
29    ParseFlag(String),
30
31    /// Unable to read a file due to IO errors.
32    #[error("{0:?}: {1}")]
33    Io(PathBuf, #[source] io::Error),
34
35    /// Config file contains invalid UTF-8.
36    #[error("{0:?}: {1}")]
37    Utf8(PathBuf, #[source] str::Utf8Error),
38
39    #[error("{0:?}: {1}")]
40    Utf8Path(CString, #[source] str::Utf8Error),
41
42    #[error(transparent)]
43    ParseInt(#[from] std::num::ParseIntError),
44
45    #[error(transparent)]
46    ParseFloat(#[from] std::num::ParseFloatError),
47
48    #[error("{0}")]
49    General(String),
50
51    #[error("config {0}.{1} is not set")]
52    NotSet(String, String),
53
54    #[error("{0}")]
55    Other(#[source] anyhow::Error),
56}
57
58impl From<String> for Error {
59    fn from(s: String) -> Self {
60        Self::General(s)
61    }
62}
63
64#[derive(Error, Debug)]
65pub struct Errors(pub Vec<Error>);
66
67impl fmt::Display for Errors {
68    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69        for error in self.0.iter() {
70            write!(f, "{}\n", error)?;
71        }
72        Ok(())
73    }
74}