| Crates.io | parse_rust |
| lib.rs | parse_rust |
| version | 0.1.0 |
| created_at | 2024-12-27 22:49:39.683726+00 |
| updated_at | 2024-12-27 22:49:39.683726+00 |
| description | A Rust implementation of Python's parse library for string parsing and pattern matching |
| homepage | https://github.com/lucien2k/parse_rust |
| repository | https://github.com/lucien2k/parse_rust |
| max_upload_size | |
| id | 1496710 |
| size | 61,253 |
A Rust implementation of Python's parse library, providing a flexible way to parse strings using simple, human-readable format strings.
str.format()The library supports various datetime format specifiers:
:tg - Generic date/time format
27/12/2024 19:57:55
27/12/2024 07:57:55 PM
2024/12/27 19:57:55
27/12/2024
19:57:55
:ta - American date/time format
12/27/2024 07:57:55 PM
12/27/2024 19:57:55
12/27/2024
:te - Email date/time format (RFC 2822)
Fri, 27 Dec 2024 19:57:55 +0000
27 Dec 2024 19:57:55 +0000
27 Dec 2024
:th - HTTP log date/time format
27/Dec/2024:19:57:55 +0000
:ts - System log date/time format
Dec 27 2024 19:57:55
:ti - ISO 8601 date/time format
2024-12-27T19:57:55.000+00:00
2024-12-27T19:57:55+00:00
2024-12-27T19:57:55.000
2024-12-27T19:57:55
2024-12-27
use parse_rust::Parser;
// Parse a simple string with an integer
let p = Parser::new("Value is {:d}", true).unwrap();
let result = p.parse("Value is 42").unwrap();
let value: &i64 = result.get(0).unwrap();
assert_eq!(*value, 42);
// Parse a datetime string
let p = Parser::new("Time: {:tg}", true).unwrap();
let result = p.parse("Time: 27/12/2024 19:57:55").unwrap();
let dt: &chrono::NaiveDateTime = result.get(0).unwrap();
assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2024-12-27 19:57:55");
// Parse with named fields
let p = Parser::new("Name: {name:w}, Age: {age:d}", true).unwrap();
let result = p.parse("Name: John, Age: 30").unwrap();
let name: &str = result.named("name").unwrap();
let age: &i64 = result.named("age").unwrap();
assert_eq!(name, "John");
assert_eq!(*age, 30);
Add this to your Cargo.toml:
[dependencies]
parse_rust = "0.1.0"
regex - For pattern matchingchrono - For date and time parsingthiserror - For error handlinglazy_static - For static initializationThis project is licensed under the MIT License - see the LICENSE file for details.