From 5ea1f8299f28634399839a4d0f750a7c9e4a5e85 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Wed, 16 Nov 2022 01:43:59 +0530 Subject: [PATCH] refactor: address Clippy warnings --- src/document.rs | 13 +++++++++++++ src/editor.rs | 8 ++++---- src/main.rs | 4 +--- src/row.rs | 14 +++++++++----- src/terminal.rs | 8 ++++++++ 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/document.rs b/src/document.rs index 173a96b..25593a9 100644 --- a/src/document.rs +++ b/src/document.rs @@ -11,14 +11,17 @@ pub struct Document { } impl Document { + #[must_use] pub fn row(&self, index: usize) -> Option<&Row> { self.rows.get(index) } + #[must_use] pub fn is_empty(&self) -> bool { self.rows.is_empty() } + #[must_use] pub fn len(&self) -> usize { self.rows.len() } @@ -35,6 +38,9 @@ impl Document { self.rows.insert(at.y + 1, new_row); } + /// # Panics + /// This function can panic if the given Position is not + /// within the document's rows. pub fn insert(&mut self, at: &Position, c: char) { if c == '\n' { self.insert_newline(at); @@ -54,6 +60,9 @@ impl Document { } } + /// # Panics + /// This function can panic if the given Position is not + /// within the document's rows. pub fn delete(&mut self, at: &Position) { let len = self.len(); if at.y >= len { @@ -69,6 +78,8 @@ impl Document { } } + /// # Errors + /// Returns an error if the file could not be opened. pub fn open(filename: &str) -> Result { let contents = fs::read_to_string(filename)?; let mut rows = vec![]; @@ -81,6 +92,8 @@ impl Document { }) } + /// # Errors + /// Returns an error if the file could not be saved. pub fn save(&self) -> Result<(), Error> { if let Some(file_name) = &self.file_name { let mut file = fs::File::create(file_name)?; diff --git a/src/editor.rs b/src/editor.rs index 87f0783..c68fa36 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -70,13 +70,13 @@ impl Editor { pub fn run(&mut self) { loop { if let Err(error) = self.refresh_screen() { - die(error); + die(&error); }; if self.should_quit { break; } if let Err(error) = self.process_keypress() { - die(error); + die(&error); }; } } @@ -276,7 +276,7 @@ impl Editor { fn draw_message_bar(&self) { Terminal::clear_current_line(); let message = &self.status_message; - if Instant::now() - message.time < Duration::new(5, 0) { + if message.time.elapsed() < Duration::new(5, 0) { let mut text = message.text.clone(); text.truncate(self.terminal.size().width as usize); print!("{}", text); @@ -284,7 +284,7 @@ impl Editor { } } -fn die(e: std::io::Error) { +fn die(e: &std::io::Error) { Terminal::clear_screen(); panic!("{}", e); } diff --git a/src/main.rs b/src/main.rs index ae4bdad..0c9d282 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,10 +8,8 @@ use crate::editor::Editor; pub use crate::editor::Position; pub use crate::row::Row; pub use crate::terminal::Terminal; -use anyhow::Result; -fn main() -> Result<()> { +fn main() { let mut editor = Editor::default(); editor.run(); - Ok(()) } diff --git a/src/row.rs b/src/row.rs index 047a518..cb5d1f7 100644 --- a/src/row.rs +++ b/src/row.rs @@ -19,6 +19,7 @@ impl From<&str> for Row { } impl Row { + #[must_use] pub fn render(&self, start: usize, end: usize) -> String { let end = cmp::min(end, self.contents.len()); let start = cmp::min(start, end); @@ -37,10 +38,12 @@ impl Row { result } + #[must_use] pub fn len(&self) -> usize { self.len } + #[must_use] pub fn is_empty(&self) -> bool { self.len == 0 } @@ -65,12 +68,11 @@ impl Row { pub fn delete(&mut self, at: usize) { if at >= self.len() { return; - } else { - let mut result: String = self.contents[..].graphemes(true).take(at).collect(); - let remainder: String = self.contents[..].graphemes(true).skip(at + 1).collect(); - result.push_str(&remainder); - self.contents = result; } + let mut result: String = self.contents[..].graphemes(true).take(at).collect(); + let remainder: String = self.contents[..].graphemes(true).skip(at + 1).collect(); + result.push_str(&remainder); + self.contents = result; self.update_len(); } @@ -79,6 +81,7 @@ impl Row { self.update_len(); } + #[must_use] pub fn split(&mut self, at: usize) -> Self { let beginning: String = self.contents[..].graphemes(true).take(at).collect(); let remainder: String = self.contents[..].graphemes(true).skip(at).collect(); @@ -87,6 +90,7 @@ impl Row { Self::from(&remainder[..]) } + #[must_use] pub fn as_bytes(&self) -> &[u8] { self.contents.as_bytes() } diff --git a/src/terminal.rs b/src/terminal.rs index 9dc11e1..de37319 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -16,6 +16,8 @@ pub struct Terminal { } impl Terminal { + /// # Errors + /// Returns an error if terminal size cannot be retrieved or raw mode cannot be enabled. pub fn default() -> Result { let size = termion::terminal_size()?; Ok(Self { @@ -27,6 +29,7 @@ impl Terminal { }) } + #[must_use] pub fn size(&self) -> &Size { &self.size } @@ -39,6 +42,7 @@ impl Terminal { print!("{}", termion::clear::CurrentLine); } + #[allow(clippy::cast_possible_truncation)] pub fn reposition_cursor(position: &Position) { let Position { mut x, mut y } = position; x = x.saturating_add(1); @@ -48,10 +52,14 @@ impl Terminal { print!("{}", termion::cursor::Goto(x, y)); } + /// # Errors + /// Returns an error if stdout cannot be flushed pub fn flush() -> Result<(), Error> { io::stdout().flush() } + /// # Errors + /// Returns an error if input events can't be retrieved pub fn read_key() -> Result { loop { if let Some(key) = io::stdin().lock().keys().next() {