6 releases

new 0.2.0 Apr 23, 2024
0.1.4 Apr 11, 2024

#1109 in Parser implementations

Download history 176/week @ 2024-03-29 490/week @ 2024-04-05 168/week @ 2024-04-12

834 downloads per month
Used in rede

Apache-2.0 OR MIT

33KB
799 lines

Rede Parser

Library crate to receive the content of Rede's files and generate the requests or environments to use in the command-line binary. It can be used on its own to implement Rede's format in other projects or implementations.

Usage

The library offers the function rede_parser::parse_request to convert a given string into a valid rede_parser::Request.

let toml = r#"
[http]
method = "POST"
url = "http://localhost:8080/note"

[headers]
Content-Type = "application/json"

[body]
raw = """
{
  "title": "Implement rede_parser" ,
  "description": "Implement it following the example
}
"""
"#;

let request = rede_parser::parse_request(toml)?;
assert_eq!(request.method, Method::POST);
assert_eq!(request.url, "http://localhost:8080/note");
assert_eq!(request.headers["Content-Type"], "application/json");
if let Body::Raw { content, mime } = &request.body {
  assert_eq!(mime, &"text/plain; charset=utf-8");
  println!("{}", &request.body);
}

lib.rs:

Library to handle the parsing of requests in TOML format used by the crate rede.

The library offers the function rede_parser::parse_request to convert a given string into a valid rede_parser::Request.

Example

Pass the correct request TOML to the parser functions to get the parsed request:


let toml = r#"
    [http]
    method = "POST"
    url = "http://localhost:8080/note"

    [headers]
    Content-Type = "application/json"

    [body]
    raw = """
    {
        "title": "Implement rede_parser" ,
        "description": "Implement it following the example
    }
    """
 "#;
 let request = rede_parser::parse_request(toml)?;
 assert_eq!(request.method, Method::POST);
 assert_eq!(request.url, "http://localhost:8080/note");
 assert_eq!(request.headers["Content-Type"], "application/json");
 if let Body::Raw { content, mime } = &request.body {
    assert_eq!(mime, &"text/plain; charset=utf-8");
    println!("{}", &request.body);
 }
 # Ok(())

Dependencies

~1.2–1.9MB
~39K SLoC