Skip to main content

docling_rag/source/
mod.rs

1//! Pluggable document sources. The default is a local [`folder`]; FTP and SFTP
2//! are available behind the `remote-sources` feature.
3
4pub mod folder;
5
6#[cfg(feature = "remote-sources")]
7pub mod ftp;
8#[cfg(feature = "remote-sources")]
9pub mod sftp;
10
11use crate::config::SourceKind;
12use crate::{RagConfig, RagError, Result};
13use async_trait::async_trait;
14use std::sync::Arc;
15
16/// A handle to one document available from a source.
17#[derive(Debug, Clone)]
18pub struct SourceRef {
19    /// A fully-qualified URI (`file:///…`, `ftp://host/…`, `sftp://host/…`).
20    pub uri: String,
21    /// A short display name, typically the file name.
22    pub name: String,
23    /// Path relative to the source root (`sub/dir/report.pdf`). Used to mirror
24    /// the source structure into `RAG_DOCUMENTS_OUTPUT`.
25    pub rel_path: String,
26}
27
28/// A place documents are read from.
29#[async_trait]
30pub trait DocumentSource: Send + Sync {
31    /// Enumerate the documents currently available.
32    async fn list(&self) -> Result<Vec<SourceRef>>;
33
34    /// Fetch the raw bytes of one document.
35    async fn fetch(&self, r: &SourceRef) -> Result<Vec<u8>>;
36}
37
38/// Build the document source selected by `cfg.source`.
39pub fn from_config(cfg: &RagConfig) -> Result<Arc<dyn DocumentSource>> {
40    match cfg.source {
41        SourceKind::Folder => Ok(Arc::new(folder::FolderSource::new(&cfg.source_path))),
42        SourceKind::Ftp => {
43            #[cfg(feature = "remote-sources")]
44            {
45                Ok(Arc::new(ftp::FtpSource::from_config(cfg)?))
46            }
47            #[cfg(not(feature = "remote-sources"))]
48            {
49                Err(RagError::FeatureDisabled(
50                    "ftp".into(),
51                    "remote-sources".into(),
52                ))
53            }
54        }
55        SourceKind::Sftp => {
56            #[cfg(feature = "remote-sources")]
57            {
58                Ok(Arc::new(sftp::SftpSource::from_config(cfg)?))
59            }
60            #[cfg(not(feature = "remote-sources"))]
61            {
62                Err(RagError::FeatureDisabled(
63                    "sftp".into(),
64                    "remote-sources".into(),
65                ))
66            }
67        }
68    }
69}