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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2016 Mozilla
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

extern crate mentat_query;

use mentat_core::{
    EdnParseError,
    ValueType,
    ValueTypeSet,
};

use self::mentat_query::{
    PlainSymbol,
};

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BindingError {
    NoBoundVariable,
    UnexpectedBinding,
    RepeatedBoundVariable, // TODO: include repeated variable(s).

    /// Expected `[[?x ?y]]` but got some other type of binding.  Mentat is deliberately more strict
    /// than Datomic: we won't try to make sense of non-obvious (and potentially erroneous) bindings.
    ExpectedBindRel,

    /// Expected `[[?x ?y]]` or `[?x ...]` but got some other type of binding.  Mentat is
    /// deliberately more strict than Datomic: we won't try to make sense of non-obvious (and
    /// potentially erroneous) bindings.
    ExpectedBindRelOrBindColl,

    /// Expected `[?x1 … ?xN]` or `[[?x1 … ?xN]]` but got some other number of bindings.  Mentat is
    /// deliberately more strict than Datomic: we prefer placeholders to omission.
    InvalidNumberOfBindings { number: usize, expected: usize },
}

error_chain! {
    types {
        Error, ErrorKind, ResultExt, Result;
    }

    foreign_links {
        EdnParseError(EdnParseError);
    }

    errors {
        UnsupportedArgument {
            description("unexpected FnArg")
            display("unexpected FnArg")
        }

        InputTypeDisagreement(var: PlainSymbol, declared: ValueType, provided: ValueType) {
            description("input type disagreement")
            display("value of type {} provided for var {}, expected {}", provided, var, declared)
        }

        UnrecognizedIdent(ident: String) {
            description("no entid found for ident")
            display("no entid found for ident: {}", ident)
        }

        UnknownFunction(name: PlainSymbol) {
            description("no such function")
            display("no function named {}", name)
        }

        InvalidNumberOfArguments(function: PlainSymbol, number: usize, expected: usize) {
            description("invalid number of arguments")
            display("invalid number of arguments to {}: expected {}, got {}.", function, expected, number)
        }

        UnboundVariable(name: PlainSymbol) {
            description("unbound variable in order clause or function call")
            display("unbound variable: {}", name)
        }

        InvalidBinding(function: PlainSymbol, binding_error: BindingError) {
            description("invalid binding")
            display("invalid binding for {}: {:?}.", function, binding_error)
        }

        GroundBindingsMismatch {
            description("mismatched bindings in ground")
            display("mismatched bindings in ground")
        }

        InvalidGroundConstant {
            // TODO: flesh this out.
            description("invalid expression in ground constant")
            display("invalid expression in ground constant")
        }

        InvalidArgument(function: PlainSymbol, expected: &'static str, position: usize) {
            description("invalid argument")
            display("invalid argument to {}: expected {} in position {}.", function, expected, position)
        }

        InvalidArgumentType(function: PlainSymbol, expected_types: ValueTypeSet, position: usize) {
            description("invalid argument")
            display("invalid argument to {}: expected one of {:?} in position {}.", function, expected_types, position)
        }

        InvalidLimit(val: String, kind: ValueType) {
            description("invalid limit")
            display("invalid limit {} of type {}: expected natural number.", val, kind)
        }

        NonMatchingVariablesInOrClause {
            // TODO: flesh out.
            description("non-matching variables in 'or' clause")
            display("non-matching variables in 'or' clause")
        }

        NonMatchingVariablesInNotClause {
            // TODO: flesh out.
            description("non-matching variables in 'not' clause")
            display("non-matching variables in 'not' clause")
        }

        DuplicateVariableError(name: PlainSymbol, clause: &'static str)  {
            description("duplicate variables")
            display("{} var {} is duplicated", clause, name)
        }

        UnknownLimitVar(name: PlainSymbol) {
            description(":limit var not present in :in")
            display(":limit var {} not present in :in", name)
        }
    }
}