shk_parser 0.1.1

A parser for Stronghold Kingdoms attack formation files (.cas)
Documentation
//! # Stronghold Kingdoms Formation Parser
//!
//! A Rust library for parsing Stronghold Kingdoms attack formation files (`.cas` files).
//!
//! ## Quick Start
//!
//! ```no_run
//! use shk_parser::{parse_formation, parse_formation_file};
//!
//! // Parse from bytes
//! let data = std::fs::read("formation.cas")?;
//! let units = parse_formation(&data)?;
//!
//! // Or parse directly from file
//! let units = parse_formation_file("formation.cas")?;
//!
//! // Display the units
//! for unit in &units {
//!     println!("{}", unit);
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ## Supported Units
//!
//! - **Archers**: Basic ranged units
//! - **Pikemen**: Basic melee units  
//! - **Catapults**: Siege weapons with target coordinates
//! - **Captains**: Special units with abilities:
//!   - Delay: Wait for specified time
//!   - Rallying Cry: Rally nearby troops
//!   - Arrow Volley: Shoot arrows at target location
//!   - Battle Cry: Boost morale
//!   - Catapults Volley: Catapult attack at target location
//!
//! ## Features
//!
//! - **`serde`**: Enables JSON serialization/deserialization support
//! - **`tsify`**: Enables TypeScript type generation for WASM bindings (requires `serde`)
//! - **`cli`**: Enables the command-line interface

#![warn(clippy::pedantic)]
#![forbid(unsafe_code)]

pub mod parser;
pub mod types;

#[cfg(test)]
mod tests;

// Re-export the main types for convenience
pub use parser::{AttackSetupParser, ParseError, ParseResult};
pub use types::{CaptainAbility, Position, UnitRecord, UnitType};

/// Parse a formation from raw bytes
///
/// This is the main parsing function. It takes the raw bytes of a `.cas` file
/// and returns a vector of unit records.
///
/// # Examples
///
/// ```no_run
/// use shk_parser::parse_formation;
///
/// let data = std::fs::read("formation.cas")?;
/// let units = parse_formation(&data)?;
///
/// println!("Formation contains {} units", units.len());
/// for unit in &units {
///     println!("- {}", unit);
/// }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Errors
///
/// Returns `ParseError` if the data is invalid or malformed.
/// See [`AttackSetupParser::parse`] for detailed error conditions.
pub fn parse_formation(data: &[u8]) -> ParseResult<Vec<UnitRecord>> {
    AttackSetupParser::parse(data)
}

/// Parse a formation file directly
///
/// Convenience function that reads the file and parses it in one step.
///
/// # Examples
///
/// ```no_run
/// use shk_parser::parse_formation_file;
///
/// let units = parse_formation_file("my_formation.cas")?;
/// println!("Loaded {} units from formation", units.len());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Errors
///
/// Returns `ParseError` if the file cannot be read or contains invalid data.
/// See [`AttackSetupParser::parse_file`] for detailed error conditions.
pub fn parse_formation_file<P: AsRef<std::path::Path>>(path: P) -> ParseResult<Vec<UnitRecord>> {
    AttackSetupParser::parse_file(path)
}