Crates.io | parser-on-rust |
lib.rs | parser-on-rust |
version | 0.1.3 |
source | src |
created_at | 2023-10-18 08:00:00.170394 |
updated_at | 2023-11-14 19:18:02.625357 |
description | url parser on rust |
homepage | |
repository | |
max_upload_size | |
id | 1006499 |
size | 15,984 |
This project implements a URL parser in Rust using the pest
parser generator. The parser is designed to handle various components of a URL, including the scheme, host, port, path, query parameters, and fragment.
To use parser in your Rust project, add it as a dependency in your Cargo.toml file:
[dependencies]
parser_on_rust = "0.1.3"
Select the current version on crates.io: parser_on_rust
First, import the library in your Rust code:
use parser_on_rust::parse_url;
Then, you can convert Markdown text to HTML using the parse_markdown function:
fn main() {
let url_string = "https://www.example.com/path?query=some";
match parse_url(url_string) {
Ok(parsed_url) => println!("Successfully parsed URL: {:?}", parsed_url),
Err(err) => eprintln!("Error parsing URL: {}", err),
}
}
Flexible URL Parsing: The parser supports a wide range of URL formats, allowing for flexibility in the structure of the provided URLs.
Optional Components: Components like path, query parameters, and fragments are optional, providing versatility in handling URLs with or without these elements.
Error Handling: Proper error handling is implemented using the Result
type, ensuring graceful handling of parsing errors.
The project is structured as follows:
src/lib.rs
: Contains the implementation of the URL parser, including the grammar rules and parsing logic.
src/main.rs
: A simple executable that demonstrates the usage of the parser by parsing example URLs.
tests/tests.rs
: Unit tests for various scenarios, ensuring the correctness of the URL parsing logic.
Cargo.toml
: The project configuration file specifying dependencies and other metadata.
To run the project, execute the following command:
cargo run
This will execute the main.rs file, demonstrating the parsing of example URLs.
The project includes a comprehensive set of unit tests in the tests.rs file. To run the tests, use the following command:
cargo test
The project relies on the pest crate for parsing and pest_derive for procedural macro support. Ensure these dependencies are correctly specified in the Cargo.toml file.
[dependencies]
pest = "2.7.5"
pest_derive = "2.7.5"
url = { scheme ~ "://" ~ host ~ (":" ~ port)? ~ (path ~ ("/" ~ file)? ~ ("?" ~ query)? ~ ("#" ~ fragment)?)? }
scheme = { identifier_with_optional_dot }
host = { identifier_with_optional_dot }
port = { number }
path = { "/" ~ identifier ~ ("/" ~ identifier)* }
query = { identifier ~ ("=" ~ identifier)* }
fragment = { identifier }
file = { identifier ~ ("." ~ identifier)* }
identifier = { ASCII_ALPHANUMERIC+ }
identifier_with_optional_dot = { identifier ~ ( "." ~ ASCII_ALPHANUMERIC+)* }
number = { DIGIT+ }
WHITESPACE = _{ " " | "\n" | "\t" }
ASCII_ALPHANUMERIC = _{ ASCII_ALPHABET | DIGIT }
ASCII_ALPHABET = _{ 'a'..'z' | 'A'..'Z' }
DIGIT = _{ '0'..'9' }
Additional URL Components: Extend the parser to support additional URL components if needed.
User Input Handling: Implement a more robust user input handling mechanism, allowing users to input URLs interactively.
Error Messages: Enhance error messages to provide more detailed information about parsing failures.
Command-Line Interface (CLI): The project includes a simple command-line interface allowing users to input URLs for parsing.