#![allow(unused)]
use async_trait::async_trait;
use mockall::{automock, predicate::*};
pub struct NewExternalSource<'a> {
pub name: &'a str,
pub bucket_id: i32,
}
#[derive(Clone)]
pub struct ExternalSource {
pub bucket_id: i32,
pub external_source_id: i32,
pub external_source_name: String,
pub updated_by: i32,
pub updated_datetime: Option<String>,
}
pub struct NewExternalItem<'a> {
pub content: &'a str,
pub url: &'a str,
pub bucket_id: i64,
pub external_source_id: i64,
pub processing_state: Option<&'a str>,
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct ExternalItem {
pub content_hash: String,
pub external_item_id: i64,
pub external_source_id: i64,
pub processing_state: String,
pub state: String,
pub updated_datetime: Option<String>,
pub url: String,
}
pub type DownloadError = Box<dyn std::error::Error + Send + Sync>;
#[derive(Debug, Clone)]
pub struct DownloadedManifest {
pub sources: Vec<DownloadedSource>,
}
#[derive(Debug, Clone)]
pub struct DownloadedSource {
pub logical_name: String,
pub local_path: std::path::PathBuf,
pub original_source: crate::download::SourceAction,
}
#[cfg_attr(any(test, feature = "test-export-mocks"), automock)]
#[async_trait]
pub trait Downloader: Send + Sync {
async fn download_all(&self) -> Result<DownloadedManifest, DownloadError>;
}
#[cfg_attr(any(test, feature = "test-export-mocks"), automock)]
#[async_trait]
pub trait Uploader: Send + Sync {
async fn create_source<'a>(
&self,
req: NewExternalSource<'a>,
) -> Result<ExternalSource, Box<dyn std::error::Error + Send + Sync>>;
async fn create_item<'a>(
&self,
req: NewExternalItem<'a>,
) -> Result<ExternalItem, Box<dyn std::error::Error + Send + Sync>>;
async fn get_source_by_id(
&self,
external_source_id: i32,
) -> Result<ExternalSource, Box<dyn std::error::Error + Send + Sync>>;
async fn delete_source_by_id(
&self,
external_source_id: i32,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
async fn delete_item_by_id(
&self,
external_source_id: i64,
external_item_id: i64,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
async fn list_sources(
&self,
) -> Result<Vec<ExternalSource>, Box<dyn std::error::Error + Send + Sync>>;
}
#[derive(Debug, Clone)]
pub struct ProcessConfig {
pub kind: ProcessorKind,
}
#[derive(Debug, Clone)]
pub enum ProcessorKind {
ReadmeToPDF,
FlattenFiles,
}
impl From<&str> for ProcessorKind {
fn from(s: &str) -> Self {
match s {
"ReadmeToPDF" | "readme_to_pdf" | "readme2pdf" => ProcessorKind::ReadmeToPDF,
"FlattenFiles" | "flattenfiles" | "flatten_files" => ProcessorKind::FlattenFiles,
other => {
tracing::warn!(
kind = other,
"Unknown processor kind, defaulting to FlattenFiles"
);
ProcessorKind::FlattenFiles
}
}
}
}
#[derive(Debug, Clone)]
pub struct ProcessInput {
pub name: String,
pub repo_path: std::path::PathBuf,
}
#[derive(Debug, Clone)]
pub struct ExternalSourceInput {
pub name: String,
pub external_items: Vec<ExternalItemInput>,
}
#[derive(Debug, Clone)]
pub struct ExternalItemInput {
pub filename: String,
pub content: Vec<u8>,
}
#[derive(Debug)]
pub enum ProcessError {
Io(std::io::Error),
NoReadme,
Other(String),
}
impl From<std::io::Error> for ProcessError {
fn from(e: std::io::Error) -> Self {
ProcessError::Io(e)
}
}
#[automock]
#[async_trait]
pub trait Preprocessor: Send + Sync {
async fn process(&self, input: ProcessInput) -> Result<ExternalSourceInput, ProcessError>;
}