csvpp/parser/ast_lexer/
unknown_token.rs1use crate::error::{BadInput, ParseError};
2use crate::ArcSourceCode;
3use csvp::{Field, SourcePosition};
4use std::fmt;
5
6#[derive(Debug)]
7pub(crate) struct UnknownToken {
8 pub(crate) bad_input: String,
9 pub(crate) position: SourcePosition,
10 pub(crate) source_code: ArcSourceCode,
11 pub(crate) field: Option<Field>,
12}
13
14impl fmt::Display for UnknownToken {
15 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16 let mut shortened_bad_input = self.bad_input.clone();
17 shortened_bad_input.truncate(50);
18 write!(f, "{shortened_bad_input}")
19 }
20}
21
22impl BadInput for UnknownToken {
23 fn position(&self) -> SourcePosition {
25 if let Some(field) = self.field.clone() {
26 if let Some(position) = field.position_for_offset(self.position.line_offset) {
27 return position;
28 }
29 }
30
31 self.position
32 }
33
34 fn into_parse_error<S: Into<String>>(self, message: S) -> ParseError {
35 self.source_code.parse_error(&self, message)
36 }
37}
38
39impl From<UnknownToken> for ParseError {
40 fn from(u: UnknownToken) -> Self {
41 u.into_parse_error("Error parsing input - invalid token")
42 }
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48 use crate::test_utils::*;
49
50 #[test]
51 fn display() {
52 let ut = UnknownToken {
53 bad_input: "foo".to_string(),
54 position: (1, 10).into(),
55 field: None,
56 source_code: build_source_code(),
57 };
58
59 assert_eq!(ut.to_string(), "foo");
60 }
61
62 #[test]
63 fn display_long() {
64 let ut = UnknownToken {
65 bad_input: "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890".to_string(),
66 position: (1, 10).into(),
67 field: None,
68 source_code: build_source_code(),
69 };
70
71 assert_eq!(
72 ut.to_string(),
73 "12345678901234567890123456789012345678901234567890"
74 );
75 }
76}