1use std::{fmt::Display, iter::Peekable};
2
3use logos::{Lexer, Logos};
4use serde_core::{
5 Deserializer,
6 de::{self, EnumAccess, Expected, MapAccess, SeqAccess, Unexpected, VariantAccess, Visitor},
7};
8use thiserror::Error;
9
10#[derive(Debug, Error)]
11pub enum Error {
12 #[error("invalid type: {0}, expected {1}")]
13 InvalidType(String, String),
14 #[error("invalid value: {0}, expected {1}")]
15 InvalidValue(String, String),
16 #[error("invalid length: {0}, expected {1}")]
17 InvalidLength(usize, String),
18 #[error("unknown variant `{0}`, expected one of {0:?}")]
19 UnknownVariant(String, &'static [&'static str]),
20 #[error("unknown field `{0}`, expected one of {0:?}")]
21 UnknownField(String, &'static [&'static str]),
22 #[error("missing field `{0}`")]
23 MissingField(&'static str),
24 #[error("duplicate field `{0}`")]
25 DuplicateField(&'static str),
26 #[error("Unexpected EOF")]
27 UnexpectedEof,
28 #[error("Map key must be stringable")]
29 KeyMustBeStringable,
30 #[error("{0}")]
31 Custom(String),
32}
33
34impl de::Error for Error {
35 #[cold]
36 fn invalid_type(unexp: Unexpected, exp: &dyn Expected) -> Self {
37 Error::InvalidType(unexp.to_string(), exp.to_string())
38 }
39
40 #[cold]
41 fn invalid_value(unexp: Unexpected, exp: &dyn Expected) -> Self {
42 Error::InvalidValue(unexp.to_string(), exp.to_string())
43 }
44
45 #[cold]
46 fn invalid_length(len: usize, exp: &dyn Expected) -> Self {
47 Error::InvalidLength(len, exp.to_string())
48 }
49
50 #[cold]
51 fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self {
52 Error::UnknownVariant(variant.to_owned(), expected)
53 }
54
55 #[cold]
56 fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self {
57 Error::UnknownField(field.to_owned(), expected)
58 }
59
60 #[cold]
61 fn missing_field(field: &'static str) -> Self {
62 Error::MissingField(field)
63 }
64
65 #[cold]
66 fn duplicate_field(field: &'static str) -> Self {
67 Error::DuplicateField(field)
68 }
69
70 fn custom<T>(msg: T) -> Self
71 where
72 T: Display,
73 {
74 Error::Custom(msg.to_string())
75 }
76}
77
78#[derive(Logos, Debug, PartialEq, Clone, Copy)]
79#[logos(skip(r"((fuckin)?[ \t\r\n\f]+)+"))]
80pub enum Token<'source> {
81 #[token("dont")]
83 Dont,
84 #[regex(r"in[ \t\r\n\f]+(fuckin[ \t\r\n\f]+)?theory")]
85 InTheory,
86
87 #[regex(r"that[ \t\r\n\f]+(fuckin[ \t\r\n\f]+)?shit")]
89 ThatShit,
90 #[regex(r"oh[ \t\r\n\f]+(fuckin[ \t\r\n\f]+)?yeah")]
91 OhYeah,
92
93 #[regex(r"[^ \t\r\n\f]+")]
94 Text(&'source str),
95}
96
97impl<'source> Display for Token<'source> {
98 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99 match *self {
100 Self::ThatShit => write!(f, "that shit"),
101 Self::Dont => write!(f, "dont"),
102 Self::OhYeah => write!(f, "oh yeah"),
103 Self::InTheory => write!(f, "in theory"),
104 Self::Text(text) => write!(f, "{}", text),
105 }
106 }
107}
108
109pub struct TsonLexer<'source>(Lexer<'source, Token<'source>>);
110
111impl<'source> Iterator for TsonLexer<'source> {
112 type Item = Token<'source>;
113
114 #[cfg(not(debug_assertions))]
115 fn next(&mut self) -> Option<Self::Item> {
116 Some(unsafe { self.0.next()?.unwrap_unchecked() })
117 }
118
119 #[cfg(debug_assertions)]
120 fn next(&mut self) -> Option<Self::Item> {
121 Some(self.0.next()?.unwrap())
122 }
123}
124
125impl<'source> TsonLexer<'source> {
126 fn new(source: &'source str) -> TsonLexer<'source> {
127 TsonLexer(Token::lexer(source))
128 }
129}
130
131pub struct TsonDeserializer<'a> {
132 reader: Peekable<TsonLexer<'a>>,
133 prefix_token: Option<Token<'a>>,
134}
135
136impl<'source> TsonDeserializer<'source> {
137 pub fn new(str: &'source str) -> Self {
138 let reader = TsonLexer::new(str).peekable();
139 Self {
140 reader,
141 prefix_token: None,
142 }
143 }
144
145 fn next(&mut self) -> Result<Token<'source>, Error> {
146 self.reader.next().ok_or(Error::UnexpectedEof)
147 }
148
149 fn that_shit(&mut self) -> Result<(), Error> {
150 let next_token = self.next()?;
151 if next_token != Token::ThatShit {
152 return Err(Error::InvalidValue(
153 next_token.to_string(),
154 Token::ThatShit.to_string(),
155 ));
156 }
157 Ok(())
158 }
159
160 fn oh_yeah_or_none(&mut self) -> Result<(), Error> {
161 let next_token = self.reader.next();
162 let Some(next_token) = next_token else {
163 return Ok(());
164 };
165 if next_token != Token::OhYeah {
166 return Err(Error::InvalidValue(
167 next_token.to_string(),
168 Token::OhYeah.to_string(),
169 ));
170 }
171
172 Ok(())
173 }
174
175 fn text(&mut self) -> Result<&'source str, Error> {
176 if self.reader.peek() == Some(&Token::ThatShit) {
177 return Ok("");
178 }
179 let next_token = self.next()?;
180 let Token::Text(text) = next_token else {
181 return Err(Error::InvalidValue(
182 next_token.to_string(),
183 String::from("text"),
184 ));
185 };
186 Ok(text)
187 }
188
189 fn identifier(&mut self) -> Result<&'source str, Error> {
190 let next_token = self.next()?;
191 let text = match next_token {
192 Token::Text(text) => text,
193 Token::Dont | Token::InTheory => {
194 self.prefix_token = Some(next_token);
195 self.text()?
196 }
197 token => {
198 return Err(Error::InvalidValue(
199 token.to_string(),
200 String::from("text, dont, in theory"),
201 ));
202 }
203 };
204
205 Ok(text)
206 }
207}
208
209macro_rules! deserialize_value {
210 ($fn_name:ident, $visitor:ident, $expected:expr) => {
211 fn $fn_name<V>(self, visitor: V) -> Result<V::Value, Self::Error>
212 where
213 V: Visitor<'de>,
214 {
215 let text = self.text()?;
216 self.that_shit()?;
217 visitor.$visitor(
218 text.parse()
219 .map_err(|_| Error::InvalidValue(text.to_string(), String::from($expected)))?,
220 )
221 }
222 };
223}
224
225impl<'de> Deserializer<'de> for &mut TsonDeserializer<'de> {
226 type Error = Error;
227
228 fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
229 where
230 V: Visitor<'de>,
231 {
232 unimplemented!()
233 }
234
235 deserialize_value!(deserialize_i8, visit_i8, "i8");
236 deserialize_value!(deserialize_i16, visit_i16, "i16");
237 deserialize_value!(deserialize_i32, visit_i32, "i32");
238 deserialize_value!(deserialize_i64, visit_i64, "i64");
239 deserialize_value!(deserialize_u8, visit_u8, "u8");
240 deserialize_value!(deserialize_u16, visit_u16, "u16");
241 deserialize_value!(deserialize_u32, visit_u32, "u32");
242 deserialize_value!(deserialize_u64, visit_u64, "u64");
243 deserialize_value!(deserialize_f32, visit_f32, "f32");
244 deserialize_value!(deserialize_f64, visit_f64, "f64");
245 deserialize_value!(deserialize_char, visit_char, "char");
246
247 fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
248 where
249 V: Visitor<'de>,
250 {
251 let result = if self.prefix_token == Some(Token::Dont) {
252 self.prefix_token = None;
253 false
254 } else {
255 true
256 };
257
258 self.that_shit()?;
259 visitor.visit_bool(result)
260 }
261
262 fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
263 where
264 V: Visitor<'de>,
265 {
266 let start_text = self.text()?;
267
268 let start_ptr = start_text.as_ptr();
269 let mut end_ptr = unsafe { start_text.as_ptr().offset(start_text.len() as isize) };
270
271 loop {
272 let next_token = self.next()?;
273 match next_token {
274 Token::Text(text) => {
275 end_ptr = unsafe { text.as_ptr().offset(text.len() as isize) };
276 }
277 Token::ThatShit => break,
278 _ => {}
279 }
280 }
281
282 visitor.visit_borrowed_str(unsafe {
283 std::str::from_utf8_unchecked(std::slice::from_raw_parts(
284 start_ptr,
285 end_ptr as usize - start_ptr as usize,
286 ))
287 })
288 }
289
290 fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
291 where
292 V: Visitor<'de>,
293 {
294 self.deserialize_str(visitor)
295 }
296
297 fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
298 where
299 V: Visitor<'de>,
300 {
301 self.deserialize_seq(visitor)
302 }
303
304 fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
305 where
306 V: Visitor<'de>,
307 {
308 self.deserialize_seq(visitor)
309 }
310
311 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
312 where
313 V: Visitor<'de>,
314 {
315 if self.prefix_token == Some(Token::InTheory) {
316 self.prefix_token = None;
317 self.that_shit()?;
318 visitor.visit_none()
319 } else {
320 visitor.visit_some(self)
321 }
322 }
323
324 fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
325 where
326 V: Visitor<'de>,
327 {
328 let next_token = self.next()?;
329 if next_token != Token::Text("unit") {
330 return Err(Error::InvalidType(
331 next_token.to_string(),
332 String::from("unit"),
333 ));
334 };
335 self.that_shit()?;
336 visitor.visit_unit()
337 }
338
339 fn deserialize_unit_struct<V>(
340 self,
341 name: &'static str,
342 visitor: V,
343 ) -> Result<V::Value, Self::Error>
344 where
345 V: Visitor<'de>,
346 {
347 let next_token = self.next()?;
348 if next_token != Token::Text(name) {
349 return Err(Error::InvalidType(
350 next_token.to_string(),
351 String::from(name),
352 ));
353 };
354 self.that_shit()?;
355 visitor.visit_unit()
356 }
357
358 fn deserialize_newtype_struct<V>(
359 self,
360 _name: &'static str,
361 visitor: V,
362 ) -> Result<V::Value, Self::Error>
363 where
364 V: Visitor<'de>,
365 {
366 visitor.visit_newtype_struct(self)
367 }
368
369 fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
370 where
371 V: Visitor<'de>,
372 {
373 let result = visitor.visit_seq(TsonSeqAccess { deserializer: self })?;
374
375 self.oh_yeah_or_none()?;
376
377 Ok(result)
378 }
379
380 fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
381 where
382 V: Visitor<'de>,
383 {
384 self.deserialize_seq(visitor)
385 }
386
387 fn deserialize_tuple_struct<V>(
388 self,
389 _name: &'static str,
390 _len: usize,
391 visitor: V,
392 ) -> Result<V::Value, Self::Error>
393 where
394 V: Visitor<'de>,
395 {
396 self.deserialize_seq(visitor)
397 }
398
399 fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
400 where
401 V: Visitor<'de>,
402 {
403 let result = visitor.visit_map(TsonMapAccess { deserializer: self })?;
404
405 self.oh_yeah_or_none()?;
406
407 Ok(result)
408 }
409
410 fn deserialize_struct<V>(
411 self,
412 _name: &'static str,
413 _fields: &'static [&'static str],
414 visitor: V,
415 ) -> Result<V::Value, Self::Error>
416 where
417 V: Visitor<'de>,
418 {
419 self.deserialize_map(visitor)
420 }
421
422 fn deserialize_enum<V>(
423 self,
424 _name: &'static str,
425 _variants: &'static [&'static str],
426 visitor: V,
427 ) -> Result<V::Value, Self::Error>
428 where
429 V: Visitor<'de>,
430 {
431 visitor.visit_enum(TsonEnumAccess { deserializer: self })
432 }
433
434 fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
435 where
436 V: Visitor<'de>,
437 {
438 let text = self.identifier()?;
439 visitor.visit_borrowed_str(text)
440 }
441
442 fn deserialize_ignored_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
443 where
444 V: Visitor<'de>,
445 {
446 unimplemented!()
447 }
448}
449
450struct MapAccessDeserializer<'de, 'a> {
451 deserializer: &'a mut TsonDeserializer<'de>,
452}
453
454macro_rules! deserialize_key_value {
455 ($fn_name:ident, $visitor:ident, $expected:expr) => {
456 fn $fn_name<V>(self, visitor: V) -> Result<V::Value, Self::Error>
457 where
458 V: Visitor<'de>,
459 {
460 let text = self.deserializer.identifier()?;
461 visitor.$visitor(
462 text.parse()
463 .map_err(|_| Error::InvalidValue(text.to_string(), String::from($expected)))?,
464 )
465 }
466 };
467}
468
469impl<'de, 'a> Deserializer<'de> for &mut MapAccessDeserializer<'de, 'a> {
470 type Error = Error;
471
472 fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
473 where
474 V: Visitor<'de>,
475 {
476 unimplemented!()
477 }
478
479 deserialize_key_value!(deserialize_i8, visit_i8, "i8");
480 deserialize_key_value!(deserialize_i16, visit_i16, "i16");
481 deserialize_key_value!(deserialize_i32, visit_i32, "i32");
482 deserialize_key_value!(deserialize_i64, visit_i64, "i64");
483 deserialize_key_value!(deserialize_u8, visit_u8, "u8");
484 deserialize_key_value!(deserialize_u16, visit_u16, "u16");
485 deserialize_key_value!(deserialize_u32, visit_u32, "u32");
486 deserialize_key_value!(deserialize_u64, visit_u64, "u64");
487 deserialize_key_value!(deserialize_f32, visit_f32, "f32");
488 deserialize_key_value!(deserialize_f64, visit_f64, "f64");
489 deserialize_key_value!(deserialize_char, visit_char, "char");
490
491 fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
492 where
493 V: Visitor<'de>,
494 {
495 let text = self.deserializer.identifier()?;
496 if text == "true" {
497 visitor.visit_bool(true)
498 } else if text == "false" {
499 visitor.visit_bool(false)
500 } else {
501 Err(Error::InvalidValue(
502 text.to_owned(),
503 String::from("true, false"),
504 ))
505 }
506 }
507
508 fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
509 where
510 V: Visitor<'de>,
511 {
512 let text = self.deserializer.identifier()?;
513 visitor.visit_borrowed_str(text)
514 }
515
516 fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
517 where
518 V: Visitor<'de>,
519 {
520 self.deserialize_str(visitor)
521 }
522
523 fn deserialize_bytes<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
524 where
525 V: Visitor<'de>,
526 {
527 Err(Error::KeyMustBeStringable)
528 }
529
530 fn deserialize_byte_buf<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
531 where
532 V: Visitor<'de>,
533 {
534 Err(Error::KeyMustBeStringable)
535 }
536
537 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
538 where
539 V: Visitor<'de>,
540 {
541 if self.deserializer.reader.peek() == Some(&Token::InTheory) {
542 visitor.visit_none()
543 } else {
544 visitor.visit_some(self)
545 }
546 }
547
548 fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
549 where
550 V: Visitor<'de>,
551 {
552 let next_token = self.deserializer.identifier()?;
553 if next_token != "unit" {
554 return Err(Error::InvalidType(
555 next_token.to_string(),
556 String::from("unit"),
557 ));
558 };
559 visitor.visit_unit()
560 }
561
562 fn deserialize_unit_struct<V>(
563 self,
564 name: &'static str,
565 visitor: V,
566 ) -> Result<V::Value, Self::Error>
567 where
568 V: Visitor<'de>,
569 {
570 let next_token = self.deserializer.identifier()?;
571 if next_token != name {
572 return Err(Error::InvalidType(
573 next_token.to_string(),
574 String::from(name),
575 ));
576 };
577 visitor.visit_unit()
578 }
579
580 fn deserialize_newtype_struct<V>(
581 self,
582 _name: &'static str,
583 visitor: V,
584 ) -> Result<V::Value, Self::Error>
585 where
586 V: Visitor<'de>,
587 {
588 visitor.visit_newtype_struct(self)
589 }
590
591 fn deserialize_seq<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
592 where
593 V: Visitor<'de>,
594 {
595 Err(Error::KeyMustBeStringable)
596 }
597
598 fn deserialize_tuple<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
599 where
600 V: Visitor<'de>,
601 {
602 Err(Error::KeyMustBeStringable)
603 }
604
605 fn deserialize_tuple_struct<V>(
606 self,
607 _name: &'static str,
608 _len: usize,
609 _visitor: V,
610 ) -> Result<V::Value, Self::Error>
611 where
612 V: Visitor<'de>,
613 {
614 Err(Error::KeyMustBeStringable)
615 }
616
617 fn deserialize_map<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
618 where
619 V: Visitor<'de>,
620 {
621 Err(Error::KeyMustBeStringable)
622 }
623
624 fn deserialize_struct<V>(
625 self,
626 _name: &'static str,
627 _fields: &'static [&'static str],
628 _visitor: V,
629 ) -> Result<V::Value, Self::Error>
630 where
631 V: Visitor<'de>,
632 {
633 Err(Error::KeyMustBeStringable)
634 }
635
636 fn deserialize_enum<V>(
637 self,
638 _name: &'static str,
639 _variants: &'static [&'static str],
640 _visitor: V,
641 ) -> Result<V::Value, Self::Error>
642 where
643 V: Visitor<'de>,
644 {
645 Err(Error::KeyMustBeStringable)
646 }
647
648 fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
649 where
650 V: Visitor<'de>,
651 {
652 self.deserializer.deserialize_identifier(visitor)
653 }
654
655 fn deserialize_ignored_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
656 where
657 V: Visitor<'de>,
658 {
659 unimplemented!()
660 }
661}
662
663pub struct TsonMapAccess<'de, 'a> {
664 deserializer: &'a mut TsonDeserializer<'de>,
665}
666
667impl<'de, 'a> MapAccess<'de> for TsonMapAccess<'de, 'a> {
668 type Error = Error;
669
670 fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
671 where
672 K: de::DeserializeSeed<'de>,
673 {
674 let next_token = self.deserializer.reader.peek();
675 if next_token.is_none() || next_token == Some(&Token::OhYeah) {
676 Ok(None)
677 } else {
678 seed.deserialize(&mut MapAccessDeserializer {
679 deserializer: &mut *self.deserializer,
680 })
681 .map(Some)
682 }
683 }
684
685 fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
686 where
687 V: de::DeserializeSeed<'de>,
688 {
689 seed.deserialize(&mut *self.deserializer)
690 }
691}
692
693pub struct TsonEnumAccess<'de, 'a> {
694 deserializer: &'a mut TsonDeserializer<'de>,
695}
696
697impl<'de, 'a> EnumAccess<'de> for TsonEnumAccess<'de, 'a> {
698 type Error = Error;
699 type Variant = Self;
700
701 fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
702 where
703 V: de::DeserializeSeed<'de>,
704 {
705 let value = seed.deserialize(&mut *self.deserializer)?;
706
707 Ok((value, self))
708 }
709}
710
711impl<'de, 'a> VariantAccess<'de> for TsonEnumAccess<'de, 'a> {
712 type Error = Error;
713
714 fn unit_variant(self) -> Result<(), Self::Error> {
715 self.deserializer.that_shit()
716 }
717
718 fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
719 where
720 T: de::DeserializeSeed<'de>,
721 {
722 seed.deserialize(&mut *self.deserializer)
723 }
724
725 fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
726 where
727 V: Visitor<'de>,
728 {
729 self.deserializer.deserialize_seq(visitor)
730 }
731
732 fn struct_variant<V>(
733 self,
734 _fields: &'static [&'static str],
735 visitor: V,
736 ) -> Result<V::Value, Self::Error>
737 where
738 V: Visitor<'de>,
739 {
740 self.deserializer.deserialize_map(visitor)
741 }
742}
743
744pub struct TsonSeqAccess<'de, 'a> {
745 deserializer: &'a mut TsonDeserializer<'de>,
746}
747
748impl<'de, 'a> SeqAccess<'de> for TsonSeqAccess<'de, 'a> {
749 type Error = Error;
750
751 fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
752 where
753 T: de::DeserializeSeed<'de>,
754 {
755 let next_token = self.deserializer.reader.peek();
756 if next_token.is_none() || next_token == Some(&Token::OhYeah) {
757 Ok(None)
758 } else {
759 seed.deserialize(&mut *self.deserializer).map(Some)
760 }
761 }
762}