Skip to content

Commit

Permalink
Return error when creating FileBackendHistory with invalid capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
nibon7 committed Jan 9, 2024
1 parent b68ce33 commit 0130480
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
4 changes: 2 additions & 2 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ use {
default_emacs_keybindings, default_vi_insert_keybindings, default_vi_normal_keybindings,
ColumnarMenu, DefaultCompleter, DefaultHinter, DefaultPrompt, DefaultValidator,
EditCommand, EditMode, Emacs, ExampleHighlighter, Keybindings, ListMenu, Reedline,
ReedlineEvent, ReedlineMenu, Signal, Vi,
ReedlineEvent, ReedlineMenu, Result, Signal, Vi,
},
};

use reedline::CursorConfig;
#[cfg(not(any(feature = "sqlite", feature = "sqlite-dynlib")))]
use reedline::FileBackedHistory;

fn main() -> std::io::Result<()> {
fn main() -> Result<()> {
println!("Ctrl-D to quit");
// quick command like parameter handling
let vi_mode = matches!(std::env::args().nth(1), Some(x) if x == "--vi");
Expand Down
33 changes: 22 additions & 11 deletions src/history/file_backed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ impl Default for FileBackedHistory {
/// Creates an in-memory [`History`] with a maximal capacity of [`HISTORY_SIZE`].
///
/// To create a [`History`] that is synchronized with a file use [`FileBackedHistory::with_file()`]
///
/// # Panics
///
/// If `HISTORY_SIZE == 0` or `HISTORY_SIZE == usize::MAX`
fn default() -> Self {
Self::new(HISTORY_SIZE)
match Self::new(HISTORY_SIZE) {
Ok(history) => history,
Err(e) => panic!("{}", e),

Check warning on line 47 in src/history/file_backed.rs

View check run for this annotation

Codecov / codecov/patch

src/history/file_backed.rs#L47

Added line #L47 was not covered by tests
}
}
}

Expand Down Expand Up @@ -283,20 +290,24 @@ impl History for FileBackedHistory {
impl FileBackedHistory {
/// Creates a new in-memory history that remembers `n <= capacity` elements
///
/// # Panics
///
/// If `capacity == usize::MAX`
pub fn new(capacity: usize) -> Self {
if capacity == usize::MAX {
panic!("History capacity too large to be addressed safely");
pub fn new(capacity: usize) -> Result<Self> {
if capacity == 0 {
return Err(ReedlineError(ReedlineErrorVariants::OtherHistoryError(
"History capacity too small to be addressed safely",
)));

Check warning on line 297 in src/history/file_backed.rs

View check run for this annotation

Codecov / codecov/patch

src/history/file_backed.rs#L295-L297

Added lines #L295 - L297 were not covered by tests
} else if capacity == usize::MAX {
return Err(ReedlineError(ReedlineErrorVariants::OtherHistoryError(
"History capacity too large to be addressed safely",
)));

Check warning on line 301 in src/history/file_backed.rs

View check run for this annotation

Codecov / codecov/patch

src/history/file_backed.rs#L299-L301

Added lines #L299 - L301 were not covered by tests
}
FileBackedHistory {

Ok(FileBackedHistory {
capacity,
entries: VecDeque::new(),
file: None,
len_on_disk: 0,
session: None,
}
})
}

/// Creates a new history with an associated history file.
Expand All @@ -307,8 +318,8 @@ impl FileBackedHistory {
///
/// **Side effects:** creates all nested directories to the file
///
pub fn with_file(capacity: usize, file: PathBuf) -> std::io::Result<Self> {
let mut hist = Self::new(capacity);
pub fn with_file(capacity: usize, file: PathBuf) -> Result<Self> {
let mut hist = Self::new(capacity)?;
if let Some(base_dir) = file.parent() {
std::fs::create_dir_all(base_dir)?;
}
Expand Down
6 changes: 6 additions & 0 deletions src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ pub enum ReedlineErrorVariants {
#[derive(Debug)]
pub struct ReedlineError(pub ReedlineErrorVariants);

impl From<std::io::Error> for ReedlineError {
fn from(err: std::io::Error) -> Self {
Self(ReedlineErrorVariants::IOError(err))
}

Check warning on line 29 in src/result.rs

View check run for this annotation

Codecov / codecov/patch

src/result.rs#L27-L29

Added lines #L27 - L29 were not covered by tests
}

impl Display for ReedlineError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
Expand Down

0 comments on commit 0130480

Please sign in to comment.