1use serde::{de, ser};
2use std::fmt;
3
4pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8 #[error(transparent)]
9 TokenizeFailed(#[from] TokenizeFailed),
10
11 #[error("Extra input string remains behind: {0}")]
12 ExtraInputRemaining(String),
13
14 #[error("Error while deserialize STEP struct: {0}")]
15 DeserializeFailed(String),
16
17 #[error("Lookup failed for #{0}")]
18 UnknownEntity(u64),
19
20 #[error("Entity ID #{0} is duplicated")]
21 DuplicatedEntity(u64),
22
23 #[error("Entity '{entity_name}' is not a member of the schema '{schema}'")]
24 UnknownEntityName { entity_name: String, schema: String },
25}
26
27impl de::Error for Error {
28 fn custom<T>(msg: T) -> Self
29 where
30 T: fmt::Display,
31 {
32 Error::DeserializeFailed(msg.to_string())
33 }
34}
35
36impl ser::Error for Error {
37 fn custom<T>(msg: T) -> Self
38 where
39 T: fmt::Display,
40 {
41 Error::DeserializeFailed(msg.to_string())
42 }
43}
44
45pub struct TokenizeFailed {
47 rendered_error: String,
48}
49
50impl fmt::Debug for TokenizeFailed {
51 fn fmt(
52 &self,
53 f: &mut fmt::Formatter<'_>,
54 ) -> std::result::Result<(), fmt::Error> {
55 write!(
56 f,
57 "Error while tokenizing STEP input\n{}",
58 self.rendered_error
59 )?;
60 Ok(())
61 }
62}
63
64impl fmt::Display for TokenizeFailed {
66 fn fmt(
67 &self,
68 f: &mut fmt::Formatter<'_>,
69 ) -> std::result::Result<(), fmt::Error> {
70 fmt::Debug::fmt(self, f)
71 }
72}
73
74impl std::error::Error for TokenizeFailed {}
75
76impl TokenizeFailed {
77 pub fn new(
78 input: &str,
79 err: nom_language::error::VerboseError<&str>,
80 ) -> Self {
81 TokenizeFailed {
82 rendered_error: nom_language::error::convert_error(input, err),
83 }
84 }
85}