Skip to content

Commit

Permalink
Fixed clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindbergem committed Aug 27, 2024
1 parent fe1ae58 commit 45a7358
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 138 deletions.
11 changes: 5 additions & 6 deletions noline/src/async_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,12 @@ where
loop {
let mut buf = [0x8; 1];
let len = io.read(&mut buf).await?;
if len == 1 {
if Self::handle_output(line.advance(buf[0]), io)
if len == 1
&& Self::handle_output(line.advance(buf[0]), io)
.await?
.is_some()
{
break;
}
{
break;
}
}

Expand All @@ -114,7 +113,7 @@ where
}

/// Get history as iterator over circular slices
pub fn get_history<'a>(&'a self) -> impl Iterator<Item = CircularSlice<'a>> {
pub fn get_history(&self) -> impl Iterator<Item = CircularSlice<'_>> {
get_history_entries(&self.history)
}
}
6 changes: 5 additions & 1 deletion noline/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
use core::marker::PhantomData;

use crate::{
async_editor, error::NolineError, history::{History, NoHistory, StaticHistory}, line_buffer::{Buffer, NoBuffer, StaticBuffer}, sync_editor
async_editor,
error::NolineError,
history::{History, NoHistory, StaticHistory},
line_buffer::{Buffer, NoBuffer, StaticBuffer},
sync_editor,
};

#[cfg(any(test, doc, feature = "alloc", feature = "std"))]
Expand Down
35 changes: 15 additions & 20 deletions noline/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Initializer {
// so to probe the size we move the cursor way out of the screen
// and then request the position, because the cursor must be in
// the screen this gives us the size.
"\r\x1b[J\x1b7\x1b[6n\x1b[999;999H\x1b[6n\x1b8".as_bytes()
b"\r\x1b[J\x1b7\x1b[6n\x1b[999;999H\x1b[6n\x1b8"
}

// Advance initializer by one byte
Expand Down Expand Up @@ -113,21 +113,21 @@ impl<'a, B: Buffer, H: History> Line<'a, B, H> {
}

// Truncate buffer, clear line and print prompt
pub fn reset<'b>(&'b mut self) -> Output<'b, B> {
pub fn reset(&mut self) -> Output<'_, B> {
self.buffer.truncate();
self.generate_output(ClearAndPrintPrompt)
}

fn generate_output<'b>(&'b mut self, action: OutputAction) -> Output<'b, B> {
Output::new(self.prompt, &mut self.buffer, &mut self.terminal, action)
fn generate_output(&mut self, action: OutputAction) -> Output<'_, B> {
Output::new(self.prompt, self.buffer, self.terminal, action)
}

fn current_position(&self) -> usize {
let pos = self.terminal.current_offset() as usize;
pos - self.prompt.len()
}

fn history_move_up<'b>(&'b mut self) -> Output<'b, B> {
fn history_move_up(&mut self) -> Output<'_, B> {
let entry = if self.nav.is_active() {
self.nav.move_up()
} else if self.buffer.len() == 0 {
Expand All @@ -152,7 +152,7 @@ impl<'a, B: Buffer, H: History> Line<'a, B, H> {
}
}

fn history_move_down<'b>(&'b mut self) -> Output<'b, B> {
fn history_move_down(&mut self) -> Output<'_, B> {
let entry = if self.nav.is_active() {
self.nav.move_down()
} else {
Expand All @@ -177,7 +177,7 @@ impl<'a, B: Buffer, H: History> Line<'a, B, H> {

// Advance state machine by one byte. Returns output iterator over
// 0 or more byte slices.
pub(crate) fn advance<'b>(&'b mut self, byte: u8) -> Output<'b, B> {
pub(crate) fn advance(&mut self, byte: u8) -> Output<'_, B> {
let action = self.parser.advance(byte);

#[cfg(test)]
Expand Down Expand Up @@ -313,7 +313,7 @@ pub(crate) mod tests {
use crate::history::{NoHistory, StaticHistory, UnboundedHistory};
use crate::line_buffer::StaticBuffer;
use crate::terminal::Cursor;
use crate::testlib::{csi, AsByteVec, MockTerminal};
use crate::testlib::{csi, MockTerminal, ToByteVec};

use super::*;

Expand All @@ -330,7 +330,7 @@ pub(crate) mod tests {
let terminal = Initializer::init()
.iter()
.map(|&b| term.advance(b))
.filter_map(|output| output.and_then(|x| Some(x.into_iter())))
.filter_map(|output| output.map(|x| x.into_iter()))
.flatten()
.collect::<Vec<u8>>()
.into_iter()
Expand Down Expand Up @@ -363,19 +363,14 @@ pub(crate) mod tests {

let output: Vec<u8> = line
.reset()
.into_iter()
.filter_map(|item| {
item.get_bytes()
.and_then(|bytes| Some(bytes.iter().map(|&b| b).collect::<Vec<u8>>()))
})
.filter_map(|item| item.get_bytes().map(|bytes| bytes.to_vec()))
.flatten()
.filter_map(|b| mockterm.advance(b))
.flatten()
.collect();

output.into_iter().for_each(|b| {
line.advance(b)
.into_iter()
.for_each(|output| assert!(output.get_bytes().is_none()))
});

Expand All @@ -386,14 +381,14 @@ pub(crate) mod tests {
}
}

fn advance<'a, B: Buffer, H: History>(
fn advance<B: Buffer, H: History>(
terminal: &mut MockTerminal,
noline: &mut Line<'a, B, H>,
input: impl AsByteVec,
noline: &mut Line<'_, B, H>,
input: impl ToByteVec,
) -> core::result::Result<(), ()> {
terminal.bell = false;

for input in input.as_byte_vec() {
for input in input.to_byte_vec() {
for item in noline.advance(input) {
if let Some(bytes) = item.get_bytes() {
for &b in bytes {
Expand All @@ -414,7 +409,7 @@ pub(crate) mod tests {
}
}

fn get_terminal_and_editor<'a>(
fn get_terminal_and_editor(
rows: usize,
columns: usize,
origin: Cursor,
Expand Down
30 changes: 14 additions & 16 deletions noline/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,14 @@ impl<'a> IntoIterator for CircularSlice<'a> {
let (range1, range2) = self.get_ranges();
let (slice1, slice2) = self.get_slices();

range1
.zip(slice1.into_iter())
.chain(range2.zip(slice2.into_iter()))
range1.zip(slice1.iter()).chain(range2.zip(slice2.iter()))
}
}

/// Trait for line history
pub trait History: Default {
/// Return entry at index, or None if out of bounds
fn get_entry<'a>(&'a self, index: usize) -> Option<CircularSlice<'a>>;
fn get_entry(&self, index: usize) -> Option<CircularSlice<'_>>;

/// Add new entry at the end
fn add_entry<'a>(&mut self, entry: &'a str) -> Result<(), &'a str>;
Expand All @@ -188,9 +186,9 @@ pub trait History: Default {
///
/// This should ideally be in the [`History`] trait, but is
/// until `type_alias_impl_trait` is stable.
pub(crate) fn get_history_entries<'a, H: History>(
history: &'a H,
) -> impl Iterator<Item = CircularSlice<'a>> {
pub(crate) fn get_history_entries<H: History>(
history: &H,
) -> impl Iterator<Item = CircularSlice<'_>> {
(0..(history.number_of_entries())).filter_map(|index| history.get_entry(index))
}

Expand All @@ -213,7 +211,7 @@ impl<const N: usize> StaticHistory<N> {
CircularRange::new(self.window.end(), self.window.end(), N, N)
}

fn get_buffer<'a>(&'a self) -> CircularSlice<'a> {
fn get_buffer(&self) -> CircularSlice<'_> {
CircularSlice::new(
&self.buffer,
self.window.start(),
Expand All @@ -222,7 +220,7 @@ impl<const N: usize> StaticHistory<N> {
)
}

fn get_entry_ranges<'a>(&'a self) -> impl Iterator<Item = CircularRange> + 'a {
fn get_entry_ranges(&self) -> impl Iterator<Item = CircularRange> + '_ {
let delimeters =
self.get_buffer()
.into_iter()
Expand All @@ -231,7 +229,7 @@ impl<const N: usize> StaticHistory<N> {
[self.window.start()]
.into_iter()
.chain(delimeters.clone().map(|i| i + 1))
.zip(delimeters.chain([self.window.end()].into_iter()))
.zip(delimeters.chain([self.window.end()]))
.filter_map(|(start, end)| {
if start != end {
Some(CircularRange::new(start, end, self.window.len(), N))
Expand All @@ -241,7 +239,7 @@ impl<const N: usize> StaticHistory<N> {
})
}

fn get_entries<'a>(&'a self) -> impl Iterator<Item = CircularSlice<'a>> {
fn get_entries(&self) -> impl Iterator<Item = CircularSlice<'_>> {
self.get_entry_ranges()
.map(|range| CircularSlice::from_range(&self.buffer, range))
}
Expand Down Expand Up @@ -287,7 +285,7 @@ impl<const N: usize> History for StaticHistory<N> {
self.get_entries().count()
}

fn get_entry<'a>(&'a self, index: usize) -> Option<CircularSlice<'a>> {
fn get_entry(&self, index: usize) -> Option<CircularSlice<'_>> {
self.get_entries().nth(index)
}
}
Expand All @@ -308,7 +306,7 @@ impl Default for NoHistory {
}

impl History for NoHistory {
fn get_entry<'a>(&'a self, _index: usize) -> Option<CircularSlice<'a>> {
fn get_entry(&self, _index: usize) -> Option<CircularSlice<'_>> {
None
}

Expand Down Expand Up @@ -345,7 +343,7 @@ impl<'a, H: History> HistoryNavigator<'a, H> {
.get_or_insert_with(|| self.history.number_of_entries())
}

pub(crate) fn move_up<'b>(&'b mut self) -> Result<CircularSlice<'b>, ()> {
pub(crate) fn move_up(&mut self) -> Result<CircularSlice<'_>, ()> {
let position = self.get_position();

if position > 0 {
Expand All @@ -357,7 +355,7 @@ impl<'a, H: History> HistoryNavigator<'a, H> {
}
}

pub(crate) fn move_down<'b>(&'b mut self) -> Result<CircularSlice<'b>, ()> {
pub(crate) fn move_down(&mut self) -> Result<CircularSlice<'_>, ()> {
let new_position = self.get_position() + 1;

if new_position < self.history.number_of_entries() {
Expand Down Expand Up @@ -406,7 +404,7 @@ mod alloc {
}

impl History for UnboundedHistory {
fn get_entry<'a>(&'a self, index: usize) -> Option<CircularSlice<'a>> {
fn get_entry(&self, index: usize) -> Option<CircularSlice<'_>> {
let s = self.buffer[index].as_str();

Some(CircularSlice::new(s.as_bytes(), 0, s.len(), s.len()))
Expand Down
14 changes: 6 additions & 8 deletions noline/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use num_enum::{IntoPrimitive, TryFromPrimitive};

use crate::utf8::{Utf8Char, Utf8Decoder, Utf8DecoderStatus};

#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Eq, PartialEq, Copy, Clone, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum ControlCharacter {
Expand Down Expand Up @@ -49,6 +50,7 @@ impl ControlCharacter {
}
}

#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum CSI {
CUU(usize),
Expand Down Expand Up @@ -84,7 +86,6 @@ impl CSI {
'n' => Self::DSR,
'~' => {
if let Some(arg) = arg1 {
let arg = arg;
match arg {
1 => Self::Home,
3 => Self::Delete,
Expand Down Expand Up @@ -244,14 +245,14 @@ impl Parser {

#[cfg(test)]
pub(crate) mod tests {
use crate::testlib::AsByteVec;
use crate::testlib::ToByteVec;

use super::*;
use std::vec::Vec;
use ControlCharacter::*;

fn input_sequence(parser: &mut Parser, seq: impl AsByteVec) -> Vec<Action> {
seq.as_byte_vec()
fn input_sequence(parser: &mut Parser, seq: impl ToByteVec) -> Vec<Action> {
seq.to_byte_vec()
.into_iter()
.map(|b| parser.advance(b))
.collect()
Expand All @@ -263,10 +264,7 @@ pub(crate) mod tests {

assert_eq!(parser.state, State::Ground);

assert_eq!(
parser.advance('a' as u8),
Action::Print(Utf8Char::from_str("a"))
);
assert_eq!(parser.advance(b'a'), Action::Print(Utf8Char::from_str("a")));
assert_eq!(parser.advance(0x7), Action::ControlCharacter(CtrlG));
assert_eq!(parser.advance(0x3), Action::ControlCharacter(CtrlC));

Expand Down
16 changes: 9 additions & 7 deletions noline/src/line_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,11 @@ impl<B: Buffer> LineBuffer<B> {
unsafe { from_utf8_unchecked(self.as_slice()) }
}

fn char_ranges<'a>(&'a self) -> impl Iterator<Item = (Range<usize>, char)> + 'a {
fn char_ranges(&self) -> impl Iterator<Item = (Range<usize>, char)> + '_ {
let s = self.as_str();

s.char_indices()
.zip(
s.char_indices()
.skip(1)
.chain([(s.len(), '\0')].into_iter()),
)
.zip(s.char_indices().skip(1).chain([(s.len(), '\0')]))
.map(|((start, c), (end, _))| (start..end, c))
}

Expand Down Expand Up @@ -173,7 +169,7 @@ impl<B: Buffer> LineBuffer<B> {
pub fn insert_utf8_char(&mut self, char_index: usize, c: Utf8Char) -> Result<(), Utf8Char> {
unsafe {
self.insert_bytes(self.get_byte_position(char_index), c.as_bytes())
.or_else(|_| Err(c))
.map_err(|_| c)
}
}

Expand All @@ -183,6 +179,12 @@ impl<B: Buffer> LineBuffer<B> {
}
}

impl<B: Buffer> Default for LineBuffer<B> {
fn default() -> Self {
Self::new()
}
}

/// Emtpy buffer used for builder
pub struct NoBuffer {}

Expand Down
Loading

0 comments on commit 45a7358

Please sign in to comment.