summary refs log blame commit diff stats
path: root/src/vm/de.rs
blob: 1f009958459b8f546f72a1b6212287048eb7b8f4 (plain) (tree)
1
2
3
4
5
6
7
8
9
10


                                             
                                            
 




                             

                          
                                     
 



                       
                 

                       

                            


                       



                                                                            



                                                      
                      

                                        

 
                                 
                                          


                              
                                      








                                                                                                       
                          










                                                                                           
                          

          

 
                                                               
                             
                      
                                             


                          


                                   

         
 

                                                     
                                             




                                              
                                         
         
     
 
                                             






                                                                               



















































                                                                               
              






                                                      
                             










































                                                                         

                                                          
                                


                 
                                                                  



                                      


                                   
                                                         


























                                                                  

















                                                                    
                                              



                                                           
              
                                                
                                                          

                                            










                                                                        
              
                                       
                                                                      

                                          
                                                                         
              
                                                
                                                                            

                                              
                                                                         

                                              
                                                                         
              
                                                         



                                                          

                                                 
                 

                                                                       


                                





                                                        
                               

                                                
             
                                       










                                                                       
                                               
                                                         


                                                                             
                 
                                 

                      
                            
         
      


                                                  
                                


                 
                                                                  



                                                                              













                                              




                                                            

                                                                           




                                                                         
                                                                   




                                                                 

                                    




                                                               
                                                                                




                                                                            
                                                                        




                                                                    
                                    
                             




                                                    
                                        




                                                                            





                                                    
                                      







                                        











                                                                  
                                                                      


                                     







































































                                                                         
         
                












                                                                    






                                                                           

                                


                      
                                     
                                
                                                                  





                             
                                                                  










































                                                                                                                                                                 




                                                   
 




























                                                                              
                                                                        



                                                


                                                             



































                                                                              

                                                                      







                                                           

                                                                      








                                                           

                                                                      











                                                                                 

                   


                                                      

                                                                      
                                                                          
                                           
                               
                                                                         
     









































































                                                                                

 
// Copyright (C) 2022 Soni L.
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Deserialization-related parts of the VM.

use std::borrow::Cow;
use std::marker::PhantomData;

use indexmap::IndexMap;

use serde::Serialize;
use serde::de::Error as _;
use serde::de::IntoDeserializer as _;

use smallvec::SmallVec;

use these::These;

use super::Frame;
use super::Interpreter;
use super::Pack;
use super::PatternConstants;
use super::PatternElement;
use super::SerdeObject;
use super::Type;
use super::Value;

/// A `DeserializeSeed` for Datafu input.
///
/// This converts from Serde to Datafu's internal representation (a "pack").
pub(crate) struct Packer<'pat, 'state, O: Serialize> {
    /// The global interpreter state.
    interp: Interpreter<'pat, 'state, O>,
    /// Current call limit.
    call_limit: usize,
    /// Whether we're collecting values.
    collecting: bool,
}

struct FramesMut<'packer, 'pat> {
    frames: &'packer mut Vec<Frame<'pat>>,
}

struct Frames<'packer, 'pat> {
    frames: &'packer Vec<Frame<'pat>>,
}

impl<'packer, 'pat> FramesMut<'packer, 'pat> {
    fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item=&'a mut Frame<'pat>> where 'packer: 'a {
        self.frames.iter_mut()
    }

    fn iter_active_mut<'a>(&'a mut self) -> impl Iterator<Item=&'a mut Frame<'pat>> where 'packer: 'a {
        self.iter_mut().filter(|frame| {
            frame.active()
        })
    }
}

impl<'packer, 'pat> Frames<'packer, 'pat> {
    fn iter<'a>(&'a self) -> impl Iterator<Item=&'a Frame<'pat>> where 'packer: 'a {
        self.frames.iter()
    }

    fn iter_active<'a>(&'a self) -> impl Iterator<Item=&'a Frame<'pat>> where 'packer: 'a {
        self.iter().filter(|frame| {
            frame.active()
        })
    }
}

impl<'pat, 'state, 'de, O: Serialize> Packer<'pat, 'state, O> {
    /// Creates a new Packer.
    pub(crate) fn new(
        interp: Interpreter<'pat, 'state, O>,
        call_limit: usize,
    ) -> Self {
        Self {
            interp: interp,
            call_limit: call_limit,
            collecting: false,
        }
    }

    fn frames_mut(&mut self) -> FramesMut<'_, 'pat> {
        FramesMut {
            frames: &mut *self.interp.frames,
        }
    }

    fn frames(&mut self) -> Frames<'_, 'pat> {
        Frames {
            frames: &*self.interp.frames,
        }
    }

    /// Steps the VM into the next operation.
    fn step_in<E: serde::de::Error>(&mut self) -> Result<(), E> {
        if self.call_limit > 0 {
            self.call_limit -= 1;
        } else {
            self.interp.error.insert(crate::errors::MatchError::StackOverflow);
            return Err(todo!());
        }
        // iterate up to the *live* length (i.e. the loop is allowed to modify
        // the length).
        // NOTE: we need to use while-let so as to not borrow anything in an
        // iterator. filtering happens on the *result* of the iterator.
        let mut index_iter = 0..;
        while let Some(index) = index_iter.next().filter(|&i| {
            i < self.interp.frames.len()
        }) {
            let frame = &mut self.interp.frames[index];
            if frame.overstep > 0 || !frame.matches {
                // overstepped and non-matching frames
                frame.overstep += 1;
            } else {
                if !frame.next() {
                    // empty/end-of frames
                    // 1 layer of step-in.
                    // step-out will undo this.
                    // this is correct because this branch implies overstep = 0
                    frame.overstep = 1;
                } else if matches!(
                    frame.op(),
                    PatternElement::SubtreeMarker,
                ) {
                    // subtrees!
                    // these are tricky, because the current frame can be moved
                    // in memory. so we have to use indexing every time.
                    // tho first we set it as overstep because it has special
                    // handling.
                    frame.overstep = 1;
                    let mut at = index + 1;
                    while self.interp.frames[index].next() {
                        let op = self.interp.frames[index].op();
                        if let PatternElement::ValueSubtree {
                            index: subtree, ..
                        } = op {
                            let new_frame = Frame {
                                ops: &self.interp.pat.protos[subtree][..],
                                iar: None,
                                overstep: 0,
                                matches: true,
                            };
                            // we want the "newest" frame last, so it is
                            // easier to unwind back.
                            self.interp.frames.insert(at, new_frame);
                            at += 1;
                        } else {
                            unreachable!()
                        }
                    }
                }
            }
        }
        Ok(())
    }

    /// Steps the VM back into the previous operation.
    fn step_out(
        &mut self,
        mut packs: Vec<Pack<'pat, 'de>>,
    ) -> Vec<Pack<'pat, 'de>> {
        self.call_limit += 1;
        let mut index_iter = 0..;
        while let Some(index) = index_iter.next().filter(|&i| {
            i < self.interp.frames.len()
        }) {
            // iterate backwards
            let index = self.interp.frames.len() - index - 1;
            let frame = &mut self.interp.frames[index];
            if frame.overstep > 0 {
                // handle overstep
                frame.overstep -= 1;
            } else {
                // unwind frame
                if frame.prev() {
                    // successfully unwound. do nothing.
                } else {
                    // find parent frame.
                    let mut count = 1;
                    let mut target = index;
                    while count > 0 && target > 0 {
                        target -= 1;
                        match self.interp.frames[target].num_subtrees() {
                            Some(num) if num <= count => {
                                count -= num;
                            },
                            Some(num) => {
                                count = 0;
                            },
                            None => {
                                count += 1;
                            },
                        }
                    }
                    if count == 0 {
                        // has parent frame
                        let pack = packs.remove(index);
                        todo!("merge packs")
                    }
                }
            }
        }
        packs
    }
}

impl<'pat, 'state, 'de, O> serde::de::DeserializeSeed<'de>
for &mut Packer<'pat, 'state, O>
where
    O: Serialize,
{
    type Value = (Vec<Pack<'pat, 'de>>, Option<SerdeObject<'de>>);
    fn deserialize<D>(
        mut self,
        deserializer: D,
    ) -> Result<Self::Value, D::Error>
    where
        D: serde::Deserializer<'de>
    {
        if let Err(e) = self.step_in() { return Err(e); }
        let pat = self.interp.pat;
        let target_type = self.frames().iter_active().fold(
            Type::IgnoredAny,
            |target_type, frame| {
                match (target_type, frame.get_type(pat)) {
                    (Type::IgnoredAny, Some((ty, _))) => ty,
                    (ty, Some((Type::IgnoredAny, _))) => ty,
                    (Type::String, Some((Type::Str, _))) => {
                        Type::String
                    },
                    (Type::Str, Some((Type::String, _))) => {
                        Type::String
                    },
                    (Type::Bytes, Some((Type::ByteBuf, _))) => {
                        Type::ByteBuf
                    },
                    (Type::ByteBuf, Some((Type::Bytes, _))) => {
                        Type::ByteBuf
                    },
                    (left, Some((right, _))) if left == right => {
                        left
                    },
                    _ => Type::Any,
                }
            },
        );
        match target_type {
            Type::Any => deserializer.deserialize_any(&mut *self),
            Type::IgnoredAny => {
                deserializer.deserialize_ignored_any(&mut *self)
            },
            Type::Bool => deserializer.deserialize_bool(&mut *self),
            Type::I8 => deserializer.deserialize_i8(&mut *self),
            Type::I16 => deserializer.deserialize_i16(&mut *self),
            Type::I32 => deserializer.deserialize_i32(&mut *self),
            Type::I64 => deserializer.deserialize_i64(&mut *self),
            Type::I128 => deserializer.deserialize_i128(&mut *self),
            Type::U8 => deserializer.deserialize_u8(&mut *self),
            Type::U16 => deserializer.deserialize_u16(&mut *self),
            Type::U32 => deserializer.deserialize_u32(&mut *self),
            Type::U64 => deserializer.deserialize_u64(&mut *self),
            Type::U128 => deserializer.deserialize_u128(&mut *self),
            Type::F32 => deserializer.deserialize_f32(&mut *self),
            Type::F64 => deserializer.deserialize_f64(&mut *self),
            Type::Char => deserializer.deserialize_char(&mut *self),
            Type::Str if !self.collecting => {
                deserializer.deserialize_str(&mut *self)
            },
            Type::Str | Type::String => {
                deserializer.deserialize_string(&mut *self)
            },
            Type::Bytes if !self.collecting => {
                deserializer.deserialize_bytes(&mut *self)
            },
            Type::Bytes | Type::ByteBuf => {
                deserializer.deserialize_byte_buf(&mut *self)
            },
            Type::Option => deserializer.deserialize_option(&mut *self),
            Type::Unit => deserializer.deserialize_unit(&mut *self),
            Type::Seq => deserializer.deserialize_seq(&mut *self),
            Type::Map => deserializer.deserialize_map(&mut *self),
            Type::Identifier => {
                deserializer.deserialize_identifier(&mut *self)
            },
            Type::Tuple(len) => {
                deserializer.deserialize_tuple(len, &mut *self)
            },
            Type::UnitStruct(name) => {
                deserializer.deserialize_unit_struct(name, &mut *self)
            },
            Type::NewtypeStruct(name) => {
                deserializer.deserialize_newtype_struct(name, &mut *self)
            },
            Type::TupleStruct { name, len } => {
                deserializer.deserialize_tuple_struct(name, len, &mut *self)
            },
            Type::Struct { name, fields } => {
                deserializer.deserialize_struct(name, fields, &mut *self)
            },
            Type::Enum { name, variants } => {
                deserializer.deserialize_enum(name, variants, &mut *self)
            },
        }.map(|(packs, obj)| (self.step_out(packs), obj))
    }
}

/// visit method generator for simple values (primitives).
///
/// can generate whole function or just the glue.
macro_rules! vs {
    (fn $visit:ident $obj:ident ($data_type:pat) $rust_type:ty) => {
        fn $visit<E>(mut self, v: $rust_type) -> Result<Self::Value, E>
        where
            E: serde::de::Error,
        {
            vs!(self (v) $obj ($data_type))
        }
    };
    ($this:ident $v:tt $obj:ident ($data_type:pat)) => {
        {
            let pat = $this.interp.pat;
            let mut obj = None;
            if $this.collecting {
                obj = Some(SerdeObject::$obj$v);
            }
            let mut packs = Vec::new();
            $this.frames_mut().iter_active_mut().try_for_each(|frame| {
                let ty = frame.get_type(pat);
                match ty {
                    | Some(($data_type, _))
                    | Some((Type::Any, _))
                    | Some((Type::IgnoredAny, _))
                    => {},
                    Some((_, false)) => todo!(),
                    Some((_, true)) => return Err(todo!()),
                    None => unreachable!(),
                }
                let mut pack = Pack::default();
                if let Some(name) = frame.get_name(pat) {
                    let mut map = IndexMap::new();
                    map.insert(name, (Pack::default(), SerdeObject::$obj$v));
                    pack.subpacks.push(map);
                }
                packs.push(pack);
                Ok(())
            })?;
            Ok((packs, obj))
        }
    };
}

impl<'pat, 'state, 'de, O> serde::de::Visitor<'de>
for &mut Packer<'pat, 'state, O>
where
    O: Serialize,
{
    type Value = (Vec<Pack<'pat, 'de>>, Option<SerdeObject<'de>>);
    fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "unsure")
    }

    vs!(fn visit_bool Bool (Type::Bool) bool);
    vs!(fn visit_i8 I8 (Type::I8) i8);
    vs!(fn visit_i16 I16 (Type::I16) i16);
    vs!(fn visit_i32 I32 (Type::I32) i32);
    vs!(fn visit_i64 I64 (Type::I64) i64);
    vs!(fn visit_i128 I128 (Type::I128) i128);
    vs!(fn visit_u8 U8 (Type::U8) u8);
    vs!(fn visit_u16 U16 (Type::U16) u16);
    vs!(fn visit_u32 U32 (Type::U32) u32);
    vs!(fn visit_u64 U64 (Type::U64) u64);
    vs!(fn visit_u128 U128 (Type::U128) u128);
    vs!(fn visit_f32 F32 (Type::F32) f32);
    vs!(fn visit_f64 F64 (Type::F64) f64);
    vs!(fn visit_char Char (Type::Char) char);

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        // no real option but to clone.
        vs!(self (Cow::Owned(v.to_owned())) Str (Type::String | Type::Str))
    }
    fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        vs!(self (Cow::Borrowed(v)) Str (Type::String | Type::Str))
    }
    fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        // TODO try to avoid cloning
        self.visit_str(&*v)
    }
    fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        vs!(self (Cow::Owned(v.to_owned())) Bytes (Type::Bytes | Type::ByteBuf))
    }
    fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        vs!(self (Cow::Borrowed(v)) Bytes (Type::Bytes | Type::ByteBuf))
    }
    fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        // TODO try to avoid cloning
        self.visit_bytes(&*v)
    }
    fn visit_none<E>(self) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        vs!(self {} None (Type::Option))
    }
    fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        todo!()
    }
    fn visit_unit<E>(self) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        vs!(self {} Unit (Type::Unit))
    }
    fn visit_newtype_struct<D>(
        self,
        deserializer: D
    ) -> Result<Self::Value, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        todo!()
    }
    fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::SeqAccess<'de>,
    {
        let mut obj = None;
        if self.collecting {
            obj = Some(SerdeObject::Seq(Vec::new()));
        }
        todo!()
    }
    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::MapAccess<'de>,
    {
        let old_collecting = self.collecting;
        let pat = self.interp.pat;
        let mut collecting = old_collecting;
        self.frames_mut().iter_active_mut().try_for_each(|frame| {
            let ty = frame.get_type(pat);
            match ty {
                | Some((Type::Map, _))
                | Some((Type::Any, _))
                | Some((Type::IgnoredAny, _))
                => {},
                Some((_, false)) => {
                    frame.matches = false;
                    todo!()
                },
                Some((_, true)) => return Err(todo!()),
                None => unreachable!(),
            }
            if frame.get_name(pat).is_some() {
                collecting = true;
            }
            Ok(())
        })?;
        if let Err(e) = self.step_in() { return Err(e); }
        self.collecting = collecting;
        let mut subframes = Vec::new();
        self.frames().iter_active().for_each(|frame| {
            if let PatternElement::Tag { key_subtree } = frame.op() {
                if let Some(key_subtree) = key_subtree {
                    subframes.push(Frame {
                        ops: &pat.protos[key_subtree],
                        iar: None,
                        overstep: 0,
                        matches: true,
                    });
                }
            } else {
                unreachable!()
            }
        });
        let mut obj_inner = Vec::new();
        while let Some(packed_key) = {
            let subinterp = Interpreter {
                pat: pat,
                frames: &mut subframes,
                error: self.interp.error,
            };
            let mut subpacker = Packer {
                interp: subinterp,
                collecting: self.collecting,
                call_limit: self.call_limit,
            };
            map.next_key_seed(&mut subpacker)?
        } {
            self.frames_mut().iter_active_mut().filter(|frame| {
                if let PatternElement::Tag { key_subtree } = frame.op() {
                    key_subtree.is_some()
                } else {
                    unreachable!()
                }
            }).zip(&mut subframes).for_each(|(frame, subframe)| {
                frame.matches = subframe.matches;
                // reset subframe for next iteration
                subframe.matches = true;
                subframe.iar = None;
            });
            let packed_value = map.next_value_seed(&mut *self)?;
            if self.collecting {
                obj_inner.push(
                    (packed_key.1.unwrap(), packed_value.1.unwrap()),
                );
            }
            todo!("merge kv");
        }
        todo!();
    }
    fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::EnumAccess<'de>,
    {
        let mut obj = None;
        if self.collecting {
            obj = Some(SerdeObject::Enum {
                variant: todo!(),
                data: todo!(),
            });
        }
        todo!()
    }
}

/// A `Deserializer` for Datafu output.
///
/// This converts from Datafu's internal representation (a "pack") into the
/// desired output type.
pub struct Unpacker<'pat, 'de> {
    pack: Pack<'pat, 'de>,
    call_limit: usize,
}

impl<'pat, 'de> Unpacker<'pat, 'de> {
    /// Unpacks a Datafu "pack".
    pub fn new(pack: Pack<'pat, 'de>, call_limit: usize) -> Self {
        Self {
            pack, call_limit,
        }
    }
}

impl<'pat, 'de> serde::Deserializer<'de> for Unpacker<'pat, 'de> {
    // TODO datafu errors
    type Error = serde::de::value::Error;
    fn deserialize_any<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_bool<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_i8<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_i16<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_i32<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_i64<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_u8<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_u16<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_u32<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_u64<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_f32<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_f64<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_char<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_str<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_string<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_bytes<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_byte_buf<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_option<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_unit<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_unit_struct<V>(self, _: &'static str, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_newtype_struct<V>(self, _: &'static str, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_seq<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_tuple<V>(self, _: usize, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_tuple_struct<V>(self, _: &'static str, _: usize, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_map<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_struct<V>(
        self,
        _: &'static str,
        fields: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value, Self::Error>
    where
        V: serde::de::Visitor<'de>,
    {
        todo!()
    }
    fn deserialize_enum<V>(self, _: &'static str, _: &'static [&'static str], _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_identifier<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
    fn deserialize_ignored_any<V>(self, _: V) -> Result<V::Value, Self::Error> where V: serde::de::Visitor<'de> { todo!() }
}

/// Deserializes a SerdeObject
pub(crate) struct SerdeObjectDeserializer<'de, E> {
    pub(crate) obj: SerdeObject<'de>,
    pub(crate) value: Option<SerdeObject<'de>>,
    pub(crate) _e: PhantomData<fn() -> E>,
}

impl<'de, E> serde::de::Deserializer<'de> for SerdeObjectDeserializer<'de, E>
where
    E: serde::de::Error,
{
    type Error = E;
    fn deserialize_any<V>(self, v: V) -> Result<V::Value, Self::Error>
    where
        V: serde::de::Visitor<'de>,
    {
        match self.obj {
            SerdeObject::Bool(x) => v.visit_bool(x),
            SerdeObject::I8(x) => v.visit_i8(x),
            SerdeObject::I16(x) => v.visit_i16(x),
            SerdeObject::I32(x) => v.visit_i32(x),
            SerdeObject::I64(x) => v.visit_i64(x),
            SerdeObject::I128(x) => v.visit_i128(x),
            SerdeObject::U8(x) => v.visit_u8(x),
            SerdeObject::U16(x) => v.visit_u16(x),
            SerdeObject::U32(x) => v.visit_u32(x),
            SerdeObject::U64(x) => v.visit_u64(x),
            SerdeObject::U128(x) => v.visit_u128(x),
            SerdeObject::F32(x) => v.visit_f32(x),
            SerdeObject::F64(x) => v.visit_f64(x),
            SerdeObject::Char(x) => v.visit_char(x),
            SerdeObject::Str(Cow::Owned(x)) => v.visit_string(x),
            SerdeObject::Str(Cow::Borrowed(x)) => v.visit_borrowed_str(x),
            SerdeObject::Bytes(Cow::Owned(x)) => v.visit_byte_buf(x),
            SerdeObject::Bytes(Cow::Borrowed(x)) => v.visit_borrowed_bytes(x),
            SerdeObject::Some(x) => v.visit_some(x.into_deserializer()),
            SerdeObject::None => v.visit_none(),
            SerdeObject::Unit => v.visit_unit(),
            SerdeObject::Seq(x) => todo!(),
            SerdeObject::Map(x) => todo!(),
            SerdeObject::NewtypeStruct(x) => {
                v.visit_newtype_struct(x.into_deserializer())
            },
            SerdeObject::Enum { variant, data } => todo!(),
        }
    }
    fn deserialize_ignored_any<V>(self, v: V) -> Result<V::Value, Self::Error>
    where
        V: serde::de::Visitor<'de>,
    {
        drop(self);
        v.visit_unit()
    }
    serde::forward_to_deserialize_any! {
        bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
        bytes byte_buf option unit unit_struct newtype_struct seq tuple
        tuple_struct map struct enum identifier
    }
}

#[cfg(test)]
mod tests {
    use super::Packer;
    use super::super::PatternConstants;
    use crate::vm::MAX_CALLS;
    use crate::vm::Interpreter;
    use crate::vm::Type;
    use crate::vm::Value;
    use crate::vm::PatternElement;
    use crate::vm::SerdeObject;
    use these::These;
    use serde_json::Deserializer as JsonDeserializer;
    use serde::de::DeserializeSeed as _;

    #[test]
    #[should_panic]
    fn test_broken() {
        let consts = PatternConstants::<()>::default();
        let mut err = Default::default();
        let mut frames = Default::default();
        let interp = Interpreter::new(&consts, &mut err, &mut frames);
        let _ = Packer::new(interp, MAX_CALLS);
    }

    #[test]
    fn test_empty_create() {
        let mut consts = PatternConstants::<()>::default();
        consts.protos.push(Vec::new());
        let mut err = Default::default();
        let mut frames = Default::default();
        let interp = Interpreter::new(&consts, &mut err, &mut frames);
        let _ = Packer::new(interp, MAX_CALLS);
    }

    #[test]
    fn test_empty_match() {
        let mut consts = PatternConstants::<()>::default();
        consts.protos.push(Vec::new());
        let mut der = JsonDeserializer::from_str("{}");
        let mut err = Default::default();
        let mut frames = Default::default();
        let interp = Interpreter::new(&consts, &mut err, &mut frames);
        let pack = Packer::new(interp, MAX_CALLS).deserialize(&mut der).unwrap();
    }

    #[test]
    fn test_simple_match() {
        let mut consts = PatternConstants::<()>::default();
        consts.strings.push("hello".into());
        consts.protos.push(vec![
            PatternElement::Value {
                name_and_value: These::Both(0, Value::Type {
                    ty: Type::U64,
                    skippable: false,
                }),
            },
        ]);
        let mut der = JsonDeserializer::from_str("3");
        let mut err = Default::default();
        let mut frames = Default::default();
        let interp = Interpreter::new(&consts, &mut err, &mut frames);
        let packed = Packer::new(interp, MAX_CALLS).deserialize(&mut der);
        let (packs, obj) = packed.unwrap();
        assert!(obj.is_none());
        assert_eq!(packs[0].subpacks[0]["hello"].1, SerdeObject::U64(3));
    }

    #[test]
    fn test_simple_error() {
        let mut consts = PatternConstants::<()>::default();
        consts.strings.push("hello".into());
        consts.protos.push(vec![
            PatternElement::Value {
                name_and_value: These::Both(0, Value::Type {
                    ty: Type::U64,
                    skippable: false,
                }),
            },
        ]);
        let mut der = JsonDeserializer::from_str("\"hello\"");
        let mut err = Default::default();
        let mut frames = Default::default();
        let interp = Interpreter::new(&consts, &mut err, &mut frames);
        let packed = Packer::new(interp, MAX_CALLS).deserialize(&mut der);
        dbg!(&packed);
        // error produced by serde_json
        assert!(packed.is_err());
    }

    #[test]
    fn test_map() {
        let mut consts = PatternConstants::<()>::default();
        consts.strings.push("key".into());
        consts.strings.push("value".into());
        consts.protos.push(vec![
            PatternElement::Value {
                name_and_value: These::This(0),
            },
        ]);
        consts.protos.push(vec![
            PatternElement::Value {
                name_and_value: These::That(Value::Type {
                    ty: Type::Map,
                    skippable: false,
                }),
            },
            PatternElement::Tag {
                key_subtree: Some(0),
            },
            PatternElement::Value {
                name_and_value: These::Both(1, Value::Type {
                    ty: Type::U64,
                    skippable: false,
                }),
            },
        ]);
        let mut der = JsonDeserializer::from_str(r#"{"hello": 0, "world": 1}"#);
        let mut err = Default::default();
        let mut frames = Default::default();
        let interp = Interpreter::new(&consts, &mut err, &mut frames);
        let packed = Packer::new(interp, MAX_CALLS).deserialize(&mut der);
        let (packs, obj) = packed.unwrap();
        assert!(obj.is_none());
        assert_eq!(
            packs[0].subpacks[0]["key"].1,
            SerdeObject::Str("hello".into()),
        );
        assert_eq!(
            packs[0].subpacks[0]["value"].1,
            SerdeObject::U64(0),
        );
        assert_eq!(
            packs[0].subpacks[1]["key"].1,
            SerdeObject::Str("world".into()),
        );
        assert_eq!(
            packs[0].subpacks[1]["value"].1,
            SerdeObject::U64(1),
        );
    }
}