Code Monkey home page Code Monkey logo

reformation's Introduction

Build Status

reformation

Parsing via regular expressions using format syntax

Deriving trait Reformation will also implement trait FromStr, with Err=Box<Error>

Derive will require attribute reformation to specify format string, which will be treated as format string -> regular expression string

Types implementing Reformation by default:

  • signed integers: i8 i16 i32 i64 i128 isize
  • unsigned integers: u8 u16 u32 u64 u128 usize
  • floats: f32 f64
  • String
use reformation::Reformation;

#[derive(Reformation)]
#[reformation(r"{year}-{month}-{day} {hour}:{minute}")]
struct Date{
    year: u16,
    month: u8,
    day: u8,
    hour: u8,
    minute: u8,
}

fn main(){
    let date: Date = "2018-12-22 20:23".parse().unwrap();

    assert_eq!(date.year, 2018);
    assert_eq!(date.month, 12);
    assert_eq!(date.day, 22);
    assert_eq!(date.hour, 20);
    assert_eq!(date.minute, 23);
}

Format string behaves as regular expression, so special symbols needs to be escaped. Also they can be used for more flexible format strings. AVOID capture groups, since they would mess up with indexing of capture group generated by macro. use non-capturing groups r"(?:)" instead.

use reformation::Reformation;

// '{' is special symbol in both format and regex syntax, so it must be escaped twice.
// Say hello to good old escape hell.
#[derive(Reformation)]
#[reformation(r"Vec\{{{x},\s*{y},\s*{z}\}}")]
struct Vec{
    x: f64,
    y: f64,
    z: f64,
}

fn main(){
    // spaces between coordinates does not matter, since any amount of spaces
    // matches to r"\s*"
    let v: Vec = "Vec{-0.4,1e-3,   2e-3}".parse().unwrap();

    assert_eq!(v.x, -0.4);
    assert_eq!(v.y, 0.001);
    assert_eq!(v.z, 0.002);
}

reformation's People

Contributors

hukumka avatar

Watchers

 avatar  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.