Skip to main content

query_algebrizer_traits/
errors.rs

1// Copyright 2016-2018 Mozilla
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4// this file except in compliance with the License. You may obtain a copy of the
5// License at http://www.apache.org/licenses/LICENSE-2.0
6// Unless required by applicable law or agreed to in writing, software distributed
7// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8// CONDITIONS OF ANY KIND, either express or implied. See the License for the
9// specific language governing permissions and limitations under the License.
10
11use std;
12
13use core_traits::{ValueType, ValueTypeSet};
14
15use edn::{query::PlainSymbol, ParseError};
16
17use thiserror::Error;
18
19pub type Result<T> = std::result::Result<T, AlgebrizerError>;
20
21#[derive(Clone, Debug, Eq, PartialEq)]
22pub enum BindingError {
23    NoBoundVariable,
24    UnexpectedBinding,
25    RepeatedBoundVariable, // TODO: include repeated variable(s).
26
27    /// Expected `[[?x ?y]]` but got some other type of binding.  Mentat is deliberately more strict
28    /// than Datomic: we won't try to make sense of non-obvious (and potentially erroneous) bindings.
29    ExpectedBindRel,
30
31    /// Expected `[[?x ?y]]` or `[?x ...]` but got some other type of binding.  Mentat is
32    /// deliberately stricter than Datomic: we won't try to make sense of non-obvious (and
33    /// potentially erroneous) bindings.
34    ExpectedBindRelOrBindColl,
35
36    /// Expected `[?x1 … ?xN]` or `[[?x1 … ?xN]]` but got some other number of bindings.  Mentat is
37    /// deliberately stricter than Datomic: we prefer placeholders to omission.
38    InvalidNumberOfBindings {
39        number: usize,
40        expected: usize,
41    },
42}
43
44#[derive(Clone, Debug, Eq, Error, PartialEq)]
45pub enum AlgebrizerError {
46    #[error("{0} var {1} is duplicated")]
47    DuplicateVariableError(PlainSymbol, &'static str),
48
49    #[error("unexpected FnArg")]
50    UnsupportedArgument,
51
52    #[error("value of type {0} provided for var {1}, expected {2}")]
53    InputTypeDisagreement(PlainSymbol, ValueType, ValueType),
54
55    #[error("invalid number of arguments to {0}: expected {2}, got {1}.")]
56    InvalidNumberOfArguments(PlainSymbol, usize, usize),
57
58    #[error("invalid argument to {0}: expected {1} in position {2}.")]
59    InvalidArgument(PlainSymbol, &'static str, usize),
60
61    #[error("invalid argument to {0}: expected one of {1:?} in position {2}.")]
62    InvalidArgumentType(PlainSymbol, ValueTypeSet, usize),
63
64    // TODO: flesh this out.
65    #[error("invalid expression in ground constant")]
66    InvalidGroundConstant,
67
68    #[error("invalid limit {0} of type {1}: expected natural number.")]
69    InvalidLimit(String, ValueType),
70
71    #[error("mismatched bindings in ground")]
72    GroundBindingsMismatch,
73
74    #[error("no entid found for ident: {0}")]
75    UnrecognizedIdent(String),
76
77    #[error("no function named {0}")]
78    UnknownFunction(PlainSymbol),
79
80    #[error(":limit var {0} not present in :in")]
81    UnknownLimitVar(PlainSymbol),
82
83    #[error("unbound variable {0} in order clause or function call")]
84    UnboundVariable(PlainSymbol),
85
86    // TODO: flesh out.
87    #[error("non-matching variables in 'or' clause")]
88    NonMatchingVariablesInOrClause,
89
90    #[error("non-matching variables in 'not' clause")]
91    NonMatchingVariablesInNotClause,
92
93    #[error("binding error in {0}: {1:?}")]
94    InvalidBinding(PlainSymbol, BindingError),
95
96    #[error("no rule named {0}")]
97    UnknownRule(PlainSymbol),
98
99    #[error("rule {0} invoked with {1} args but its definition takes {2}")]
100    RuleArgumentMismatch(PlainSymbol, usize, usize),
101
102    #[error("recursive rule {0} is not yet supported in the embedded engine")]
103    RecursiveRuleUnsupported(PlainSymbol),
104
105    #[error("multi-clause rule {0} is not yet supported in the embedded engine")]
106    MultiClauseRuleUnsupported(PlainSymbol),
107
108    #[error("bigint constants are not yet supported in this position (#280)")]
109    UnsupportedBigInteger,
110
111    #[error(
112        "queries against a non-default source (`{0}`) are not yet supported by the SQLite engine"
113    )]
114    UnsupportedSource(String),
115
116    #[error("{0}")]
117    EdnParseError(ParseError), // TODO [#from] ?
118}
119
120impl core::convert::From<ParseError> for AlgebrizerError {
121    fn from(error: ParseError) -> AlgebrizerError {
122        AlgebrizerError::EdnParseError(error)
123    }
124}