1use std::cell::RefCell;
2use std::collections::{HashMap, HashSet};
3use std::io;
4use std::str::FromStr;
5
6use serde::de::{
7 self, DeserializeSeed, EnumAccess, IntoDeserializer, MapAccess, SeqAccess, VariantAccess,
8 Visitor,
9};
10
11use crate::error::{Error, Result};
12use crate::parser::{Parser, Watcher};
13use crate::tokenizer::Mode;
14use crate::tokenizer::{Token, TokenSpan, TokenType};
15
16thread_local!(pub(crate) static SECTION_CTX: RefCell<SectionCtx> = RefCell::new(SectionCtx::empty()));
18
19#[derive(Debug, Clone)]
21pub(crate) struct SectionCtx {
22 token: Option<Token>,
23 typename: &'static str,
24 level: u32,
25}
26
27impl SectionCtx {
28 fn empty() -> SectionCtx {
30 SectionCtx {
31 token: None,
32 typename: "",
33 level: 0,
34 }
35 }
36
37 fn new<T>(&mut self) -> SectionCtx {
39 self.new2(std::any::type_name::<T>())
40 }
41
42 fn new2(&mut self, typename: &'static str) -> SectionCtx {
44 let this = self.clone();
45 self.token = None;
46 self.typename = typename.split("<").next().unwrap().split(":").last().unwrap();
47 self.level += 1;
48 SECTION_CTX.with(|ctx| *ctx.borrow_mut() = self.clone());
49 this
50 }
51
52 fn restore(&mut self, scx: SectionCtx) {
54 *self = scx;
55 SECTION_CTX.with(|ctx| *ctx.borrow_mut() = self.clone());
56 }
57
58 fn update_section_token(&mut self, section_token: Token) {
60 self.token = Some(section_token);
61 SECTION_CTX.with(|ctx| *ctx.borrow_mut() = self.clone());
62 }
63
64 pub fn section_type(&self) -> &'static str {
66 self.typename
67 }
68
69 pub fn subsection_name(&self) -> &str {
71 self.token.as_ref().map(|t| t.value()).unwrap_or("")
72 }
73
74 }
80
81pub(crate) struct Deserializer {
82 pub(crate) parser: Parser,
83 eov: TokenType,
84 ctx: SectionCtx,
85 is_closed: bool,
86 mode: Mode,
87 aliases: HashMap<String, String>,
88 ignored: HashSet<String>,
89}
90
91impl Deserializer {
92 fn new(
93 parser: Parser,
94 mode: Mode,
95 aliases: HashMap<String, String>,
96 ignored: HashSet<String>,
97 ) -> Self {
98 let eov = if mode == Mode::Semicolon {
99 TokenType::Semi
100 } else {
101 TokenType::Nl
102 };
103 Deserializer {
104 parser,
105 eov,
106 ctx: SectionCtx::empty(),
107 is_closed: false,
108 mode,
109 aliases,
110 ignored,
111 }
112 }
113 pub fn from_str(
114 s: impl Into<String>,
115 mode: Mode,
116 aliases: HashMap<String, String>,
117 ignored: HashSet<String>,
118 watcher: Option<Watcher>,
119 ) -> Self {
120 let parser = Parser::from_string(s, mode, watcher);
121 Self::new(parser, mode, aliases, ignored)
122 }
123
124 pub fn from_file(
125 file: impl Into<String>,
126 mode: Mode,
127 aliases: HashMap<String, String>,
128 ignored: HashSet<String>,
129 watcher: Option<Watcher>,
130 ) -> io::Result<Self> {
131 let parser = Parser::from_file(file, mode, watcher)?;
132 Ok(Self::new(parser, mode, aliases, ignored))
133 }
134}
135
136impl Deserializer {
137 fn parse_expr<T>(&mut self, name: &str) -> Result<(TokenSpan, T)>
138 where
139 T: FromStr,
140 {
141 let word = self.parser.expect(TokenType::Word)?;
142 let value: T = FromStr::from_str(word.value())
143 .map_err(|_| Error::new(format!("expected {} value", name), word.span()))?;
144 Ok((word.span(), value))
145 }
146
147 fn current_section(&self, t1: &Token) -> bool {
148 let v1 = format!("{}.{}", self.ctx.section_type(), t1.value());
149 let v2 = format!("{}.{}", self.ctx.section_type(), self.ctx.subsection_name());
150 let t1 = self.aliases.get(&v1).map(|v| v.as_str()).unwrap_or(t1.value());
151 let t2 = self
152 .aliases
153 .get(&v2)
154 .map(|s| s.as_str())
155 .unwrap_or(self.ctx.subsection_name());
156 t1 == t2
157 }
158
159 fn check_include(&mut self) -> Result<()> {
161 loop {
163 let mut lookahead = self.parser.lookahead(1);
164 if let Some(ident) = lookahead.peek(TokenType::Ident)? {
165 if ident.value() == "include" {
166 lookahead.advance(&mut self.parser);
167 let curfile = &ident.span().source.filename;
168 let filename = self.parser.expect(TokenType::Expr)?;
169 self.parser.expect(self.eov)?;
170 self.parser.include(filename.value(), curfile).map_err(|e| {
171 Error::new(e.to_string(), filename.span())
172 })?;
173 continue;
174 }
175 }
176 break;
177 }
178 Ok(())
179 }
180}
181
182impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer {
183 type Error = Error;
184
185 fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value>
186 where
187 V: Visitor<'de>,
188 {
189 let value = self.parser.expect(TokenType::Word)?;
190 Err(Error::new("BUG: serde called deserialize_any", value.span()))
191 }
192
193 fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
194 where
195 V: Visitor<'de>,
196 {
197 let mut lookahead = self.parser.lookaheadnl(1);
198
199 if let Some(token) = lookahead.peek(self.eov)? {
202 return visitor
203 .visit_bool(true)
204 .map_err(|e| update_span(e, token.span()));
205 }
206 if let Some(token) = lookahead.peek(TokenType::Expr)? {
207 lookahead.advance(&mut self.parser);
208 let v = match token.value() {
209 "y" | "yes" | "t" | "true" | "on" | "1" => true,
210 "n" | "no" | "f" | "false" | "off" | "0" => false,
211 _ => return Err(Error::new(format!("expected boolean"), token.span())),
212 };
213 return visitor.visit_bool(v).map_err(|e| update_span(e, token.span()));
214 }
215 lookahead.error()
216 }
217
218 fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value>
221 where
222 V: Visitor<'de>,
223 {
224 let (span, value) = self.parse_expr("i8 integer")?;
225 visitor.visit_i8(value).map_err(|e| update_span(e, span))
226 }
227
228 fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value>
229 where
230 V: Visitor<'de>,
231 {
232 let (span, value) = self.parse_expr("i16 integer")?;
233 visitor.visit_i16(value).map_err(|e| update_span(e, span))
234 }
235
236 fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value>
237 where
238 V: Visitor<'de>,
239 {
240 let (span, value) = self.parse_expr("i32 integer")?;
241 visitor.visit_i32(value).map_err(|e| update_span(e, span))
242 }
243
244 fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value>
245 where
246 V: Visitor<'de>,
247 {
248 let (span, value) = self.parse_expr("i64 integer")?;
249 visitor.visit_i64(value).map_err(|e| update_span(e, span))
250 }
251
252 fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
253 where
254 V: Visitor<'de>,
255 {
256 let (span, value) = self.parse_expr("u8 integer")?;
257 visitor.visit_u8(value).map_err(|e| update_span(e, span))
258 }
259
260 fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value>
261 where
262 V: Visitor<'de>,
263 {
264 let (span, value) = self.parse_expr("u16 integer")?;
265 visitor.visit_u16(value).map_err(|e| update_span(e, span))
266 }
267
268 fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value>
269 where
270 V: Visitor<'de>,
271 {
272 let (span, value) = self.parse_expr("u32 integer")?;
273 visitor.visit_u32(value).map_err(|e| update_span(e, span))
274 }
275
276 fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value>
277 where
278 V: Visitor<'de>,
279 {
280 let (span, value) = self.parse_expr("u64 integer")?;
281 visitor.visit_u64(value).map_err(|e| update_span(e, span))
282 }
283
284 fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value>
285 where
286 V: Visitor<'de>,
287 {
288 let (span, value) = self.parse_expr("f32 float")?;
289 visitor.visit_f32(value).map_err(|e| update_span(e, span))
290 }
291
292 fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value>
293 where
294 V: Visitor<'de>,
295 {
296 let (span, value) = self.parse_expr("f64 float")?;
297 visitor.visit_f64(value).map_err(|e| update_span(e, span))
298 }
299
300 fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>
301 where
302 V: Visitor<'de>,
303 {
304 let (span, value) = self.parse_expr("single character")?;
306 visitor.visit_char(value).map_err(|e| update_span(e, span))
307 }
308
309 fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
310 where
311 V: Visitor<'de>,
312 {
313 let (span, value) = self.parse_expr::<String>("string")?;
314 visitor.visit_str(&value).map_err(|e| update_span(e, span))
315 }
316
317 fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
318 where
319 V: Visitor<'de>,
320 {
321 let (span, value) = self.parse_expr::<String>("string")?;
322 visitor.visit_str(&value).map_err(|e| update_span(e, span))
323 }
324
325 fn deserialize_bytes<V>(self, _visitor: V) -> Result<V::Value>
326 where
327 V: Visitor<'de>,
328 {
329 unimplemented!()
330 }
331
332 fn deserialize_byte_buf<V>(self, _visitor: V) -> Result<V::Value>
333 where
334 V: Visitor<'de>,
335 {
336 unimplemented!()
337 }
338
339 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
343 where
344 V: Visitor<'de>,
345 {
346 visitor.visit_some(self)
347 }
348
349 fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
351 where
352 V: Visitor<'de>,
353 {
354 visitor.visit_unit()
355 }
356
357 fn deserialize_unit_struct<V>(self, _name: &'static str, _visitor: V) -> Result<V::Value>
359 where
360 V: Visitor<'de>,
361 {
362 unimplemented!()
363 }
364
365 fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
367 where
368 V: Visitor<'de>,
369 {
370 visitor.visit_newtype_struct(self)
371 }
372
373 fn deserialize_seq<V>(mut self, visitor: V) -> Result<V::Value>
375 where
376 V: Visitor<'de>,
377 {
378 self.is_closed = false;
380 visitor.visit_seq(ListAccess::new(&mut self, false)).map_err(|e| update_span(e, self.parser.last_span()))
381 }
382
383 fn deserialize_tuple<V>(mut self, _len: usize, visitor: V) -> Result<V::Value>
385 where
386 V: Visitor<'de>,
387 {
388 self.is_closed = false;
390 visitor.visit_seq(ListAccess::new(&mut self, true)).map_err(|e| update_span(e, self.parser.last_span()))
391 }
392
393 fn deserialize_tuple_struct<V>(
395 self,
396 _name: &'static str,
397 _len: usize,
398 visitor: V,
399 ) -> Result<V::Value>
400 where
401 V: Visitor<'de>,
402 {
403 self.deserialize_tuple(_len, visitor)
404 }
405
406 fn deserialize_map<V>(mut self, visitor: V) -> Result<V::Value>
413 where
414 V: Visitor<'de>,
415 {
416 debug!("deserialize_map");
417
418 let hma = HashMapAccess::new::<V::Value>(&mut self);
420 let value = visitor
421 .visit_map(hma)
422 .map_err(|e| update_span(e, self.parser.last_span()))?;
423
424 self.is_closed = true;
426
427 Ok(value)
428 }
429
430 fn deserialize_struct<V>(
436 mut self,
437 _name: &'static str,
438 fields: &'static [&'static str],
439 visitor: V,
440 ) -> Result<V::Value>
441 where
442 V: Visitor<'de>,
443 {
444 debug!("deserialize_struct({})", _name);
445
446 let mut label = None;
447 if self.ctx.level > 0 {
448 let mut lookahead = self.parser.lookahead(1);
450 let token = lookahead.peek(TokenType::Expr)?;
451 debug!("deserialize_struct({}): peeked: {:?}", _name, token);
452 if let Some(lbl) = token {
453 lookahead.advance(&mut self.parser);
455 if !fields.contains(&"__label__") {
456 debug!("deserialize_struct({}): fields has no __label__", _name);
457 return Err(Error::new(format!("expected '{{'"), lbl.span()));
458 }
459 debug!("fields is OK");
460 label = Some(lbl.value().to_string());
461 } else {
462 if fields.contains(&"__label__") {
464 lookahead.end()?;
466 }
467 }
468
469 if self.mode == Mode::Diablo {
471 self.parser.expect(TokenType::Nl)?;
472 } else {
473 self.parser.expect(TokenType::LcBrace)?;
474 }
475 }
476
477 let saved_context = self.ctx.new::<V::Value>();
479 let res = visitor.visit_map(SectionAccess::new(&mut self, label, Some(fields)));
480 self.ctx.restore(saved_context);
481 let value = res.map_err(|e| update_span(e, self.parser.last_span()))?;
482
483 debug!("XX level is {}", self.ctx.level);
484 if self.ctx.level != 0 {
485 if self.mode == Mode::Diablo {
487 self.parser.expect(TokenType::End)?;
488 } else {
489 self.parser.expect(TokenType::RcBrace)?;
490 }
491 }
492
493 self.is_closed = true;
495
496 Ok(value)
497 }
498
499 fn deserialize_enum<V>(
501 self,
502 _name: &'static str,
503 _variants: &'static [&'static str],
504 visitor: V,
505 ) -> Result<V::Value>
506 where
507 V: Visitor<'de>,
508 {
509 let mut lookahead = self.parser.lookaheadnl(1);
510 if let Some(token) = lookahead.peek(TokenType::Ident)? {
511 visitor.visit_enum(Enum::new(self)).map_err(|e| update_span(e, token.span()))
512 } else {
513 lookahead.error()
514 }
515 }
516
517 fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value>
520 where
521 V: Visitor<'de>,
522 {
523 self.deserialize_str(visitor)
524 }
525
526 fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
530 where
531 V: Visitor<'de>,
532 {
533 loop {
534 let mut lookahead = self.parser.lookaheadnl(1);
535 if lookahead.peek(self.eov)?.is_some() {
536 break;
537 }
538 lookahead.advance(&mut self.parser);
539 }
540 self.deserialize_unit(visitor)
541 }
542}
543
544struct ListAccess<'a> {
546 de: &'a mut Deserializer,
547 first: bool,
548 is_tuple: bool,
549}
550
551impl<'a> ListAccess<'a> {
552 fn new(de: &'a mut Deserializer, is_tuple: bool) -> Self {
553 ListAccess { de, first: true, is_tuple }
554 }
555}
556
557impl<'de, 'a> SeqAccess<'de> for ListAccess<'a> {
560 type Error = Error;
561
562 fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
563 where
564 T: DeserializeSeed<'de>,
565 {
566 if !self.first {
567 let mut lookahead = self.de.parser.lookaheadnl(2);
568 let mut expect_next = false;
569 let mut continuation = false;
570 let mut check_continuation_no_eov = false;
571
572 if self.de.is_closed && !self.is_tuple {
574 check_continuation_no_eov = true;
575 }
576
577 if let Some(_) = lookahead.peek(TokenType::Comma)? {
579 lookahead.advance(&mut self.de.parser);
580 check_continuation_no_eov = false;
581 expect_next = true;
582 }
583
584 if let Some(_) = lookahead.peek(self.de.eov)? {
586 check_continuation_no_eov = false;
587
588 let mut lookahead2 = self.de.parser.lookaheadnl(2);
590
591 if let Some(ident) = lookahead2.peek2(self.de.eov, TokenType::Ident)? {
592 if self.de.current_section(&ident) {
593 self.de.ctx.update_section_token(ident);
597 lookahead2.advance(&mut self.de.parser);
598 continuation = true;
599 }
600 }
601 }
602
603 if check_continuation_no_eov {
606
607 let mut lookahead = self.de.parser.lookahead(1);
609
610 if let Some(ident) = lookahead.peek(TokenType::Ident)? {
611 if self.de.current_section(&ident) {
612 self.de.ctx.update_section_token(ident);
616 lookahead.advance(&mut self.de.parser);
617 continuation = true;
618 }
619 }
620 }
621
622 if !continuation {
623 if !self.de.is_closed {
624 lookahead.end()?;
625 }
626 if !expect_next {
627 return Ok(None);
628 }
629 }
630 }
631 self.first = false;
632 self.de.is_closed = false;
633
634 let token = self.de.parser.peek()?;
636 seed.deserialize(&mut *self.de)
637 .map(Some)
638 .map_err(|e| update_tspan(e, &token))
639 }
640}
641
642struct SectionAccess<'a> {
643 de: &'a mut Deserializer,
644 label: Option<String>,
645 first: bool,
646 fields: Option<&'static [&'static str]>,
647}
648
649impl<'a> SectionAccess<'a> {
650 fn new(
651 de: &'a mut Deserializer,
652 label: Option<String>,
653 fields: Option<&'static [&'static str]>,
654 ) -> Self {
655 debug!("SectionAccess::new");
656 SectionAccess {
657 label,
658 de,
659 first: true,
660 fields,
661 }
662 }
663}
664
665impl<'de, 'a> MapAccess<'de> for SectionAccess<'a> {
668 type Error = Error;
669
670 fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
671 where
672 K: DeserializeSeed<'de>,
673 {
674 debug!("SectionAccess::MapAccess::next_key_seed");
675
676 if self.first && self.label.is_some() {
678 debug!(
679 "SectionAccess::MapAccess::next_key_seed: insert label {:?}",
680 self.label
681 );
682 let de = "__label__".into_deserializer();
683 return seed.deserialize(de).map(Some);
684 }
685
686 if !self.first {
687 if !self.de.is_closed {
688 self.de.parser.expect(self.de.eov)?;
690 }
691 }
692 self.first = false;
693
694 self.de.check_include()?;
696
697 debug!("check for EOF");
699 if self.de.ctx.level == 1 && self.de.parser.is_eof() {
700 return Ok(None);
701 }
702 debug!("check for EOF done");
703
704 let mut continuation = false;
706 let mut lookahead = self.de.parser.lookaheadnl(2);
707 if let Some(ident) = lookahead.peek2(self.de.eov, TokenType::Ident)? {
708 if self.de.current_section(&ident) {
709 lookahead.advance(&mut self.de.parser);
710 continuation = true;
711 }
712 }
713
714 let mut lookahead = self.de.parser.lookahead(1);
715
716 if !continuation {
717 let closed = if self.de.mode == Mode::Diablo {
719 lookahead.peek(TokenType::End)?.is_some()
720 } else {
721 lookahead.peek(TokenType::RcBrace)?.is_some()
722 };
723 if closed {
724 return Ok(None);
725 }
726 }
727
728 if let Some(token) = lookahead.peek(TokenType::Ident)? {
730 debug!("next_key_seed: key {:?}", token);
731 lookahead.advance(&mut self.de.parser);
732
733 let type_dot_field = format!("{}.{}", self.de.ctx.section_type(), token.value());
735 let name = self
736 .de
737 .aliases
738 .get(&type_dot_field)
739 .map(|s| s.as_str())
740 .unwrap_or(token.value());
741
742 let fields = self.fields.as_ref().unwrap();
744 if !fields.contains(&name) && !self.de.ignored.contains(&type_dot_field) {
745 return Err(Error::new(
746 format!("unknown field: `{}'", token.value()),
747 token.span(),
748 ));
749 }
750 debug!("XXX set section_name_token to {}", token.value());
751 self.de.ctx.update_section_token(token.clone());
752
753 return seed.deserialize(name.into_deserializer()).map(Some);
755 }
756
757 lookahead.error()
758 }
759
760 fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
761 where
762 V: DeserializeSeed<'de>,
763 {
764 debug!(
765 "SectionAccess::MapAccess::next_value_seed {}",
766 std::any::type_name::<V::Value>()
767 );
768
769 if let Some(label) = self.label.take() {
771 let mut de = Deserializer::from_str(label, Mode::Newline, HashMap::new(), HashSet::new(), self.de.parser.watcher.clone());
772 return seed.deserialize(&mut de);
773 }
774
775 self.de.is_closed = false;
777 let token = self.de.parser.peek()?;
778 seed.deserialize(&mut *self.de)
779 .map_err(|e| update_tspan(e, &token))
780 }
781}
782
783struct HashMapAccess<'a> {
784 de: &'a mut Deserializer,
785 first: bool,
786 subsection: Option<Token>,
787 typename: &'static str,
788}
789
790impl<'a> HashMapAccess<'a> {
791 fn new<T>(de: &'a mut Deserializer) -> Self {
792 debug!("HashMapAccess::new");
793 let typename = std::any::type_name::<T>();
794 HashMapAccess { de, first: true, subsection: None, typename }
795 }
796}
797
798impl<'de, 'a> MapAccess<'de> for HashMapAccess<'a> {
800 type Error = Error;
801
802 fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
803 where
804 K: DeserializeSeed<'de>,
805 {
806 debug!("HashMapAccess::MapAccess::next_key_seed");
807
808 if !self.first {
809 let mut continuation = false;
811
812 let mut lookahead = self.de.parser.lookaheadnl(2);
813 if self.de.is_closed {
814
815 if self.de.mode == Mode::Diablo {
817 if let Some(ident) = lookahead.peek2(TokenType::Nl, TokenType::Ident)? {
818 if self.de.current_section(&ident) {
819 lookahead.advance(&mut self.de.parser);
820 continuation = true;
821 }
822 }
823 } else {
824 if let Some(ident) = lookahead.peek(TokenType::Ident)? {
825 if self.de.current_section(&ident) {
826 lookahead.advance(&mut self.de.parser);
827 continuation = true;
828 }
829 }
830 }
831 } else {
832 if let Some(ident) = lookahead.peek2(self.de.eov, TokenType::Ident)? {
834 if self.de.current_section(&ident) {
835 lookahead.advance(&mut self.de.parser);
836 continuation = true;
837 }
838 }
839 }
840
841 if !continuation {
842 if !self.de.is_closed {
843 self.de.parser.expect(self.de.eov)?;
845 }
846 return Ok(None);
847 }
848 }
849 self.first = false;
850
851 let mut lookahead = self.de.parser.lookaheadnl(2);
853 let ttype = if self.de.mode == Mode::Diablo {
854 TokenType::Nl
855 } else {
856 TokenType::LcBrace
857 };
858 if let Some(_) = lookahead.peek2(TokenType::Expr, ttype)? {
859 let saved_pos = self.de.parser.save_pos();
861 let result: Result<K::Value> = seed.deserialize(&mut *self.de);
862 match result {
863 Ok(res) => {
864 self.de.parser.restore_pos(saved_pos);
865 return Ok(Some(res));
866 },
867 Err(e) => return Err(e),
868 }
869 }
870
871 if let Some(key) = lookahead.peek(TokenType::Expr)? {
876 if let Some(ctx_token) = self.de.ctx.token.as_ref() {
878 let mut token = ctx_token.clone();
879 token.value = Some(format!("{}.{}", self.de.ctx.subsection_name(), key.value()));
880 self.subsection = Some(token);
881 }
882 }
883
884 seed.deserialize(&mut *self.de).map(Some)
885 }
886
887 fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
888 where
889 V: DeserializeSeed<'de>,
890 {
891 debug!(
892 "HashMapAccess::MapAccess::next_value_seed {}",
893 std::any::type_name::<V::Value>()
894 );
895
896 self.de.is_closed = false;
898
899 let token = self.de.parser.peek()?;
901
902 let saved = match self.subsection.take() {
904 Some(subsection) => {
905 let saved = self.de.ctx.new2(self.typename);
906 self.de.ctx.update_section_token(subsection);
907 Some(saved)
908 },
909 None => None,
910 };
911
912 let result = seed.deserialize(&mut *self.de).map_err(|e| update_tspan(e, &token));
914
915 if let Some(saved) = saved {
917 self.de.ctx.restore(saved);
918 }
919
920 result
921 }
922}
923
924struct Enum<'a> {
925 de: &'a mut Deserializer,
926}
927
928impl<'a> Enum<'a> {
929 fn new(de: &'a mut Deserializer) -> Self {
930 Enum { de }
931 }
932}
933
934impl<'de, 'a> EnumAccess<'de> for Enum<'a> {
937 type Error = Error;
938 type Variant = Self;
939
940 fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant)>
941 where
942 V: DeserializeSeed<'de>,
943 {
944 let val = seed.deserialize(&mut *self.de)?;
946
947 Ok((val, self))
949 }
950}
951
952impl<'de, 'a> VariantAccess<'de> for Enum<'a> {
955 type Error = Error;
956
957 fn unit_variant(self) -> Result<()> {
959 Ok(())
960 }
961
962 fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
964 where
965 T: DeserializeSeed<'de>,
966 {
967 seed.deserialize(self.de)
968 }
969
970 fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
972 where
973 V: Visitor<'de>,
974 {
975 de::Deserializer::deserialize_tuple(self.de, _len, visitor)
976 }
977
978 fn struct_variant<V>(self, fields: &'static [&'static str], visitor: V) -> Result<V::Value>
980 where
981 V: Visitor<'de>,
982 {
983 de::Deserializer::deserialize_struct(self.de, "", fields, visitor)
984 }
985}
986
987fn update_span(mut e: Error, span: TokenSpan) -> Error {
989 if e.span.is_none() {
990 e.span = Some(span);
991 }
992 e
993}
994
995fn update_tspan(mut e: Error, token: &Option<Token>) -> Error {
997 if e.span.is_none() {
998 if let Some(token) = token.as_ref() {
999 e.span = Some(token.span());
1000 }
1001 }
1002 e
1003}