curl_parser/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! A parser for converting curl commands into structured request objects.
//!
//! This crate provides functionality to parse curl command strings into a structured [`ParsedRequest`]
//! that can be used programmatically. It's particularly useful for:
//!
//! - Converting curl commands into programmatic HTTP requests
//! - Testing and debugging HTTP clients
//! - Generating code from curl examples
//!
//! # Design Goals
//!
//! The main goals of this crate are:
//!
//! - **Accuracy**: Faithfully parse curl commands while preserving all relevant HTTP request details
//! - **Ergonomics**: Provide a simple, intuitive API for parsing and using the results
//! - **Flexibility**: Support common curl options while allowing for future extensibility
//! - **Safety**: Handle malformed input gracefully with meaningful errors
//!
//! # Architecture
//!
//! The crate uses a multi-stage parsing approach:
//!
//! 1. **Lexical Analysis**: Uses Pest parser to tokenize the curl command string according to formal grammar rules
//! 2. **Semantic Analysis**: Converts tokens into structured request components (method, headers, etc.)
//! 3. **Request Building**: Assembles the components into a [`ParsedRequest`] struct
//!
//! The parser handles various curl options including:
//! - HTTP methods (`-X`, `--request`)
//! - Headers (`-H`, `--header`)
//! - Request body (`-d`, `--data`)
//! - Authentication (`-u`)
//! - SSL verification (`-k`, `--insecure`)
//! - URL redirection (`-L`, `--location`)
//!
//! # Examples
//!
//! Basic GET request:
//!
//! ```
//! use curl_parser::ParsedRequest;
//! use std::str::FromStr;
//! # fn main() -> Result<(), curl_parser::Error> {
//! let curl = r#"curl https://api.example.com/users"#;
//! let request = ParsedRequest::from_str(curl)?;
//! # Ok(())
//! # }
//! ```
//!
//! POST request with headers and body:
//!
//! ```
//! use curl_parser::ParsedRequest;
//! use serde_json::json;
//! # fn main() -> Result<(), curl_parser::Error> {
//! let curl = r#"curl -X POST https://api.example.com/users \
//!     -H 'Content-Type: application/json' \
//!     -H 'Authorization: Bearer {{ token }}' \
//!     -d '{"name": "John Doe", "email": "john@example.com"}'"#;
//! let request = ParsedRequest::load(curl, json!({ "token": "123456" }))?;
//! # Ok(())
//! # }
//! ```
//!
//! Using with reqwest (requires `reqwest` feature):
//!
//! ```
//! # #[cfg(feature = "reqwest")]
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! use curl_parser::ParsedRequest;
//! use serde_json::json;
//!
//! let curl = r#"curl -X POST https://api.example.com/users \
//!     -H 'Authorization: Bearer {{ token }}' \
//!     -d '{"name": "John Doe"}'"#;
//!
//! let parsed = ParsedRequest::load(curl, json!({ "token": "123456" }))?;
//! let request = reqwest::RequestBuilder::try_from(&parsed)?;
//! let response = request.send().await?;
//! # Ok(())
//! # }
//! ```

pub(crate) mod error;
mod parser;

use http::{HeaderMap, Method, Uri};

pub use error::Error;

#[derive(Debug, Clone, Default)]
pub struct ParsedRequest {
    pub method: Method,
    pub url: Uri,
    pub headers: HeaderMap,
    pub body: Vec<String>,
    pub insecure: bool,
}