<commit_before>#[macro_export]

macro_rules! enum_to_nl {
    ( $name:ident, $type:tt, $( $cmd:ident ),* ) => {
        #[derive(Clone,Debug,PartialEq)]
        pub enum $name {
            $( $cmd, )*
            UnrecognizedVariant,
        }

        impl Into<$type> for $name {
            fn into(self) -> $type {
                self as $type
            }
        }

        impl From<$type> for $name {
            fn from(v: $type) -> Self {
                match v {
                    $(
                        i if i == $name::$cmd as $type => $name::$cmd,
                    )*
                    _ => $name::UnrecognizedVariant,
                }
            }
        }

        impl Nl for $name {
            type SerIn = ();
            type DeIn = ();

            fn serialize(&self, mem: &mut MemWrite) -> Result<(), SerError> {
                let val = self.clone() as $type;
                val.serialize(mem)?;
                Ok(())
            }

            fn deserialize(mem: &mut MemRead) -> Result<Self, DeError> {
                let cmd = $type::deserialize(mem)?;
                Ok(Self::from(cmd))
            }

            fn size(&self) -> usize {
                ::std::mem::size_of::<$type>()
            }
        }
    }
}

#[macro_export]
macro_rules! cmd_to_nl {
    ( $name:ident, $( $cmd:ident ),* ) => {
        enum_to_nl!($name, u8, $( $cmd ),*);
    }
}

#[cfg(test)]
mod test {
    use {Nl,MemRead,MemWrite};
    use err::{SerError,DeError};

    #[test]
    fn test_attr_cmd_to_nl_macro() {
        cmd_to_nl!(TestCmd, Cmd0, Cmd1, Cmd2, Cmd3);

        let slice = &mut [0u8; 1];
        let mut ser_mem = MemWrite::new_slice(slice);
        TestCmd::Cmd1.serialize(&mut ser_mem).unwrap();
        assert_eq!(ser_mem.as_slice(), &[1]);
        let mut de_mem = ser_mem.into();
        assert_eq!(TestCmd::Cmd1, TestCmd::deserialize(&mut de_mem).unwrap());
    }
}<commit_msg>Rework macros to allow choosing relevant enum options<commit_after>#[macro_export]

macro_rules! enum_to_nl {
    ( $name:ident, $type:tt, $( $cmd:ident ),* ) => {
        #[derive(Clone,Debug,PartialEq)]
        pub enum $name {
            $( $cmd, )*
            UnrecognizedVariant,
        }

        impl Into<$type> for $name {
            fn into(self) -> $type {
                self as $type
            }
        }

        impl From<$type> for $name {
            fn from(v: $type) -> Self {
                match v {
                    $(
                        i if i == $name::$cmd as $type => $name::$cmd,
                    )*
                    _ => $name::UnrecognizedVariant,
                }
            }
        }

        impl Nl for $name {
            type SerIn = ();
            type DeIn = ();

            fn serialize(&self, mem: &mut MemWrite) -> Result<(), SerError> {
                let val = self.clone() as $type;
                val.serialize(mem)?;
                Ok(())
            }

            fn deserialize(mem: &mut MemRead) -> Result<Self, DeError> {
                let cmd = $type::deserialize(mem)?;
                Ok(Self::from(cmd))
            }

            fn size(&self) -> usize {
                ::std::mem::size_of::<$type>()
            }
        }
    };
    ( $name:ident, $type:tt, $( $cmd:ident = $num:tt ),* ) => {
        #[derive(Clone,Debug,PartialEq)]
        pub enum $name {
            $( $cmd = $num, )*
            UnrecognizedVariant,
        }

        impl Into<$type> for $name {
            fn into(self) -> $type {
                self as $type
            }
        }

        impl From<$type> for $name {
            fn from(v: $type) -> Self {
                match v {
                    $(
                        i if i == $name::$cmd as $type => $name::$cmd,
                    )*
                    _ => $name::UnrecognizedVariant,
                }
            }
        }

        impl Nl for $name {
            type SerIn = ();
            type DeIn = ();

            fn serialize(&self, mem: &mut MemWrite) -> Result<(), SerError> {
                let val = self.clone() as $type;
                val.serialize(mem)?;
                Ok(())
            }

            fn deserialize(mem: &mut MemRead) -> Result<Self, DeError> {
                let cmd = $type::deserialize(mem)?;
                Ok(Self::from(cmd))
            }

            fn size(&self) -> usize {
                ::std::mem::size_of::<$type>()
            }
        }
    };
}

#[macro_export]
macro_rules! cmd_to_nl {
    ( $name:ident, $( $cmd:ident ),* ) => {
        enum_to_nl!($name, u8, $( $cmd ),*);
    };
    ( $name:ident, $( $cmd:ident = $num:tt ),* ) => {
        enum_to_nl!($name, u8, $( $cmd = $num ),*);
    };
}

#[cfg(test)]
mod test {
    use {Nl,MemRead,MemWrite};
    use err::{SerError,DeError};

    #[test]
    fn test_attr_cmd_to_nl_macro() {
        cmd_to_nl!(TestCmd, Cmd0, Cmd1, Cmd2, Cmd3);

        let slice = &mut [0u8; 1];
        let mut ser_mem = MemWrite::new_slice(slice);
        TestCmd::Cmd1.serialize(&mut ser_mem).unwrap();
        assert_eq!(ser_mem.as_slice(), &[1]);
        let mut de_mem = ser_mem.into();
        assert_eq!(TestCmd::Cmd1, TestCmd::deserialize(&mut de_mem).unwrap());
    }
}
{'fraction_non_alphanumeric': 0.15819531696173614, 'fraction_numerical': 0.00437845040928993, 'mean_word_length': 1.383847549909256, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7608022', 'n_tokens_mistral': 1682, 'n_tokens_neox': 1642, 'n_words': 327}