diffx_core/parser/
format.rs1use anyhow::Result;
2use serde_json::Value;
3use std::path::Path;
4
5use super::{parse_csv, parse_ini, parse_json, parse_toml, parse_xml, parse_yaml};
6
7#[derive(Debug, Clone, Copy)]
8pub enum FileFormat {
9 Json,
10 Yaml,
11 Csv,
12 Toml,
13 Ini,
14 Xml,
15}
16
17pub fn detect_format_from_path(path: &Path) -> FileFormat {
18 match path.extension().and_then(|ext| ext.to_str()) {
19 Some("json") => FileFormat::Json,
20 Some("yaml") | Some("yml") => FileFormat::Yaml,
21 Some("csv") => FileFormat::Csv,
22 Some("toml") => FileFormat::Toml,
23 Some("ini") | Some("cfg") => FileFormat::Ini,
24 Some("xml") => FileFormat::Xml,
25 _ => FileFormat::Json, }
27}
28
29pub fn parse_content_by_format(content: &str, format: FileFormat) -> Result<Value> {
30 match format {
31 FileFormat::Json => parse_json(content),
32 FileFormat::Yaml => parse_yaml(content),
33 FileFormat::Csv => parse_csv(content),
34 FileFormat::Toml => parse_toml(content),
35 FileFormat::Ini => parse_ini(content),
36 FileFormat::Xml => parse_xml(content),
37 }
38}