saffron_data/importers/
mod.rs1pub 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#[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
45pub trait ImportFormat {
47 type Source;
49
50 fn can_import(content: &str) -> bool;
52
53 fn parse(content: &str) -> ImportResult<Self::Source>;
55
56 fn convert(source: Self::Source) -> ImportResult<Vec<ImportedCollection>>;
58
59 fn import(content: &str) -> ImportResult<Vec<ImportedCollection>> {
61 let source = Self::parse(content)?;
62 Self::convert(source)
63 }
64}
65
66pub fn auto_import(content: &str) -> ImportResult<Vec<ImportedCollection>> {
68 if insomnia::InsomniaImporter::can_import(content) {
70 return insomnia::InsomniaImporter::import(content);
71 }
72
73 Err(ImportError::InvalidFormat(
79 "Unknown format. Supported: Insomnia v4".into(),
80 ))
81}