pub mod file;
pub mod http;
pub mod s3;
pub mod ssh;
use crate::{
connectors::{Source, Target},
Result, TinyEtlError,
};
use async_trait::async_trait;
use std::collections::HashMap;
use url::Url;
#[async_trait]
pub trait Protocol: Send + Sync {
async fn create_source(
&self,
url: &Url,
options: &HashMap<String, String>,
) -> Result<Box<dyn Source>>;
async fn create_source_with_type(
&self,
url: &Url,
_source_type: Option<&str>,
options: &HashMap<String, String>,
) -> Result<Box<dyn Source>> {
self.create_source(url, options).await
}
async fn create_target(
&self,
url: &Url,
options: &HashMap<String, String>,
) -> Result<Box<dyn Target>>;
fn validate_url(&self, url: &Url) -> Result<()>;
fn name(&self) -> &'static str;
}
pub(crate) fn extension_from_path(path: &str) -> Option<String> {
let filename = path.split('/').next_back()?;
let (stem, extension) = filename.rsplit_once('.')?;
if extension.is_empty() || extension.len() > 10 {
return None;
}
let extension = extension.to_lowercase();
if extension == "gz" || extension == "zst" {
if let Some((_, inner)) = stem.rsplit_once('.') {
if !inner.is_empty() && inner.len() <= 10 {
return Some(format!("{}.{}", inner.to_lowercase(), extension));
}
}
}
Some(extension)
}
pub fn create_protocol(url: &str) -> Result<Box<dyn Protocol>> {
if !url.contains("://") && (url.contains('.') || url.starts_with('/')) {
return Ok(Box::new(file::FileProtocol::new()));
}
let parsed_url = Url::parse(url)
.map_err(|e| TinyEtlError::Configuration(format!("Invalid URL '{}': {}", url, e)))?;
match parsed_url.scheme() {
"file" => Ok(Box::new(file::FileProtocol::new())),
"http" | "https" => Ok(Box::new(http::HttpProtocol::new())),
"ssh" => Ok(Box::new(ssh::SshProtocol::new())),
"s3" => Ok(Box::new(s3::S3Protocol::new())),
scheme => {
Err(TinyEtlError::Configuration(
format!("Unsupported protocol: {}. Supported protocols: file://, http://, https://, ssh://, s3:// (databases such as snowflake:// are handled by connectors)", scheme)
))
}
}
}
pub async fn create_source_from_url(url: &str) -> Result<Box<dyn Source>> {
create_source_from_url_with_type(url, None, &HashMap::new()).await
}
pub async fn create_source_from_url_with_type(
url: &str,
source_type: Option<&str>,
options: &HashMap<String, String>,
) -> Result<Box<dyn Source>> {
let protocol = create_protocol(url)?;
let parsed_url = if url.contains("://") {
Url::parse(url)
.map_err(|e| TinyEtlError::Configuration(format!("Invalid URL '{}': {}", url, e)))?
} else {
Url::parse(&format!("file://{}", url)).map_err(|e| {
TinyEtlError::Configuration(format!("Invalid file path '{}': {}", url, e))
})?
};
protocol.validate_url(&parsed_url)?;
protocol
.create_source_with_type(&parsed_url, source_type, options)
.await
}
pub async fn create_target_from_url(url: &str) -> Result<Box<dyn Target>> {
create_target_from_url_with_options(url, &HashMap::new()).await
}
pub async fn create_target_from_url_with_options(
url: &str,
options: &HashMap<String, String>,
) -> Result<Box<dyn Target>> {
let protocol = create_protocol(url)?;
let parsed_url = if url.contains("://") {
Url::parse(url)
.map_err(|e| TinyEtlError::Configuration(format!("Invalid URL '{}': {}", url, e)))?
} else {
Url::parse(&format!("file://{}", url)).map_err(|e| {
TinyEtlError::Configuration(format!("Invalid file path '{}': {}", url, e))
})?
};
protocol.validate_url(&parsed_url)?;
protocol.create_target(&parsed_url, options).await
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_file_protocol() {
let protocol = create_protocol("file:///path/to/file.csv");
assert!(protocol.is_ok());
assert_eq!(protocol.unwrap().name(), "file");
}
#[test]
fn test_create_http_protocol() {
let protocol = create_protocol("http://example.com/data.csv");
assert!(protocol.is_ok());
assert_eq!(protocol.unwrap().name(), "http");
}
#[test]
fn test_create_https_protocol() {
let protocol = create_protocol("https://example.com/data.csv");
assert!(protocol.is_ok());
assert_eq!(protocol.unwrap().name(), "http");
}
#[test]
fn test_create_ssh_protocol() {
let protocol = create_protocol("ssh://user@example.com/path/to/data.csv");
assert!(protocol.is_ok());
assert_eq!(protocol.unwrap().name(), "ssh");
}
#[test]
fn test_create_s3_protocol() {
let protocol = create_protocol("s3://my-bucket/path/to/data.parquet");
assert!(protocol.is_ok());
assert_eq!(protocol.unwrap().name(), "s3");
}
#[test]
fn test_extension_from_path() {
assert_eq!(
extension_from_path("/a/b/data.CSV"),
Some("csv".to_string())
);
assert_eq!(extension_from_path("/a.b/data"), None);
assert_eq!(extension_from_path("/a/b/"), None);
assert_eq!(
extension_from_path("/d/worldcities.csv.gz"),
Some("csv.gz".to_string())
);
assert_eq!(
extension_from_path("/d/t.Parquet.ZST"),
Some("parquet.zst".to_string())
);
assert_eq!(extension_from_path("/d/archive.gz"), Some("gz".to_string()));
}
#[test]
fn test_backward_compatibility() {
let protocol = create_protocol("test.csv");
assert!(protocol.is_ok());
assert_eq!(protocol.unwrap().name(), "file");
}
#[test]
fn test_unsupported_protocol() {
let result = create_protocol("ftp://example.com/file.csv");
assert!(result.is_err());
let result = create_protocol("unknown://example.com/file.csv");
assert!(result.is_err());
}
}