Skip to main content

Source

Struct Source 

Source
pub struct Source { /* private fields */ }

Implementations§

Source§

impl Source

Source

pub fn open(path: impl AsRef<Path>) -> Result<Self>

Opens a file by memory-mapping it.

Source

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.

Source

pub fn from_bytes(name: impl Into<String>, bytes: impl Into<Vec<u8>>) -> Self

Source

pub fn origin(&self) -> &Origin

Source

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.

Source

pub fn display_name(&self) -> &str

A human-readable name for headers and error messages.

Source

pub fn bytes(&self) -> &[u8]

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn peek(&self, n: usize) -> &[u8]

The detection prefix, without consuming anything.

Source

pub fn probe(&self) -> &[u8]

Source

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.

Source

pub fn encoding_name(&self) -> &'static str

The encoding in force. UTF-8 unless set_encoding said otherwise.

Source

pub fn text(&self) -> (Cow<'_, str>, bool)

The source as text, as a Cow.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for Source

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.