Skip to content

Theoya/Wargaming-XML-Parser-Rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

13 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Rust XML Parser

A powerful Rust library for parsing XML files, with specialized support for BattleScribe roster files (.rosz, .ros, .cat) and constraint validation. This project provides tools for decompressing, parsing, and validating XML-based game roster files commonly used in tabletop wargaming.

๐ŸŽฏ Features

  • XML Parsing: Robust XML parsing with support for complex nested structures
  • File Decompression: Decompress .rosz files (ZIP-based roster files)
  • Constraint Validation: Validate XML constraints for selection counts, points limits, and more
  • Lexical Analysis: Advanced token parsing and analysis
  • BattleScribe Support: Specialized parsing for BattleScribe roster files
  • Error Handling: Comprehensive error handling with detailed error messages

๐Ÿ“ Project Structure

Rust-XML-Parser/
โ”œโ”€โ”€ Cargo.toml                 # Project configuration and dependencies
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ lib.rs                 # Library entry point
โ”‚   โ”œโ”€โ”€ main.rs                # CLI application entry point
โ”‚   โ”œโ”€โ”€ XmlParser.rs           # Main XML parsing logic
โ”‚   โ”œโ”€โ”€ models/                # Data structures and models
โ”‚   โ”‚   โ”œโ”€โ”€ mod.rs
โ”‚   โ”‚   โ”œโ”€โ”€ Constraint.rs      # Constraint data model
โ”‚   โ”‚   โ”œโ”€โ”€ ConstraintType.rs  # Constraint type definitions
โ”‚   โ”‚   โ”œโ”€โ”€ ValidationResult.rs # Validation result model
โ”‚   โ”‚   โ”œโ”€โ”€ XmlDocument.rs     # XML document model
โ”‚   โ”‚   โ”œโ”€โ”€ XmlElement.rs      # XML element model
โ”‚   โ”‚   โ””โ”€โ”€ XmlNode.rs         # XML node model
โ”‚   โ”œโ”€โ”€ Tools/                 # Core functionality modules
โ”‚   โ”‚   โ”œโ”€โ”€ mod.rs
โ”‚   โ”‚   โ”œโ”€โ”€ decompression.rs   # File decompression utilities
โ”‚   โ”‚   โ”œโ”€โ”€ lexical_analysis.rs # Token parsing and analysis
โ”‚   โ”‚   โ”œโ”€โ”€ parse_tokens.rs    # Token parsing logic
โ”‚   โ”‚   โ””โ”€โ”€ validator.rs       # Constraint validation engine
โ”‚   โ””โ”€โ”€ tests/                 # Test modules
โ”‚       โ”œโ”€โ”€ mod.rs
โ”‚       โ”œโ”€โ”€ decompression_tests.rs
โ”‚       โ”œโ”€โ”€ parse_tokens_tests.rs
โ”‚       โ”œโ”€โ”€ roster_validation_tests.rs
โ”‚       โ””โ”€โ”€ validator_tests.rs
โ”œโ”€โ”€ examples/
โ”‚   โ””โ”€โ”€ validator_example.rs   # Comprehensive validation examples
โ”œโ”€โ”€ docs/
โ”‚   โ””โ”€โ”€ validator_usage.md     # Detailed validator documentation
โ””โ”€โ”€ example-data/              # Sample files for testing
    โ”œโ”€โ”€ Orks.cat
    โ”œโ”€โ”€ Test-Chaos-Thousand Sons.cat
    โ”œโ”€โ”€ Test-sample-army.rosz
    โ”œโ”€โ”€ Tts game teams.ros
    โ””โ”€โ”€ Tts Ork game teams.rosz

๐Ÿš€ Quick Start

Prerequisites

  • Rust 1.70+ (install via rustup)
  • Cargo (comes with Rust)

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/xml-parser.git
cd xml-parser
  1. Build the project:
cargo build

Running the Application

CLI Application

# Run the main CLI application
cargo run

# This will:
# - Decompress example .rosz files
# - Parse XML content
# - Demonstrate constraint validation

Examples

# Run the comprehensive validator example
cargo run --example validator_example

# This demonstrates:
# - Basic constraint validation
# - Selection entry group validation
# - Field-specific validation

Running Tests

# Run all tests
cargo test

# Run specific test modules
cargo test validator
cargo test decompression
cargo test parse_tokens

# Run tests with output
cargo test -- --nocapture

๐Ÿ“– Usage Examples

1. Decompressing .rosz Files

use xml_parser::Tools::decompression;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Decompress a .rosz file
    let files = decompression::decompress_rosz_file("example-data/Tts Ork game teams.rosz")?;
    
    println!("Decompressed {} files:", files.len());
    for file in &files {
        println!("  - {}", file.filename);
        // Process file.content as needed
    }
    
    Ok(())
}

2. Constraint Validation

use xml_parser::Tools::validator::ConstraintValidator;
use xml_parser::models::Constraint::Constraint;
use xml_parser::models::ConstraintType::ConstraintType;

fn main() {
    let mut validator = ConstraintValidator::new();
    
    // Add constraints
    let min_constraint = Constraint {
        constraint_type: ConstraintType::Min,
        value: 2,
        field: "selections".to_string(),
        scope: "parent".to_string(),
        shared: true,
        id: "test-id".to_string(),
        include_child_selections: Some(true),
        include_child_forces: None,
        percent_value: None,
    };
    
    validator.add_constraint(min_constraint);
    
    // Validate selections
    let results = validator.validate_selections(3);
    for result in results {
        if result.is_valid {
            println!("โœ“ {}", result.message);
        } else {
            println!("โœ— {}", result.message);
        }
    }
}

3. XML Parsing and Validation

use xml_parser::Tools::validator::ConstraintValidator;
use xml_parser::models::XmlElement::XmlElement;

fn main() -> Result<(), String> {
    // Parse XML constraints
    let xml_string = r#"
        <constraints>
            <constraint type="min" value="2" field="selections" scope="parent" shared="true" id="c6ca-55be-a638-5f08"/>
            <constraint type="max" value="5" field="selections" scope="parent" shared="true" id="ad81-4838-8c1d-8c1c"/>
        </constraints>
    "#;
    
    let validator = ConstraintValidator::from_xml_string(xml_string)?;
    
    // Validate different selection counts
    let test_values = vec![1, 2, 3, 5, 6];
    for value in test_values {
        let results = validator.validate_selections(value);
        println!("Selection count {}: ", value);
        for result in results {
            let status = if result.is_valid { "โœ“" } else { "โœ—" };
            println!("  {} {}", status, result.message);
        }
    }
    
    Ok(())
}

๐Ÿ”ง Core Components

1. Decompression Module (src/Tools/decompression.rs)

  • Decompresses .rosz files (ZIP-based roster files)

2. Validator Module (src/Tools/validator.rs)

  • Validates XML constraints for selection counts, points limits, etc.
  • Supports constraint types: min, max, equal, notEqual, atLeast, atMost
  • Provides comprehensive validation results with detailed messages in the event something fails

3. Lexical Analysis (src/Tools/lexical_analysis.rs)

  • Parses XML structures

4. Parse Tokens (src/Tools/parse_tokens.rs)

  • Token-based XML parsing to follow up on lexical_analysis

๐Ÿ“‹ Constraint Types

The validator supports the following constraint types:

Type Description Example
min Value must be โ‰ฅ specified value value="2"
max Value must be โ‰ค specified value value="5"
equal Value must be exactly equal value="3"
notEqual Value must not be equal value="0"
atLeast Alias for min value="1"
atMost Alias for max value="10"

๐Ÿงช Testing

The project includes comprehensive tests for all major components:

# Run all tests
cargo test

# Run specific test suites
cargo test validator_tests
cargo test decompression_tests
cargo test parse_tokens_tests
cargo test roster_validation_tests

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

๐Ÿ“š Documentation

  • Validator Usage: See docs/validator_usage.md for detailed validator documentation
  • Examples: Check examples/validator_example.rs for comprehensive usage examples
  • API Documentation: Generate with cargo doc --open

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐ŸŽฎ Use Cases

This XML parser is particularly useful for:

  • Tabletop Wargaming: Parse BattleScribe roster files.
  • Game Development: Validate game rules and constraints
  • Data Analysis: Extract and analyze roster data
  • Tool Development: Build applications that work with roster files

๐Ÿ” Example Output

When running the validator example, you'll see output like:

=== XML Constraint Validator Example ===

1. Basic Constraint Validation:
  Selection count 1: 
    โœ— Selection count (1) is below minimum (2)
    โ†’ Invalid configuration
  Selection count 2: 
    โœ“ Selection count (2) meets minimum requirement (2)
    โœ“ Selection count (2) is within maximum limit (5)
    โ†’ Valid configuration
  Selection count 3: 
    โœ“ Selection count (3) meets minimum requirement (2)
    โœ“ Selection count (3) is within maximum limit (5)
    โ†’ Valid configuration

๐Ÿ› ๏ธ Dependencies

  • zip: File decompression (used for .rosz file handling)
  • anyhow: Error handling (used throughout the codebase)

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages