Skip to main content

toon_validate_core/
parser.rs

1use crate::{
2    error::{Result, TqError},
3    json_parser::JsonParser,
4    toon_parser::ToonParser,
5    value::Value,
6};
7
8#[derive(Debug, Clone, Copy, PartialEq)]
9pub enum InputFormat {
10    Toon,
11    Json,
12}
13
14impl InputFormat {
15    pub fn from_str(s: &str) -> Result<Self> {
16        match s.to_lowercase().as_str() {
17            "toon" => Ok(InputFormat::Toon),
18            "json" => Ok(InputFormat::Json),
19            _ => Err(TqError::InvalidFormat(format!("Unknown format: {}", s))),
20        }
21    }
22}
23
24pub struct Parser;
25
26impl Parser {
27    pub fn parse(input: &str, format: InputFormat) -> Result<Value> {
28        match format {
29            InputFormat::Toon => ToonParser::parse(input),
30            InputFormat::Json => JsonParser::parse(input),
31        }
32    }
33    
34    pub fn detect_format(input: &str) -> InputFormat {
35        let trimmed = input.trim();
36        if trimmed.starts_with('{') || trimmed.starts_with('[') {
37            InputFormat::Json
38        } else {
39            InputFormat::Toon
40        }
41    }
42}