elementary-row-operation-verifier 0.0.1

A tool to verify the correctness of elementary row operations on matrices
Documentation
use super::types::OpType;
use crate::error::MatrixError;

pub fn parse_operation(op_str: &str, target_row: usize) -> Result<OpType, MatrixError> {
    let op_str = op_str.trim();

    // 解析加法:+ 2 r[1]
    if let Some(pos) = op_str.find('+') {
        let rest = &op_str[pos + 1..].trim();
        let parts: Vec<&str> = rest.split_whitespace().collect();
        if parts.len() >= 2 {
            let multiplier = parts[0]
                .parse::<f64>()
                .map_err(|_| MatrixError::ParseError {
                    line: 0,
                    message: format!("Invalid multiplier: {}", parts[0]),
                })?;
            let source = parse_row_index(parts[1])?;
            return Ok(OpType::AddMultiple {
                target: target_row,
                source,
                multiplier,
            });
        }
    }

    // 解析减法:- 2 r[1]
    if let Some(pos) = op_str.find('-') {
        // 确保不是负数的一部分
        if pos == 0 || !op_str.chars().nth(pos - 1).unwrap().is_ascii_digit() {
            let rest = &op_str[pos + 1..].trim();
            let parts: Vec<&str> = rest.split_whitespace().collect();
            if parts.len() >= 2 {
                let multiplier = parts[0]
                    .parse::<f64>()
                    .map_err(|_| MatrixError::ParseError {
                        line: 0,
                        message: format!("Invalid multiplier: {}", parts[0]),
                    })?;
                let source = parse_row_index(parts[1])?;
                return Ok(OpType::AddMultiple {
                    target: target_row,
                    source,
                    multiplier: -multiplier,
                });
            }
        }
    }

    // 解析乘法:x -1
    if let Some(pos) = op_str.find('x') {
        let rest = &op_str[pos + 1..].trim();
        let multiplier = rest.parse::<f64>().map_err(|_| MatrixError::ParseError {
            line: 0,
            message: format!("Invalid multiplier: {}", rest),
        })?;
        return Ok(OpType::Multiply {
            target: target_row,
            multiplier,
        });
    }

    // 解析替换:< r[3]
    if let Some(pos) = op_str.find('<') {
        let rest = &op_str[pos + 1..].trim();
        let source = parse_row_index(rest)?;
        return Ok(OpType::Replace {
            target: target_row,
            source,
        });
    }

    Err(MatrixError::ParseError {
        line: 0,
        message: format!("No valid operation found: {}", op_str),
    })
}

fn parse_row_index(s: &str) -> Result<usize, MatrixError> {
    let cleaned = s
        .trim()
        .trim_start_matches('r')
        .trim_start_matches('R')
        .trim_start_matches('[')
        .trim_end_matches(']');

    cleaned.parse().map_err(|_| MatrixError::ParseError {
        line: 0,
        message: format!("Invalid row index: {}", s),
    })
}