tele_tokenizer/
error.rs

1#![warn(missing_docs)]
2//! A custom Error type for tokenization
3
4use serde::Serialize;
5use std::fmt::Display;
6
7pub type Result<T> = std::result::Result<T, Error>;
8
9#[derive(Debug, Serialize, PartialEq)]
10pub struct Error {
11  pub kind: ErrorKind,
12}
13
14impl Display for Error {
15  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16    write!(f, "{}", self.kind.as_str())
17  }
18}
19
20impl From<ErrorKind> for Error {
21  fn from(kind: ErrorKind) -> Self {
22    Error { kind }
23  }
24}
25
26#[derive(Debug, Serialize, PartialEq)]
27pub enum ErrorKind {
28  UnexpectedEOF,
29  InvalidIdentSeq,
30  BadString,
31  BadURL,
32}
33
34impl ErrorKind {
35  pub fn as_str(&self) -> &'static str {
36    match *self {
37      Self::UnexpectedEOF => "Unexpected End Of File",
38      Self::InvalidIdentSeq => "invalid identifier sequence",
39      Self::BadString => "Bad String",
40      Self::BadURL => "Bad URL",
41    }
42  }
43}