musli_json/de/
object_pair_decoder.rs

1use musli::de::EntryDecoder;
2use musli::Context;
3
4use crate::parser::{Parser, Token};
5
6use super::{JsonDecoder, JsonKeyDecoder};
7
8pub(crate) struct JsonObjectPairDecoder<'a, P, C: ?Sized> {
9    cx: &'a C,
10    parser: P,
11}
12
13impl<'a, P, C: ?Sized> JsonObjectPairDecoder<'a, P, C> {
14    #[inline]
15    pub(super) fn new(cx: &'a C, parser: P) -> Self {
16        Self { cx, parser }
17    }
18}
19
20impl<'a, 'de, P, C> EntryDecoder<'de> for JsonObjectPairDecoder<'a, P, C>
21where
22    P: Parser<'de>,
23    C: ?Sized + Context,
24{
25    type Cx = C;
26    type DecodeKey<'this> = JsonKeyDecoder<'a, P::Mut<'this>, C>
27    where
28        Self: 'this;
29    type DecodeValue = JsonDecoder<'a, P, C>;
30
31    #[inline]
32    fn decode_key(&mut self) -> Result<Self::DecodeKey<'_>, C::Error> {
33        Ok(JsonKeyDecoder::new(self.cx, self.parser.borrow_mut()))
34    }
35
36    #[inline]
37    fn decode_value(mut self) -> Result<Self::DecodeValue, C::Error> {
38        let actual = self.parser.peek(self.cx)?;
39
40        if !matches!(actual, Token::Colon) {
41            return Err(self
42                .cx
43                .message(format_args!("Expected colon `:`, was {actual}")));
44        }
45
46        self.parser.skip(self.cx, 1)?;
47        Ok(JsonDecoder::new(self.cx, self.parser))
48    }
49}