refactor: address Clippy warnings

This commit is contained in:
Harsh Shandilya 2022-11-16 01:43:59 +05:30
parent cf08ffc7bf
commit 5ea1f8299f
No known key found for this signature in database
5 changed files with 35 additions and 12 deletions

View File

@ -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<Self, std::io::Error> {
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)?;

View File

@ -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);
}

View File

@ -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(())
}

View File

@ -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()
}

View File

@ -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<Self, Error> {
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<Key, io::Error> {
loop {
if let Some(key) = io::stdin().lock().keys().next() {