Skip to main content

xls_rs/
format_detector.rs

1//! Format detection for file types
2
3use crate::traits::FormatDetector;
4use anyhow::Result;
5
6/// Default format detector implementation
7pub struct DefaultFormatDetector;
8
9impl DefaultFormatDetector {
10    pub fn new() -> Self {
11        Self
12    }
13}
14
15impl FormatDetector for DefaultFormatDetector {
16    fn detect_format(&self, path: &str) -> Result<String> {
17        // Check for Google Sheets URLs or IDs first
18        if path.starts_with("gsheet://")
19            || path.starts_with("https://docs.google.com/spreadsheets/")
20            || (path.len() >= 44
21                && path
22                    .chars()
23                    .all(|c| c.is_alphanumeric() || c == '-' || c == '_'))
24        {
25            return Ok("gsheet".to_string());
26        }
27
28        // Fall back to file extension detection
29        path.split('.')
30            .last()
31            .map(|s| s.to_lowercase())
32            .ok_or_else(|| anyhow::anyhow!("No file extension found in: {}", path))
33    }
34
35    fn is_supported(&self, format: &str) -> bool {
36        matches!(
37            format.to_lowercase().as_str(),
38            "csv" | "xlsx" | "xls" | "ods" | "parquet" | "avro" | "gsheet"
39        )
40    }
41
42    fn supported_formats(&self) -> Vec<String> {
43        vec![
44            "csv".to_string(),
45            "xlsx".to_string(),
46            "xls".to_string(),
47            "ods".to_string(),
48            "parquet".to_string(),
49            "avro".to_string(),
50            "gsheet".to_string(),
51        ]
52    }
53}