Rust library for string parsing of basic data structures.

Related tags

Data structures afmt
Overview

afmt

Simple rust library for parsing basic data structures from strings.

Usage

You can specify string formats to any strucute, via the use of the fmt macro, in which you can specify a chain of string literals and struct member names, to represent the format of a structure.

#[fmt("value: " v "--" f)]
struct Foo {
    v: u32,
    r: f64,
}

let f: Foo = "value: 65--3.14".parse()?;

Limitations

Consider we want to parse strings similar to

some text here 364

into a struct with a String first part and a u32 second part.

If we tried parsing it as the following format attribute

#[fmt(msg v)]
struct Foo {
    msg: String,
    v: u32,
}

the result would be ambiguous, multiple interpretations could be

Foo {
    msg: "some text here ",
    v: 364,
}
Foo {
    msg: "some text here 3",
    v: 64,
}
Foo {
    msg: "some text here 36",
    v: 4,
}
            ...

this means that we must specify some literal delimiter between every pair of capture variables. Literal delimiters are parsed in a way that the first match splits the string, so a delimiter of " " would result in the two parts beign "some" and "text here 364", which is not ideal.

For this reason, you should consider which strings you can easily parse, by considering special delimiters, i.e. delimiters should not appear in data you want to capture.

Examples

#[macro_use] extern crate afmt;
#[fmt("value :" v)]
struct Foo {
    v: u32,
}

#[test]
fn it_works() {
    let f: Foo = "value: 65".parse().unwrap();
    assert_eq!(f.v, 65);
}
#[fmt("x[" x "] y[" y "]")]
struct Point {
    x: u32,
    y: u32,
}

#[test]
fn it_works() {
    let p: Point = "x[65] y[39]".parse().unwrap();
    assert_eq!(p.x, 65);
    assert_eq!(p.y, 39);
}
= "WARN: this is a warning".parse(); assert!(b.is_err()); } ">
#[fmt("INFO: " msg)]
struct Bar {
    msg: String,
}

#[test]
fn it_works() {
    let b: Result<Bar,_> = "WARN: this is a warning".parse();
    assert!(b.is_err());
}
You might also like...
Succinct data structures in Rust

sucds: Succinct data structures in Rust sucds contains some succinct data structures written in Rust. Data structures So far, the following data struc

Coding-challenge - Algorithms and Data-structures, problems and solutions in Rust language using cargo-workspaces

Coding Challenge LeetCode/Hackerrank e.t.c Using this as an opportunity to improve my knowledge of rust lang If you found this repo useful to you, add

Common data structures and algorithms in Rust

Contest Algorithms in Rust A collection of classic data structures and algorithms, emphasizing usability, beauty and clarity over full generality. As

Rust data structures and client for the PubChem REST API

pubchem.rs Rust data structures and client for the PubChem REST API. πŸ”Œ Usage πŸ’Š Compound Create a Compound to query the PubChem API for a single comp

Dade is data definition for Rust structures.

dade dade is data definition for Rust structures. For the easy handle of data, the following will support it. Data validation. Data schema conforms Js

rust_aads - Rust Algorithms And Data Structures

rust_aads - Rust Algorithms And Data Structures rust_aads is an open repository with algorithms and data structures, used in computer science and comp

This repository aims to organize codes related to data structures in Rust. πŸ¦€
This repository aims to organize codes related to data structures in Rust. πŸ¦€

Rust Data Structure A project with the objective of introducing the main concepts about data structure using Rust! Explore the docs and learn Rust Β» R

Obake is a procedural macro for declaring and maintaining versioned data-structures.

Obake is a procedural macro for declaring and maintaining versioned data-structures. The name 'obake' is taken from the Japanese 'γŠεŒ–γ‘ (γŠγ°γ‘)', a class of supernatural beings in Japanese folklore that shapeshift.

Garbage Collector(Hyaline- Safe Memory Reclaimation) for lock free data structures

Hyaline-SMR This crate provides garbage collection using hyaline algorithm for building concurrent data structures. When a thread removes an object fr

Comments
  • Rename fmt to parse

    Rename fmt to parse

    I was very confused in the first place, that afmt::fmt actually is meant for parsing. When I first saw fmt I thought it would implement std::fmt::Display and I thought, hey! That's neat!

    But only after looking at the sources (and reading the doc πŸ™ƒ) I saw it's actually meant for parsing a string into a struct.

    Maybe consider renaming the struct? Next step would be to have a fmt macro that implements std::fmt::Display? Because that would be sick as well!

    opened by hellow554 1
  • use Format strings instead

    use Format strings instead

    Currently your Syntax is

    #[fmt("..." V "...")]

    And for me this is so confusing, because you have to open, close, open, close, open, .... The string a lot!

    My proposal is that you should be able to use Format strings, e.g.

    "... {}...{}...{}", x, y, z
    

    Or inspired by Python

    "...{x}...{y}...{z}"
    

    What do you think?

    opened by hellow554 0
Owner
Eduard
Eduard
A library for comparing data structures in Rust, oriented toward testing

Delta: Structural differencing in Rust The delta crate defines the trait Delta, along with a derive macro for auto-generating instances of this trait

John Wiegley 19 Oct 7, 2022
A library for comparing data structures in Rust, oriented toward testing

The comparable crate defines the trait [Comparable], along with a derive macro for auto-generating instances of this trait for most data types. Primar

John Wiegley 19 Oct 7, 2022
NixEl is a Rust library that turns Nix code into a variety of correct, typed, memory-safe data-structures

?? NixEL Lexer, Parser, Abstract Syntax Tree and Concrete Syntax Tree for the Nix Expressions Language. NixEl is a Rust library that turns Nix code in

Kevin Amado 56 Dec 29, 2022
Library containing implementations of various sequential data-structures.

Library containing implementations of various sequential data-structures.

c1m50c 0 May 15, 2022
Rust-algorithm-club - Learn algorithms and data structures with Rust

Rust Algorithm Club ?? ?? This repo is under construction. Most materials are written in Chinese. Check it out here if you are able to read Chinese. W

Weihang Lo 360 Dec 28, 2022
Rust Persistent Data Structures

Rust Persistent Data Structures Rust Persistent Data Structures provides fully persistent data structures with structural sharing. Setup To use rpds a

Diogo Sousa 883 Dec 31, 2022
A proof of concept implementation of cyclic data structures in stable, safe, Rust.

A proof of concept implementation of cyclic data structures in stable, safe, Rust. This demonstrates the combined power of the static-rc crate and the

null 157 Dec 28, 2022
Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.

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

null 6.5k Dec 31, 2022
Algorithms and Data Structures of all kinds written in Rust.

Classic Algorithms in Rust This repo contains the implementation of various classic algorithms for educational purposes in Rust. Right now, it is in i

Alexander GonzΓ‘lez 49 Dec 14, 2022
Collection of Data Structures in Rust

cds - Collection of Data Structures !!! UNDER CONSTRUCTION !!! The version v0.0.1 is a crates.io placeholder. License Licensed under either of Apache

Rafael Buchbinder 2 May 23, 2022