pub mod insomnia;
use std::io;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ImportError {
#[error("Invalid format: {0}")]
InvalidFormat(String),
#[error("Unsupported version: {0}")]
UnsupportedVersion(String),
#[error("Parse error: {0}")]
ParseError(String),
#[error("IO error: {0}")]
IoError(#[from] io::Error),
#[error("Missing required field: {0}")]
MissingField(String),
}
pub type ImportResult<T> = Result<T, ImportError>;
#[derive(Debug, Clone)]
pub struct ImportedCollection {
pub name: String,
pub description: Option<String>,
pub requests: Vec<ImportedRequest>,
}
#[derive(Debug, Clone)]
pub struct ImportedRequest {
pub id: String,
pub name: String,
pub description: Option<String>,
pub method: String,
pub url: String,
pub headers: Vec<(String, String)>,
pub body: Option<String>,
}
pub trait ImportFormat {
type Source;
fn can_import(content: &str) -> bool;
fn parse(content: &str) -> ImportResult<Self::Source>;
fn convert(source: Self::Source) -> ImportResult<Vec<ImportedCollection>>;
fn import(content: &str) -> ImportResult<Vec<ImportedCollection>> {
let source = Self::parse(content)?;
Self::convert(source)
}
}
pub fn auto_import(content: &str) -> ImportResult<Vec<ImportedCollection>> {
if insomnia::InsomniaImporter::can_import(content) {
return insomnia::InsomniaImporter::import(content);
}
Err(ImportError::InvalidFormat(
"Unknown format. Supported: Insomnia v4".into(),
))
}