Skip to content
Snippets Groups Projects
Select Git revision
  • 652e457b3296e21d016283f10868f26f95f06b64
  • master default protected
  • tuxmain/fix-change-owner-key
  • fix_picked_up_file_in_runtime_release
  • network/gtest-1000 protected
  • upgradable-multisig
  • runtime/gtest-1000
  • network/gdev-800 protected
  • cgeek/issue-297-cpu
  • gdev-800-tests
  • update-docker-compose-rpc-squid-names
  • fix-252
  • 1000i100-test
  • hugo/tmp-0.9.1
  • network/gdev-803 protected
  • hugo/endpoint-gossip
  • network/gdev-802 protected
  • hugo/distance-precompute
  • network/gdev-900 protected
  • tuxmain/anonymous-tx
  • debug/podman
  • gtest-1000-0.11.1 protected
  • gtest-1000-0.11.0 protected
  • gtest-1000 protected
  • gdev-900-0.10.1 protected
  • gdev-900-0.10.0 protected
  • gdev-900-0.9.2 protected
  • gdev-800-0.8.0 protected
  • gdev-900-0.9.1 protected
  • gdev-900-0.9.0 protected
  • gdev-803 protected
  • gdev-802 protected
  • runtime-801 protected
  • gdev-800 protected
  • runtime-800-bis protected
  • runtime-800 protected
  • runtime-800-backup protected
  • runtime-701 protected
  • runtime-700 protected
  • runtime-600 protected
  • runtime-500 protected
41 results

README.md

Blame
  • deserializer.rs 3.56 KiB
    //  Copyright (C) 2019  Eloïs SANCHEZ.
    //
    // This program is free software: you can redistribute it and/or modify
    // it under the terms of the GNU Affero General Public License as
    // published by the Free Software Foundation, either version 3 of the
    // License, or (at your option) any later version.
    //
    // This program is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU Affero General Public License for more details.
    //
    // You should have received a copy of the GNU Affero General Public License
    // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    
    //! Define PKSTL deserializer.
    
    use super::SerdeError;
    use super::{IncomingMessage, HEADER_FORMAT_LEN};
    use crate::format::MessageFormat;
    use crate::{Error, IncomingBinaryMessage, Result, SecureLayer};
    use serde::de::DeserializeOwned;
    use std::convert::TryFrom;
    use std::fmt::Debug;
    
    pub(crate) fn read<M>(
        sl: &mut SecureLayer,
        incoming_datas: &[u8],
    ) -> Result<Vec<IncomingMessage<M>>>
    where
        M: Debug + DeserializeOwned,
    {
        let bin_msgs = sl.read_bin(incoming_datas)?;
    
        let mut msgs = Vec::new();
    
        for bin_msg in bin_msgs {
            match bin_msg {
                IncomingBinaryMessage::Connect {
                    custom_datas,
                    peer_sig_public_key,
                } => msgs.push(IncomingMessage::Connect {
                    custom_datas: if let Some(custom_datas) = custom_datas {
                        Some(deserialize(&custom_datas)?)
                    } else {
                        None
                    },
                    peer_sig_public_key,
                }),
                IncomingBinaryMessage::Ack { custom_datas } => msgs.push(IncomingMessage::Ack {
                    custom_datas: if let Some(custom_datas) = custom_datas {
                        Some(deserialize(&custom_datas)?)
                    } else {
                        None
                    },
                }),
                IncomingBinaryMessage::Message { datas } => msgs.push(IncomingMessage::Message {
                    datas: if let Some(datas) = datas {
                        Some(deserialize(&datas)?)
                    } else {
                        None
                    },
                }),
            };
        }
        Ok(msgs)
    }
    
    #[inline]
    fn deserialize<M: Debug + DeserializeOwned>(binary_message: &[u8]) -> Result<M> {
        if binary_message.len() < HEADER_FORMAT_LEN {
            return Err(Error::RecvInvalidMsg(
                crate::errors::IncomingMsgErr::MessageTooShort,
            ));
        }
    
        // Read format
        let message_format = MessageFormat::try_from(&binary_message[..HEADER_FORMAT_LEN])?;
    
        deserialize_inner(&binary_message[HEADER_FORMAT_LEN..], message_format)
            .map_err(Error::SerdeError)
    }
    
    pub fn deserialize_inner<M>(
        binary_message: &[u8],
        message_format: MessageFormat,
    ) -> std::result::Result<M, SerdeError>
    where
        M: Debug + DeserializeOwned,
    {
        match message_format {
            MessageFormat::RawBinary => Err(SerdeError::UseSuffixedBinFunctions),
            #[cfg(feature = "bin")]
            MessageFormat::Bincode => Ok(bincode::deserialize::<M>(binary_message)
                .map_err(|e| SerdeError::BincodeError(format!("{}", e)))?),
            #[cfg(feature = "cbor")]
            MessageFormat::Cbor => {
                Ok(serde_cbor::from_slice::<M>(binary_message).map_err(SerdeError::CborError)?)
            }
            #[cfg(feature = "json")]
            MessageFormat::Utf8Json => {
                Ok(serde_json::from_slice::<M>(binary_message).map_err(SerdeError::JsonError)?)
            }
            _ => unimplemented!(),
        }
    }