ssh_cfg/
config_error.rs

1use std::fmt;
2
3use crate::SshOptionKey;
4
5/// Errors when parsing SSH config file.
6#[derive(Debug)]
7pub enum ConfigError {
8    /// An SSH option is provided before the `Host` key.
9    SshOptionBeforeHost {
10        /// The SSH option.
11        option: SshOptionKey,
12        /// Value provided to the key.
13        value: String,
14    },
15    /// Unknown SSH option in the SSH configuration file.
16    SshOptionUnknown {
17        /// The configuration option key.
18        key: String,
19    },
20    /// Line could not be parsed into a key and value pair.
21    KeyValueNotFound {
22        /// The line that could not be parsed.
23        line: String,
24    },
25}
26
27impl fmt::Display for ConfigError {
28    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29        match self {
30            Self::SshOptionBeforeHost { option, .. } => {
31                write!(
32                    f,
33                    "SSH option `{}` provided before `Host` is specified.",
34                    option,
35                )
36            }
37            Self::SshOptionUnknown { key } => {
38                write!(f, "Unknown SSH configuration option `{}`.", key)
39            }
40            Self::KeyValueNotFound { line } => write!(
41                f,
42                "Could not determine key / value for this line: `{}`.\n\
43                    Key / value pairs must be separated by ` ` or `=`.",
44                line
45            ),
46        }
47    }
48}
49
50impl std::error::Error for ConfigError {
51    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
52        match self {
53            Self::SshOptionBeforeHost { .. } => None,
54            Self::SshOptionUnknown { .. } => None,
55            Self::KeyValueNotFound { .. } => None,
56        }
57    }
58}