xdiff-live 0.1.1

A live diff tool for comparing files and directories.
Documentation
# XDiff-NG: HTTP Request Comparison and Building Tools

[![Rust](https://img.shields.io/badge/rust-1.70+-orange.svg)](https://www.rust-lang.org)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

XDiff-NG is a powerful command-line toolkit for HTTP request comparison and construction, written in Rust. It provides two main utilities:

- **xdiff**: A sophisticated diff tool for comparing HTTP requests and responses. Perfect for comparing differences between production and staging environments, or different versions of the same API.
- **xreq**: A flexible HTTP request builder based on predefined profiles. An excellent replacement for curl/httpie when dealing with complex HTTP requests.

## Features

- 🔍 **Smart HTTP Comparison**: Compare requests and responses with configurable skip rules
- 🎨 **Syntax Highlighting**: Beautiful colored output for better readability  
- ⚙️ **Flexible Configuration**: YAML-based configuration with profile support
- 🚀 **High Performance**: Built with Rust for speed and reliability
- 📝 **Rich Output Formats**: Support for various output formats and highlighting
- 🔧 **Extensible**: Easy to extend with custom profiles and configurations

## Installation

### From Source

```bash
git clone https://github.com/YanQD/xdiff_ng.git
cd xdiff_ng
cargo build --release
```

The binaries will be available in `target/release/`:
- `xdiff-live`
- `xreq-live`

### Using Cargo

```bash
cargo install --path .
```

## Quick Start

### 1. Using xdiff

**Basic comparison:**
```bash
# Compare two API endpoints using a predefined profile
cargo run --bin xdiff-live -- run -p rust -c fixtures/test.yml

# Compare with extra parameters
cargo run --bin xdiff-live -- run -p rust -c fixtures/test.yml -e a=100 -e @b=2 -e m=10
```

**Interactive mode:**
```bash
# Parse and compare URLs interactively
cargo run --bin xdiff-live -- parse
# Then input URLs like:
# https://jsonplaceholder.typicode.com/todos/1?a=1&b=2
# https://jsonplaceholder.typicode.com/todos/2?a=2&b=3
# todo
```

### 2. Using xreq

**Make HTTP requests:**
```bash
# Execute a request using a predefined profile
cargo run --bin xreq-live -- run -p todo -c fixtures/xreq_test.yml

# Run with custom parameters
cargo run --bin xreq-live -- run -p api -c config.yml -e token=abc123
```

## XDiff - HTTP Request Comparison Tool

XDiff allows you to compare HTTP requests and responses between different environments or API versions. It's particularly useful for:

- Comparing production vs staging environments
- Validating API changes between versions
- Debugging differences in HTTP responses
- Testing API compatibility

### Configuration

XDiff uses YAML configuration files to define comparison profiles. Each profile contains:

- **Request definitions**: Two requests to compare (method, URL, headers, parameters, body)
- **Response filtering**: Rules for skipping certain headers or body fields during comparison

**Example configuration:**

```yaml
---
# Profile for comparing Rust website responses
rust:
  req1:
    method: GET
    url: https://www.rust-lang.org/
    headers:
        user-agent: Aloha
    params:
      hello: world
  req2:
    method: GET
    url: https://www.rust-lang.org/
    params: {}
  res:
    skip_headers:
      - set-cookie
      - date
      - via
      - x-amz-cf-id
    skip_body:
      - timestamp
      - request_id

# Profile for comparing JSON API responses  
todo:
  req1:
    url: https://jsonplaceholder.typicode.com/todos/1
    params: 
      a: 100
  req2:
    url: https://jsonplaceholder.typicode.com/todos/2
    params:
      c: 200
  res:
    skip_headers:
      - report-to
      - date
      - x-ratelimit-remaining
      - x-ratelimit-reset
      - cf-ray
      - age
    skip_body:
      - id
```

### Command Line Options

```bash
xdiff-live <COMMAND>

Commands:
  run    Run diff comparison with specified profile
  parse  Interactive mode for parsing and comparing URLs
  help   Print this message or the help of the given subcommand(s)

Options for 'run':
  -p, --profile <PROFILE>    Profile name to use
  -c, --config <CONFIG>      Configuration file path
  -e, --extra <EXTRA>        Extra parameters (format: key=value or @key=value)
```

### Extra Parameters

You can override or add parameters using the `-e` flag:

- `-e key=value`: Add/override query parameter
- `-e @key=value`: Add/override header
- `-e #key=value`: Add/override body parameter

## XReq - HTTP Request Builder

XReq is a powerful HTTP client that builds requests based on predefined profiles. It's designed to replace curl or httpie for complex HTTP requests.

### Features

- **Profile-based requests**: Define reusable request templates
- **Parameter substitution**: Dynamic parameter injection
- **Rich output**: Syntax highlighted response display
- **Header management**: Easy header customization
- **Body templating**: Support for various body formats

### Configuration

```yaml
---
# API testing profile
api:
  method: POST
  url: https://api.example.com/users
  headers:
    content-type: application/json
    authorization: Bearer {{token}}
  body:
    name: "{{name}}"
    email: "{{email}}"

# Simple GET request profile    
todo:
  method: GET
  url: https://jsonplaceholder.typicode.com/todos/{{id}}
  params:
    _limit: 10
```

### Command Line Usage

```bash
xreq-live run -p <PROFILE> -c <CONFIG> [OPTIONS]

Options:
  -p, --profile <PROFILE>    Profile name to use
  -c, --config <CONFIG>      Configuration file path  
  -e, --extra <EXTRA>        Extra parameters for substitution
```

## Configuration File Format

Both tools use YAML configuration files. See [yaml.md](yaml.md) for detailed YAML syntax reference.

### Profile Structure

```yaml
---
profile_name:
  method: GET|POST|PUT|DELETE|PATCH    # HTTP method (default: GET)
  url: string                          # Request URL (required)
  headers:                             # Request headers
    key: value
  params:                              # Query parameters  
    key: value
  body:                                # Request body (for POST/PUT/PATCH)
    key: value
  res:                                 # Response filtering (xdiff only)
    skip_headers:                      # Headers to ignore in comparison
      - header-name
    skip_body:                         # Body fields to ignore in comparison
      - field-name
```

## Examples

### 1. Comparing API Endpoints

Compare two different API endpoints:

```bash
# Compare todo items from JSONPlaceholder API
cargo run --bin xdiff-live -- run -p todo -c fixtures/test.yml
```

### 2. Environment Comparison

Compare the same endpoint across different environments:

```yaml
---
api_comparison:
  req1:
    url: https://staging-api.example.com/users
    headers:
      authorization: Bearer staging-token
  req2:
    url: https://prod-api.example.com/users  
    headers:
      authorization: Bearer prod-token
  res:
    skip_headers:
      - date
      - server
      - x-request-id
```

### 3. Version Comparison

Compare different API versions:

```bash
# Compare v1 vs v2 API responses
cargo run --bin xdiff-live -- run -p version_compare -c api_versions.yml
```

### 4. Making HTTP Requests with xreq

```bash
# Make a simple GET request
cargo run --bin xreq-live -- run -p todo -c fixtures/xreq_test.yml

# POST request with parameters
cargo run --bin xreq-live -- run -p create_user -c api.yml -e name="John Doe" -e email="john@example.com"
```

## Development

### Prerequisites

- Rust 1.70 or later
- Cargo

### Building

```bash
# Debug build
cargo build

# Release build  
cargo build --release

# Run tests
cargo test

# Run with examples
cargo run --example highlight
cargo run --example similar
```

### Project Structure

```
├── src/
│   ├── bin/          # Binary entry points
│   │   ├── xdiff.rs  # xdiff CLI
│   │   └── xreq.rs   # xreq CLI  
│   ├── config/       # Configuration handling
│   ├── cli.rs        # Command line interface
│   ├── lib.rs        # Library exports
│   └── utils.rs      # Utility functions
├── examples/         # Example code
├── fixtures/         # Test configuration files
├── tests/           # Integration tests
└── README.md        # This file
```

### Running Tests

```bash
# Run all tests
cargo test

# Run specific test
cargo test cli_tests

# Run with verbose output
cargo test -- --nocapture
```

## Contributing

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Acknowledgments

- Built with [Rust]https://www.rust-lang.org/
- HTTP client powered by [reqwest]https://github.com/seanmonstar/reqwest
- Text diffing by [similar]https://github.com/mitsuhiko/similar  
- Syntax highlighting by [syntect]https://github.com/trishume/syntect
- CLI framework by [clap]https://github.com/clap-rs/clap

## Changelog

### v0.1.0
- Initial release
- Basic xdiff functionality for HTTP request comparison
- xreq tool for making HTTP requests from profiles  
- YAML configuration support
- Syntax highlighting for output
- Interactive parsing mode