saffron_data/importers/
mod.rs

1pub mod insomnia;
2
3use std::io;
4use thiserror::Error;
5
6#[derive(Debug, Error)]
7pub enum ImportError {
8    #[error("Invalid format: {0}")]
9    InvalidFormat(String),
10
11    #[error("Unsupported version: {0}")]
12    UnsupportedVersion(String),
13
14    #[error("Parse error: {0}")]
15    ParseError(String),
16
17    #[error("IO error: {0}")]
18    IoError(#[from] io::Error),
19
20    #[error("Missing required field: {0}")]
21    MissingField(String),
22}
23
24pub type ImportResult<T> = Result<T, ImportError>;
25
26/// Generic imported collection structure (format-agnostic)
27#[derive(Debug, Clone)]
28pub struct ImportedCollection {
29    pub name: String,
30    pub description: Option<String>,
31    pub requests: Vec<ImportedRequest>,
32}
33
34#[derive(Debug, Clone)]
35pub struct ImportedRequest {
36    pub id: String,
37    pub name: String,
38    pub description: Option<String>,
39    pub method: String,
40    pub url: String,
41    pub headers: Vec<(String, String)>,
42    pub body: Option<String>,
43}
44
45/// Trait for importing collections from external formats
46pub trait ImportFormat {
47    /// The source format type (e.g., InsomniaV4, PostmanV2)
48    type Source;
49
50    /// Validates if the input can be parsed by this importer
51    fn can_import(content: &str) -> bool;
52
53    /// Parses the content into the source format
54    fn parse(content: &str) -> ImportResult<Self::Source>;
55
56    /// Converts the source format into generic imported collections
57    fn convert(source: Self::Source) -> ImportResult<Vec<ImportedCollection>>;
58
59    /// Full import pipeline: parse and convert
60    fn import(content: &str) -> ImportResult<Vec<ImportedCollection>> {
61        let source = Self::parse(content)?;
62        Self::convert(source)
63    }
64}
65
66/// Auto-detect and import from multiple formats
67pub fn auto_import(content: &str) -> ImportResult<Vec<ImportedCollection>> {
68    // Try Insomnia first
69    if insomnia::InsomniaImporter::can_import(content) {
70        return insomnia::InsomniaImporter::import(content);
71    }
72
73    // Add more formats here as we implement them
74    // if postman::PostmanImporter::can_import(content) {
75    //     return postman::PostmanImporter::import(content);
76    // }
77
78    Err(ImportError::InvalidFormat(
79        "Unknown format. Supported: Insomnia v4".into(),
80    ))
81}