labscript 0.1.0

Prescription PDF generator with e-signature and QR verification
use crate::errors::LabscriptError;
use crate::types::VerifyResult;

/// Parse and verify a QR code string.
/// Expected format: labscript:v1:{uuid}:{sha256hash}:{iso_timestamp}
pub fn verify_qr(qr_string: &str) -> Result<VerifyResult, LabscriptError> {
    let parts: Vec<&str> = qr_string.splitn(5, ':').collect();

    if parts.len() != 5 {
        return Err(LabscriptError::InvalidInput(format!(
            "Invalid QR format: expected 5 colon-separated fields, got {}",
            parts.len()
        )));
    }

    if parts[0] != "labscript" {
        return Err(LabscriptError::InvalidInput(format!(
            "Invalid QR prefix: expected 'labscript', got '{}'",
            parts[0]
        )));
    }

    let version = parts[1].to_string();
    if version != "v1" {
        return Err(LabscriptError::InvalidInput(format!(
            "Unsupported QR version: {version}"
        )));
    }

    let prescription_id = parts[2].to_string();
    let hash = parts[3].to_string();
    let timestamp = parts[4].to_string();

    // Validate UUID format (basic: 36 chars with hyphens)
    if prescription_id.len() != 36 {
        return Err(LabscriptError::InvalidInput(format!(
            "Invalid prescription ID (expected UUID): {}",
            prescription_id
        )));
    }

    // Validate hash is hex, 64 chars
    if hash.len() != 64 || !hash.chars().all(|c| c.is_ascii_hexdigit()) {
        return Err(LabscriptError::InvalidInput(
            "Invalid verification hash (expected 64-char hex)".to_string(),
        ));
    }

    Ok(VerifyResult {
        valid: true,
        version,
        prescription_id,
        hash,
        timestamp,
    })
}