Skip to main content

lawkit_core/common/input/
parser.rs

1use super::file_detector::{detect_file_format, parse_file_by_format};
2use crate::common::international::extract_numbers_international;
3use std::path::Path;
4
5/// Extract numbers from text input
6use crate::error::Result;
7
8pub fn parse_text_input(text: &str) -> Result<Vec<f64>> {
9    let numbers = extract_numbers_international(text);
10
11    if numbers.is_empty() {
12        return Err(crate::error::BenfError::NoNumbersFound);
13    }
14
15    Ok(numbers)
16}
17
18/// Parse any supported file format and extract numbers
19pub fn parse_file_input(file_path: &Path) -> Result<Vec<f64>> {
20    // First check if file exists
21    if !file_path.exists() {
22        return Err(crate::error::BenfError::FileError(format!(
23            "File not found: {}",
24            file_path.display()
25        )));
26    }
27
28    // Detect file format
29    let format = detect_file_format(file_path);
30
31    // Parse based on detected format
32    parse_file_by_format(file_path, &format)
33}
34
35/// Parse input that could be either a file path or text content
36pub fn parse_input_auto(input: &str) -> Result<Vec<f64>> {
37    let path = Path::new(input);
38
39    if path.exists() {
40        // Input is a file path
41        parse_file_input(path)
42    } else {
43        // Input is text content
44        parse_text_input(input)
45    }
46}