nimbus_fml/editing/
error_kind.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::collections::BTreeSet;

use serde_json::{Map, Value};

use crate::intermediate_representation::{PropDef, TypeRef};

pub(crate) enum ErrorKind {
    InvalidKey {
        key_type: TypeRef,
        in_use: BTreeSet<String>,
    },
    InvalidPropKey {
        valid: BTreeSet<String>,
        in_use: BTreeSet<String>,
    },
    InvalidValue {
        value_type: TypeRef,
    },
    InvalidNestedValue {
        prop_name: String,
        prop_type: TypeRef,
    },
    TypeMismatch {
        value_type: TypeRef,
    },
}

impl ErrorKind {
    pub(crate) fn invalid_key(type_ref: &TypeRef, map: &Map<String, Value>) -> Self {
        let keys = map.keys().cloned().collect();
        Self::InvalidKey {
            key_type: type_ref.clone(),
            in_use: keys,
        }
    }

    pub(crate) fn invalid_value(type_ref: &TypeRef) -> Self {
        Self::InvalidValue {
            value_type: type_ref.clone(),
        }
    }

    pub(crate) fn invalid_nested_value(prop_name: &str, type_ref: &TypeRef) -> Self {
        Self::InvalidNestedValue {
            prop_name: prop_name.to_owned(),
            prop_type: type_ref.clone(),
        }
    }

    pub(crate) fn invalid_prop(props: &[PropDef], map: &Map<String, Value>) -> Self {
        let keys = map.keys().cloned().collect();
        let props = props.iter().map(|p| p.name.clone()).collect();
        Self::InvalidPropKey {
            valid: props,
            in_use: keys,
        }
    }

    pub(crate) fn type_mismatch(type_ref: &TypeRef) -> Self {
        Self::TypeMismatch {
            value_type: type_ref.clone(),
        }
    }
}

impl ErrorKind {
    pub(crate) fn message(&self, token: &str) -> String {
        match self {
            Self::InvalidKey { key_type: t, .. } => match t {
                TypeRef::String => format!("Invalid key {token}"),
                _ => format!("Invalid key {token} for type {t}"),
            },
            Self::InvalidPropKey { .. } => format!("Invalid property {token}"),
            Self::InvalidValue { value_type: t } => format!("Invalid value {token} for type {t}"),
            Self::InvalidNestedValue {
                prop_name,
                prop_type: t,
            } => {
                format!("A valid value for {prop_name} of type {t} is missing")
            }
            Self::TypeMismatch { value_type: t } => format!("Invalid value {token} for type {t}"),
        }
    }
}