Strongly typed Gura library for Rust

Overview

Serde Gura

CI

This crate is a Rust library for using the Serde serialization framework with data in Gura file format.

This library does not re-implement a Gura parser; it uses the gura-rs-parser which is a pure Rust Gura 1.0.0 implementation.

Documentation - Cargo

Dependency

Add the following dependencies to your Cargo.toml:

[dependencies]
serde = "1.0"
serde_gura = "0.1.3"

If you want to use Serialize/Deserialize traits you must specify the derive feature in your Cargo.toml:

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_gura = "0.1.3"

Using Serde Gura

API documentation is available but the general idea is:

use serde::{Deserialize, Serialize};
use serde_gura::Result;

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Database {
    ip: String,
    port: Vec<u16>,
    connection_max: u32,
    enabled: bool,
}

fn main() -> Result<()> {
    // You have some type.
    let database = Database {
        ip: "127.0.0.1".to_string(),
        port: vec![80, 8080],
        connection_max: 1200,
        enabled: true,
    };

    // Serialize it to a Gura string
    let database_str = serde_gura::to_string(&database)?;
    let expected = r#"
ip: "127.0.0.1"
port: [80, 8080]
connection_max: 1200
enabled: true
    "#;
    assert_eq!(database_str, expected.trim());

    // Deserialize it back to a Rust type
    let deserialized_database: Database = serde_gura::from_str(&database_str)?;
    assert_eq!(database, deserialized_database);

    Ok(())
}

Licence

Serde Gura is distributed under the terms of the MIT license.

You might also like...
Rust telegram bot library for many runtimes

Telbot Telbot provides telegram bot types and api wrappers. Specifically, telbot now supports: telbot-types: basic telegram types / requests / respons

An asynchronous Rust client library for the Hashicorp Vault API

vaultrs An asynchronous Rust client library for the Hashicorp Vault API The following features are currently supported: Auth AppRole JWT/OIDC Token Us

Culture ship names in a rust library.

General Systems Vehicles Culture Ships In case you ever needed Iain M. Banks's Culture ship names as a service. Names sourced from the pleasingly exte

Modrinth API is a simple library for using, you guessed it, the Modrinth API in Rust projects

Modrinth API is a simple library for using, you guessed it, the Modrinth API in Rust projects. It uses reqwest as its HTTP(S) client and deserialises responses to typed structs using serde.

Wrapper library for utilizing DigitalOcean API v2 in Rust

doapi-rs Wrapper library for utilizing DigitalOcean API v2 in Rust Disclaimer This library is in alpha - it may do anything up to, and including, eati

Rust LZ4 bindins library

lz4 This repository contains binding for lz4 compression library (https://github.com/Cyan4973/lz4). LZ4 is a very fast lossless compression algorithm,

Noto-sans-mono-bitmap (Rust library)
Noto-sans-mono-bitmap (Rust library)

noto-sans-mono-bitmap (Rust library) Pre-rasterized bitmap font from "Noto Sans Mono", an open font from Google. Original font files taken from: https

Serenity is a Rust library for the Discord API
Serenity is a Rust library for the Discord API

serenity Serenity is a Rust library for the Discord API. View the examples on how to make and structure a bot. Serenity supports bot login via the use

Deser: an experimental serialization and deserialization library for Rust

deser: an experimental serialization and deserialization library for Rust Deser is an experimental serialization system for Rust. It wants to explore

Comments
  • Possible to have line info in errors ?

    Possible to have line info in errors ?

    Every time I make a mistake in my gura file, I get an error and I need to figure out where this damn error is located. Having a line information would be a massive quality of life improvement!

    So far I've only met one instance of error coming with the line number

    Err: Input text does not have a valid Gura format. Parsing failed with error "Expected end of string but got 'a' at line 65 position 1800", in file Some("my_file_path.ura")

    But all other errors have been a hide and seek game between me and my configuration file.

    opened by StyMaar 7
  • Incorrect HashMap serialization

    Incorrect HashMap serialization

    Hashmaps seem to be incorrectly serialized (or at least I guess it must be a serialization issue, because the resulting string cannot be parsed back by the Gura parser).

    use std::collections::HashMap;
    
    use serde::{Deserialize, Serialize};
    use serde_gura::Result;
    use gura::parse;
    
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Database {
        ip: String,
        port: Vec<u16>,
        connection_max: u32,
        enabled: bool,
    }
    
    fn main() -> Result<()> {
    
        let database = Database {
            ip: "127.0.0.1".to_string(),
            port: vec![80, 8080],
            connection_max: 1200,
            enabled: true,
        };
    
        let mut map = HashMap::new();
        map.insert("k", database);
    
        let sss_str = serde_gura::to_string(&map)?;
        
        println!("{}", sss_str);
        serde_gura::from_str(&sss_str)?;
    }
    

    output:

    "k":
        ip: "127.0.0.1"
        port: [80, 8080]
        connection_max: 1200
        enabled: true
    Error: Syntax("Expected end of string but got '\"' at line 0 position 0")
    

    If I understand the Gura format correctly, the serialized version should be:

    k: # no «"» around the key name
        ip: "127.0.0.1"
        port: [80, 8080]
        connection_max: 1200
        enabled: true
    

    And this does parse correctly:

    
    use std::collections::HashMap;
    
    use serde::{Deserialize, Serialize};
    use serde_gura::Result;
    use gura::parse;
    
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Database {
        ip: String,
        port: Vec<u16>,
        connection_max: u32,
        enabled: bool,
    }
    
    fn main() -> Result<()> {
    
        let database_str = r##"
    k:
        ip: "127.0.0.1"
        port: [80, 8080]
        connection_max: 1200
        enabled: true
    
        "##;
    
        // Deserialize it back to a Rust type
        let deserialized_database: HashMap<String, Database> = serde_gura::from_str(&database_str)?;
        
        println!("{}", deserialized_database.get("k").unwrap().ip);
    
        Ok(())
    }
    
    opened by StyMaar 3
  • Deserialization doesn't support Serde alternative enum representations

    Deserialization doesn't support Serde alternative enum representations

    Serde allows different enum representations: Internally tagged, Adjacently tagged and Untagged in addition to the default Externally tagged representation see Enum representations in Serde's doc.

    Serde gura properly support seralization of such enum representation, but fails to perform deserialization of such representation.

    Example:

    Internally tagged

    
    use serde::{Deserialize, Serialize};
    use serde_gura::Result;
    
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    #[serde(rename_all = "lowercase",tag = "type")]
    enum MyEnum{
        Database(Database)
    }
    
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Database {
        ip: String,
        port: Vec<u16>,
        connection_max: u32,
        enabled: bool,
    }
    
    fn main() -> Result<()> {
    
    
        // You have some type.
        let database = MyEnum::Database(Database {
            ip: "127.0.0.1".to_string(),
            port: vec![80, 8080],
            connection_max: 1200,
            enabled: true,
        });
    
        let sss_str = serde_gura::to_string(&database)?;
        
        println!("{}", &sss_str);
     
    
        // Deserialize it back to a Rust type
        let deserialized_database: MyEnum = serde_gura::from_str(&sss_str)?;
        
        println!("{:#?}", deserialized_database);
    
        Ok(())
    }
    
    type: "database"
    ip: "127.0.0.1"
    port: [80, 8080]
    connection_max: 1200
    enabled: true
    Error: ExpectedIdentifier
    

    Untagged

    use serde::{Deserialize, Serialize};
    use serde_gura::Result;
    
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    #[serde(rename_all = "lowercase",untagged)]
    enum MyEnum{
        Database(Database)
    }
    
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Database {
        ip: String,
        port: Vec<u16>,
        connection_max: u32,
        enabled: bool,
    }
    
    fn main() -> Result<()> {
    
    
        // You have some type.
        let database = MyEnum::Database(Database {
            ip: "127.0.0.1".to_string(),
            port: vec![80, 8080],
            connection_max: 1200,
            enabled: true,
        });
    
        let sss_str = serde_gura::to_string(&database)?;
        
        println!("{}", &sss_str);
     
    
        // Deserialize it back to a Rust type
        let deserialized_database: MyEnum = serde_gura::from_str(&sss_str)?;
        
        println!("{:#?}", deserialized_database);
    
        Ok(())
    }
    

    output

    ip: "127.0.0.1"
    port: [80, 8080]
    connection_max: 1200
    enabled: true
    Error: InvalidType
    

    Adjacently tagged

    
    use serde::{Deserialize, Serialize};
    use serde_gura::Result;
    
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    #[serde(rename_all = "lowercase",tag = "t", content = "c")]
    enum MyEnum{
        Database(Database)
    }
    
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Database {
        ip: String,
        port: Vec<u16>,
        connection_max: u32,
        enabled: bool,
    }
    
    fn main() -> Result<()> {
    
    
        // You have some type.
        let database = MyEnum::Database(Database {
            ip: "127.0.0.1".to_string(),
            port: vec![80, 8080],
            connection_max: 1200,
            enabled: true,
        });
    
        let sss_str = serde_gura::to_string(&database)?;
        
        println!("{}", &sss_str);
     
    
        // Deserialize it back to a Rust type
        let deserialized_database: MyEnum = serde_gura::from_str(&sss_str)?;
        
        println!("{:#?}", deserialized_database);
    
        Ok(())
    }
    

    output

    t: "database"
    c:
        ip: "127.0.0.1"
        port: [80, 8080]
        connection_max: 1200
        enabled: true
    Error: ExpectedIdentifier
    
    opened by StyMaar 2
Owner
Gura Config Lang
Gura configuration language by JWare
Gura Config Lang
A simple, fast and fully-typed JSPaste API wrapper for Rust

rspaste A simple, fast and fully-typed JSPaste API wrapper for Rust. aidak.tk » Installation Put the desired version of the crate into the dependencie

Aidak 2 May 17, 2022
A stringly-typed Error that includes `#[track_caller]` information.

A stringly-typed Error that includes #[track_caller] information.

null 5 Oct 7, 2022
A highly modular Bitcoin Lightning library written in Rust. Its Rust-Lightning, not Rusty's Lightning!

Rust-Lightning is a Bitcoin Lightning library written in Rust. The main crate, lightning, does not handle networking, persistence, or any other I/O. Thus, it is runtime-agnostic, but users must implement basic networking logic, chain interactions, and disk storage. More information is available in the About section.

Lightning Dev Kit 850 Jan 3, 2023
Rust library that can be reset if you think it's slow

GoodbyeKT Rust library that can be reset if you think it's slow

null 39 Jun 16, 2022
Notion Offical API client library for rust

Notion API client library for rust.

Jake Swenson 65 Dec 26, 2022
Rust library for program synthesis of string transformations from input-output examples 🔮

Synox implements program synthesis of string transformations from input-output examples. Perhaps the most well-known use of string program synthesis in end-user programs is the Flash Fill feature in Excel. These string transformations are learned from input-output examples.

Anish Athalye 21 Apr 27, 2022
SE3 Rust library for Robotics

Algebraic Robots A small Rust Library for SE3 Supported: Twist Screw SE3 Group se3 algebra Adjoint SE3 Twist Chains Wrenches Future plans: Jacobians V

Pau Carré Cardona 4 Jul 18, 2021
Rust library for emulating RISC-V rv32imac

This library can execute instructions against any memory and register file that implements the required primitives in the traits lib_rv32::traits::{Memory, RegisterFile}. This is to encourage usage with whatever frontend you desire.

Trevor McKay 14 Dec 7, 2022
Yet another ROS2 client library written in Rust

RclRust Target CI Status Document Foxy (Ubuntu 20.04) Introduction This is yet another ROS2 client library written in Rust. I have implemented it inde

rclrust 42 Dec 1, 2022
A boiler plate code to create dynamic link library in rust.

?? rust-dll-bp This is a boiler plate code that will be generated as a dll binary. I personally cache this here for me but if you're intend to create

s3pt3mb3r 9 Nov 7, 2022