Giter Club home page Giter Club logo

serde-klv's Introduction

Serde KLV โ€ƒ Build Status Latest Version Rustc Version 1.65+

Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.


You may be looking for:

Usage

use serde_klv::{from_bytes, to_bytes};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
// Set Universal Key string or byte-literal
#[serde(rename = "TESTDATA00000000")]
// #[serde(rename = "\x06\x0e\x2b\x34\x02\x0b\x01\x01\x0e\x01\x03\x01\x01\x00\x00\x00")]
struct TestStruct<'a> {
    // rename to u8 range number
    #[serde(rename = "10")]
    u8: u8,
    #[serde(rename = "11")]
    u64: u64,
    // can use Option
    #[serde(rename = "120", skip_serializing_if = "Option::is_none")]
    none_skip_some: Option<u16>,
    #[serde(rename = "121", skip_serializing_if = "Option::is_none")]
    none_skip_none: Option<u16>,
    #[serde(rename = "60")]
    str: &'a str,
    // Use `serde_bytes` when using Vec<u8} or &[u8]: https://crates.io/crates/serde_bytes
    #[serde(rename = "61", with = "serde_bytes")]
    bytes: &'a [u8],
    // Implement a serializer when using structs
    #[serde(rename = "62", with = "timestamp_micro")]
    ts: SystemTime,
}

mod timestamp_micro {
    use serde::{Deserialize, Deserializer, Serializer};
    use std::time::{Duration, SystemTime};

    pub fn serialize<S>(date: &SystemTime, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let micros = date
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_micros();
        serializer.serialize_u64(micros as u64)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<SystemTime, D::Error>
    where
        D: Deserializer<'de>,
    {
        let micros = u64::deserialize(deserializer)?;
        SystemTime::UNIX_EPOCH
            .checked_add(Duration::from_micros(micros))
            .ok_or_else(|| serde::de::Error::custom("failed to deserialize systemtime"))
    }
}

fn main() {
    let ts = SystemTime::UNIX_EPOCH
            .checked_add(Duration::from_micros(1_000_233_000))
            .unwrap();
    let t = TestStruct {
        u8: 127,
        u64: u32::MAX as u64 + 1,
        none_skip_some: Some(2016),
        none_skip_none: None,
        str: "this is string",
        bytes: b"this is byte",
        ts,
    };
    let buf = to_bytes(&t).unwrap();
    let x = from_bytes::<TestStruct>(&buf).unwrap();
    assert_eq!(&t, &x);

    // with checksum
    use serde_klv::{from_bytes_with_checksum, to_bytes_with_checksum, WrappedCRC};
    
    let buf = to_bytes_with_checksum(&t, WrappedCRC::default()).unwrap();
    let x: TestStruct = from_bytes_with_checksum(&buf, WrappedCRC::default()).unwrap();
    assert_eq!(&t, &x);
}

serde-klv's People

Contributors

uzuna avatar andresv avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.