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 workspace panel `panel` has no `table` to bind.
48 MissingTable { step: &'static str, panel: String },
49}
50
51impl fmt::Display for MigrateError {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 match self {
54 Self::InvalidVersion { version } => write!(f, "invalid version `{version}`"),
55 Self::Unsupported { version } => {
56 write!(
57 f,
58 "configs written before 2.0.0 are unsupported (`{version}`)"
59 )
60 },
61 Self::FromFuture { version } => {
62 write!(f, "`{version}` is newer than this migrator")
63 },
64 Self::NotAnObject => write!(f, "expected a JSON object"),
65 Self::Malformed {
66 step,
67 pointer,
68 expected,
69 } => write!(f, "{step}: expected {expected} at `{pointer}`"),
70 Self::Conflict {
71 step,
72 pointer,
73 keys,
74 } => write!(f, "{step}: conflicting keys {keys:?} at `{pointer}`"),
75 Self::MissingTable { step, panel } => {
76 write!(f, "{step}: panel `{panel}` has no `table`")
77 },
78 }
79 }
80}
81
82impl std::error::Error for MigrateError {}