documented_toml/error.rs
1//! Failures that stop a merge before it starts.
2
3use toml_edit::TomlError;
4
5/// A document that could not be parsed. Which one is part of the error, because
6/// a broken default document is the application's fault and a broken user
7/// document is the person's.
8#[derive(Debug)]
9pub enum Error {
10 /// The shipped default document failed to parse.
11 DefaultParse {
12 /// The underlying parse failure.
13 source: TomlError,
14 },
15 /// The user's document failed to parse.
16 UserParse {
17 /// The underlying parse failure.
18 source: TomlError,
19 },
20 /// The defaults parsed and hold text, but declare no keys and document
21 /// none either.
22 ///
23 /// A defaults document made only of the maintainers' own notes has nothing
24 /// to merge into and nothing to say. That is an application bug, so it
25 /// fails here instead of becoming a state the merge has to carry. A
26 /// document of marker lines alone is not this: an option anchored on a key
27 /// the application ships no value for is documented without declaring
28 /// anything, and a defaults file of nothing but such blocks is a whole
29 /// configuration. A zero-byte defaults document is a separate case and is
30 /// allowed, see [`SourceDocument::Empty`](crate::SourceDocument::Empty).
31 DefaultsDeclareNoKeys,
32 /// A rename rule was given a path that is not a TOML key path.
33 MigrationPath {
34 /// The underlying parse failure.
35 source: TomlError,
36 },
37 /// [`Merged::set`](crate::Merged::set) was given a path that is not a
38 /// TOML key path.
39 SetPath {
40 /// The underlying parse failure.
41 source: TomlError,
42 },
43 /// [`Merged::set`](crate::Merged::set) was given a path where a table or
44 /// an array of tables already sits. Writing a value there would throw
45 /// away everything under it.
46 NotAValue {
47 /// The path as it was given.
48 path: String,
49 },
50}
51
52impl std::fmt::Display for Error {
53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 match self {
55 Self::DefaultParse { source } => {
56 write!(f, "the default document does not parse: {source}")
57 }
58 Self::UserParse { source } => write!(f, "the user document does not parse: {source}"),
59 Self::DefaultsDeclareNoKeys => f.write_str("the default document declares no keys"),
60 Self::MigrationPath { source } => {
61 write!(f, "a rename rule has an unreadable path: {source}")
62 }
63 Self::SetPath { source } => write!(f, "unreadable path: {source}"),
64 Self::NotAValue { path } => {
65 write!(f, "{path} holds a table, so no value can be set there")
66 }
67 }
68 }
69}
70
71impl std::error::Error for Error {
72 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
73 match self {
74 Self::DefaultParse { source }
75 | Self::UserParse { source }
76 | Self::MigrationPath { source }
77 | Self::SetPath { source } => Some(source),
78 Self::DefaultsDeclareNoKeys | Self::NotAValue { .. } => None,
79 }
80 }
81}