perspective_migrate/error.rs
1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
5// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors. ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use std::fmt;
14
15use serde::Serialize;
16
17/// Why a config could not be migrated to a valid current form.
18#[derive(Clone, Debug, PartialEq, Serialize)]
19#[serde(tag = "kind")]
20pub enum MigrateError {
21 /// `version` is not `x.y.z`.
22 InvalidVersion { version: String },
23
24 /// `version` predates `2.0.0`.
25 Unsupported { version: String },
26
27 /// `version` is newer than this crate's schema.
28 FromFuture { version: String },
29
30 /// The config is not a JSON object.
31 NotAnObject,
32
33 /// The value at `pointer` is not the shape its declared version wrote.
34 Malformed {
35 step: &'static str,
36 pointer: String,
37 expected: &'static str,
38 },
39
40 /// An alias and its canonical key are both present at `pointer`.
41 Conflict {
42 step: &'static str,
43 pointer: String,
44 keys: Vec<String>,
45 },
46
47 /// The panel at `pointer` has no `table` to bind.
48 MissingTable { step: &'static str, pointer: String },
49
50 /// The migrated config does not deserialize into the current schema.
51 Invalid { pointer: String, message: String },
52}
53
54impl fmt::Display for MigrateError {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 match self {
57 Self::InvalidVersion { version } => write!(f, "invalid version `{version}`"),
58 Self::Unsupported { version } => {
59 write!(
60 f,
61 "configs written before 2.0.0 are unsupported (`{version}`)"
62 )
63 },
64 Self::FromFuture { version } => {
65 write!(f, "`{version}` is newer than this migrator")
66 },
67 Self::NotAnObject => write!(f, "expected a JSON object"),
68 Self::Malformed {
69 step,
70 pointer,
71 expected,
72 } => write!(f, "{step}: expected {expected} at `{pointer}`"),
73 Self::Conflict {
74 step,
75 pointer,
76 keys,
77 } => write!(f, "{step}: conflicting keys {keys:?} at `{pointer}`"),
78 Self::MissingTable { step, pointer } => {
79 write!(f, "{step}: panel `{pointer}` has no `table`")
80 },
81 Self::Invalid { pointer, message } => {
82 write!(f, "migrated config is invalid at `{pointer}`: {message}")
83 },
84 }
85 }
86}
87
88impl std::error::Error for MigrateError {}
89
90impl From<crate::ValidateError> for MigrateError {
91 fn from(error: crate::ValidateError) -> Self {
92 Self::Invalid {
93 pointer: error.pointer,
94 message: error.message,
95 }
96 }
97}