pub struct Source { /* private fields */ }Implementations§
Source§impl Source
impl Source
Sourcepub fn from_stdin() -> Result<Self>
pub fn from_stdin() -> Result<Self>
Reads stdin to completion.
KNOWN M0 LIMITATION: stdin is fully buffered. The M0 formats (plain text and
Markdown) gain nothing from incremental streaming —Markdown needs the whole input
anyway— and huge files have the file path, which is lazy. The incremental stdin
reader arrives with the log reader in M1, where kubectl logs -f | termdoc makes
it essential.
pub fn from_bytes(name: impl Into<String>, bytes: impl Into<Vec<u8>>) -> Self
pub fn origin(&self) -> &Origin
Sourcepub fn path(&self) -> Option<&Path>
pub fn path(&self) -> Option<&Path>
The file path, when the source is a file. Extension-based detection needs it; stdin has none, which is why content sniffing is the primary path rather than an optional extra.
Sourcepub fn display_name(&self) -> &str
pub fn display_name(&self) -> &str
A human-readable name for headers and error messages.
pub fn bytes(&self) -> &[u8] ⓘ
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn probe(&self) -> &[u8] ⓘ
Sourcepub fn set_encoding(&mut self, label: &str) -> Result<()>
pub fn set_encoding(&mut self, label: &str) -> Result<()>
Sets the encoding the source will be decoded with.
It has to be called before any read, and only once: the decoded text is cached so
readers can borrow a &str with the source’s lifetime, and re-decoding would
invalidate borrows that already exist. Taking &mut self is what makes that a
compile-time guarantee rather than a comment.
The label is anything encoding_rs accepts (latin1, windows-1252, shift_jis,
…). An unknown label is a usage error, not a silent fallback: guessing after the user
asked for something specific is worse than saying no.
Sourcepub fn encoding_name(&self) -> &'static str
pub fn encoding_name(&self) -> &'static str
The encoding in force. UTF-8 unless set_encoding said otherwise.
Sourcepub fn as_str(&self) -> (&str, bool)
pub fn as_str(&self) -> (&str, bool)
The source as a &str with the source’s own lifetime.
Returns (text, had_replacements). Readers that cannot work line by line need this —
Markdown needs the complete document — because a &'a str borrowed from the source
can travel inside the events, while a String local to the reader cannot.
UTF-8 input copies nothing. Any other encoding is transcoded once and cached here.
This walks the entire source, so a reader that can go line by line must use
decode_line instead: that is the difference between termdoc huge.log | head -5
reading a few pages and reading the whole file.
Sourcepub fn decode_line<'a>(&self, bytes: &'a [u8]) -> (Cow<'a, str>, bool)
pub fn decode_line<'a>(&self, bytes: &'a [u8]) -> (Cow<'a, str>, bool)
Decodes a single slice of the source, honoring the configured encoding.
This is the streaming readers’ path: it keeps the laziness of going line by line — nothing forces a walk of the whole file — while still handling non-UTF-8 input correctly. Valid UTF-8 is borrowed; anything else costs one allocation for that line alone.
Sourcepub fn looks_binary(&self) -> bool
pub fn looks_binary(&self) -> bool
Binary heuristic: a NUL byte in the prefix. The same rule grep and git use,
and enough to avoid dumping an executable into the terminal.