streampager/
file.rs

1//! Files.
2
3use std::borrow::Cow;
4
5use enum_dispatch::enum_dispatch;
6
7pub(crate) use crate::control::ControlledFile;
8pub(crate) use crate::loaded_file::LoadedFile;
9
10/// An identifier for a file streampager is paging.
11pub type FileIndex = usize;
12
13/// Default value for `needed_lines`.
14pub(crate) const DEFAULT_NEEDED_LINES: usize = 5000;
15
16/// Trait for getting information from a file.
17#[enum_dispatch]
18pub(crate) trait FileInfo {
19    /// The file's index.
20    fn index(&self) -> FileIndex;
21
22    /// The file's title.
23    fn title(&self) -> Cow<'_, str>;
24
25    /// The file's info.
26    fn info(&self) -> Cow<'_, str>;
27
28    /// True once the file is loaded and all newlines have been parsed.
29    fn loaded(&self) -> bool;
30
31    /// Returns the number of lines in the file.
32    fn lines(&self) -> usize;
33
34    /// Runs the `call` function, passing it the contents of line `index`.
35    /// Tries to avoid copying the data if possible, however the borrowed
36    /// line only lasts as long as the function call.
37    fn with_line<T, F>(&self, index: usize, call: F) -> Option<T>
38    where
39        F: FnMut(Cow<'_, [u8]>) -> T;
40
41    /// Set how many lines are needed.
42    ///
43    /// If `self.lines()` exceeds that number, pause loading until
44    /// `set_needed_lines` is called with a larger number.
45    /// This is only effective for "streamed" input.
46    fn set_needed_lines(&self, lines: usize);
47
48    /// True if the loading thread has been paused.
49    fn paused(&self) -> bool;
50}
51
52/// A file.
53#[enum_dispatch(FileInfo)]
54#[derive(Clone)]
55pub(crate) enum File {
56    LoadedFile,
57    ControlledFile,
58}