biscuit_parser/
error.rs

1/*
2 * Copyright (c) 2019 Geoffroy Couprie <contact@geoffroycouprie.com> and Contributors to the Eclipse Foundation.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5use thiserror::Error;
6
7#[derive(Error, Clone, Debug, PartialEq, Eq)]
8#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
9pub enum LanguageError {
10    #[error("datalog parsing error: {0:?}")]
11    ParseError(ParseErrors),
12    #[error("datalog parameters must all be bound, provided values must all be used.\nMissing parameters: {missing_parameters:?}\nUnused parameters: {unused_parameters:?}")]
13    Parameters {
14        missing_parameters: Vec<String>,
15        unused_parameters: Vec<String>,
16    },
17}
18
19#[derive(Clone, Debug, PartialEq, Eq)]
20#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
21pub struct ParseErrors {
22    pub errors: Vec<ParseError>,
23}
24
25#[derive(Clone, Debug, PartialEq, Eq)]
26#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
27pub struct ParseError {
28    pub input: String,
29    pub message: Option<String>,
30}
31
32impl<'a> From<crate::parser::Error<'a>> for ParseError {
33    fn from(e: crate::parser::Error<'a>) -> Self {
34        ParseError {
35            input: e.input.to_string(),
36            message: e.message,
37        }
38    }
39}
40
41impl<'a> From<crate::parser::Error<'a>> for ParseErrors {
42    fn from(error: crate::parser::Error<'a>) -> Self {
43        ParseErrors {
44            errors: vec![error.into()],
45        }
46    }
47}
48
49impl<'a> From<Vec<crate::parser::Error<'a>>> for ParseErrors {
50    fn from(errors: Vec<crate::parser::Error<'a>>) -> Self {
51        ParseErrors {
52            errors: errors.into_iter().map(|e| e.into()).collect(),
53        }
54    }
55}
56
57impl<'a> From<crate::parser::Error<'a>> for LanguageError {
58    fn from(e: crate::parser::Error<'a>) -> Self {
59        LanguageError::ParseError(e.into())
60    }
61}
62
63impl<'a> From<Vec<crate::parser::Error<'a>>> for LanguageError {
64    fn from(e: Vec<crate::parser::Error<'a>>) -> Self {
65        LanguageError::ParseError(e.into())
66    }
67}