parse_rust
A Rust implementation of Python's parse library, providing a flexible way to parse strings using simple, human-readable format strings.
Features
- Parse strings using format strings similar to Python's
str.format() - Extract typed values from strings
- Support for various data types:
- Integers
- Floats
- Words (strings)
- Custom types
- Date and Time formats
Date and Time Format Specifiers
The library supports various datetime format specifiers:
-
:tg- Generic date/time format27/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 format12/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 format27/Dec/2024:19:57:55 +0000 -
:ts- System log date/time formatDec 27 2024 19:57:55 -
:ti- ISO 8601 date/time format2024-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
Usage
use Parser;
// Parse a simple string with an integer
let p = new.unwrap;
let result = p.parse.unwrap;
let value: &i64 = result.get.unwrap;
assert_eq!;
// Parse a datetime string
let p = new.unwrap;
let result = p.parse.unwrap;
let dt: &NaiveDateTime = result.get.unwrap;
assert_eq!;
// Parse with named fields
let p = new.unwrap;
let result = p.parse.unwrap;
let name: &str = result.named.unwrap;
let age: &i64 = result.named.unwrap;
assert_eq!;
assert_eq!;
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Dependencies
regex- For pattern matchingchrono- For date and time parsingthiserror- For error handlinglazy_static- For static initialization
License
This project is licensed under the MIT License - see the LICENSE file for details.