pub struct SourceDocument {
pub name: String,
pub format: InputFormat,
pub bytes: Vec<u8>,
pub path: Option<PathBuf>,
pub base_url: Option<String>,
pub encoding: Option<String>,
}Expand description
A loaded input document: its name, detected format, and raw bytes.
Fields§
§name: String§format: InputFormat§bytes: Vec<u8>§path: Option<PathBuf>The filesystem path it was loaded from, if any (from_file). Used to
resolve relative <img src> paths when image fetching is enabled; None
for in-memory sources.
base_url: Option<String>The URL this document was fetched from, if any. Used to resolve
relative / protocol-relative <img src> against the page’s origin when
image fetching is enabled (an HTML page fetched from the web references
its images by relative path). None for local / in-memory sources.
encoding: Option<String>The character encoding to decode the bytes with when a backend reads
them as text — docling’s TextBackendOptions.encoding
(MarkdownBackendOptions(encoding="shift_jis")). A WHATWG encoding
label (shift_jis, koi8-r, windows-1251, latin1; Python codec
spellings with _ are accepted too). None (default) detects the
encoding — see Self::text. Set by Self::with_encoding or, for
every source a converter handles, DocumentConverter::encoding.
Implementations§
Source§impl SourceDocument
impl SourceDocument
Sourcepub fn from_file(path: impl AsRef<Path>) -> Result<Self, ConversionError>
pub fn from_file(path: impl AsRef<Path>) -> Result<Self, ConversionError>
Load a document from a filesystem path, detecting the format from the extension.
Sourcepub fn from_bytes(
name: impl Into<String>,
format: InputFormat,
bytes: Vec<u8>,
) -> Self
pub fn from_bytes( name: impl Into<String>, format: InputFormat, bytes: Vec<u8>, ) -> Self
Construct directly from in-memory bytes (no disk access).
Sourcepub fn with_encoding(self, label: Option<String>) -> Self
pub fn with_encoding(self, label: Option<String>) -> Self
Decode the bytes with this character encoding when read as text —
nothing is guessed, and bytes the encoding cannot decode fail the
conversion (docling raises DocumentLoadError). None restores
detection.
Sourcepub fn with_base_url(self, url: impl Into<String>) -> Self
pub fn with_base_url(self, url: impl Into<String>) -> Self
Record the URL this document was fetched from (for resolving relative
<img src> against the page origin when image fetching is enabled).
Sourcepub fn base_dir(&self) -> Option<&Path>
pub fn base_dir(&self) -> Option<&Path>
The directory containing the source file, for resolving relative asset
paths. None for in-memory sources.
Sourcepub fn text(&self) -> Result<Cow<'_, str>, ConversionError>
pub fn text(&self) -> Result<Cow<'_, str>, ConversionError>
The bytes as text, for text-based backends — docling’s decode_text
(docling#4202, 2.129). A byte-order mark settles the encoding first
(UTF-32, UTF-8, UTF-16, and is dropped — docling#4098/#4109: Excel and
Google Sheets write one when exporting “CSV UTF-8”, and kept it
prefixes the first cell/line, so # Title stops being a heading and
WebVTT’s signature check misses WEBVTT). Without one the text must
be UTF-8; when it is not, the share of non-ASCII bytes that still form
well-formed UTF-8 sequences decides: at 50% or more the file reads as
damaged UTF-8 and is an error (decoding it as a code page would turn
the damage into text that looks fine and is not), below that it is the
one code page docling falls back to, windows-1252 — decoded with a
warning, or an error on the five bytes that page leaves undefined
(Python’s cp1252 rejects them where the WHATWG table maps them to
C1 controls). Borrowed when the bytes are already UTF-8.
Trait Implementations§
Source§impl Clone for SourceDocument
impl Clone for SourceDocument
Auto Trait Implementations§
impl Freeze for SourceDocument
impl RefUnwindSafe for SourceDocument
impl Send for SourceDocument
impl Sync for SourceDocument
impl Unpin for SourceDocument
impl UnsafeUnpin for SourceDocument
impl UnwindSafe for SourceDocument
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more