Skip to main content

xdiff_live/
lib.rs

1//! # XDiff-NG: HTTP Request Comparison and Building Tools
2//!
3//! XDiff-NG is a powerful command-line toolkit for HTTP request comparison and construction.
4//! It provides two main utilities:
5//!
6//! - **xdiff**: A sophisticated diff tool for comparing HTTP requests and responses
7//! - **xreq**: A flexible HTTP request builder based on predefined profiles
8//!
9//! ## Features
10//!
11//! - Smart HTTP comparison with configurable skip rules
12//! - Syntax highlighting for better readability
13//! - YAML-based configuration with profile support
14//! - High performance built with Rust
15//! - Rich output formats and highlighting
16//! - Extensible with custom profiles and configurations
17//!
18//! ## Example Usage
19//!
20//! ```no_run
21//! use xdiff_live::{DiffConfig, ExtraArgs};
22//!
23//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
24//! // Load configuration from YAML file
25//! let config = DiffConfig::load_yaml("config.yml").await?;
26//!
27//! // Get a specific profile
28//! let profile = config.get_profile("api_comparison").unwrap();
29//!
30//! // Create extra arguments for parameter override
31//! let extra_args = ExtraArgs::new_with_query(vec![
32//!     ("param1".to_string(), "value1".to_string()),
33//! ]);
34//!
35//! // Perform the comparison
36//! let diff_result = profile.diff(extra_args).await?;
37//! println!("{}", diff_result);
38//! # Ok(())
39//! # }
40//! ```
41
42pub mod cli;
43pub mod config;
44mod utils;
45
46pub use config::{
47    get_body_text, get_headers_text, get_status_text, is_default, DiffConfig, DiffProfile,
48    LoadConfig, RequestConfig, RequestProfile, ResponseProfile, ValidateConfig,
49};
50pub use utils::{diff_text, highlight_text, process_error_output};
51
52/// Extra arguments for HTTP requests that can override or supplement
53/// the base configuration from profiles.
54///
55/// This structure allows users to provide additional headers, query parameters,
56/// and body parameters that will be merged with the profile configuration.
57///
58/// # Examples
59///
60/// ```
61/// use xdiff_live::ExtraArgs;
62///
63/// // Create extra args with custom headers
64/// let extra = ExtraArgs::new_with_headers(vec![
65///     ("Authorization".to_string(), "Bearer token123".to_string()),
66///     ("User-Agent".to_string(), "MyApp/1.0".to_string()),
67/// ]);
68///
69/// // Create extra args with query parameters
70/// let extra = ExtraArgs::new_with_query(vec![
71///     ("page".to_string(), "1".to_string()),
72///     ("limit".to_string(), "50".to_string()),
73/// ]);
74/// ```
75#[derive(Debug, Default, Clone, PartialEq, Eq)]
76pub struct ExtraArgs {
77    /// Additional HTTP headers to include in the request
78    pub headers: Vec<(String, String)>,
79    /// Additional query parameters to include in the request URL
80    pub query: Vec<(String, String)>,
81    /// Additional body parameters to include in the request body
82    pub body: Vec<(String, String)>,
83}
84
85impl ExtraArgs {
86    /// Creates a new `ExtraArgs` instance with only headers specified.
87    ///
88    /// # Arguments
89    ///
90    /// * `headers` - A vector of key-value pairs representing HTTP headers
91    ///
92    /// # Examples
93    ///
94    /// ```
95    /// use xdiff_live::ExtraArgs;
96    ///
97    /// let extra = ExtraArgs::new_with_headers(vec![
98    ///     ("Content-Type".to_string(), "application/json".to_string()),
99    /// ]);
100    /// ```
101    pub fn new_with_headers(headers: Vec<(String, String)>) -> Self {
102        Self {
103            headers,
104            ..Default::default()
105        }
106    }
107
108    /// Creates a new `ExtraArgs` instance with only query parameters specified.
109    ///
110    /// # Arguments
111    ///
112    /// * `query` - A vector of key-value pairs representing query parameters
113    ///
114    /// # Examples
115    ///
116    /// ```
117    /// use xdiff_live::ExtraArgs;
118    ///
119    /// let extra = ExtraArgs::new_with_query(vec![
120    ///     ("search".to_string(), "rust".to_string()),
121    ///     ("page".to_string(), "1".to_string()),
122    /// ]);
123    /// ```
124    pub fn new_with_query(query: Vec<(String, String)>) -> Self {
125        Self {
126            query,
127            ..Default::default()
128        }
129    }
130
131    /// Creates a new `ExtraArgs` instance with only body parameters specified.
132    ///
133    /// # Arguments
134    ///
135    /// * `body` - A vector of key-value pairs representing body parameters
136    ///
137    /// # Examples
138    ///
139    /// ```
140    /// use xdiff_live::ExtraArgs;
141    ///
142    /// let extra = ExtraArgs::new_with_body(vec![
143    ///     ("username".to_string(), "john_doe".to_string()),
144    ///     ("email".to_string(), "john@example.com".to_string()),
145    /// ]);
146    /// ```
147    pub fn new_with_body(body: Vec<(String, String)>) -> Self {
148        Self {
149            body,
150            ..Default::default()
151        }
152    }
153}