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