From 925659639a3d9bef6811db924e298ea9d0135ea0 Mon Sep 17 00:00:00 2001 From: alemi Date: Sun, 17 Sep 2023 03:51:44 +0200 Subject: [PATCH] chore: thiserror, removed mut + comments, editorcfg --- .editorconfig | 2 ++ Cargo.toml | 2 ++ src/music.rs | 38 +++++--------------------------------- src/parser.rs | 10 ++++------ 4 files changed, 13 insertions(+), 39 deletions(-) diff --git a/.editorconfig b/.editorconfig index c04d239..011b707 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,3 +6,5 @@ charset = utf-8 indent_style = tab indent_size = 4 +[*.rs] +indent_size = 2 diff --git a/Cargo.toml b/Cargo.toml index 634b5b6..6b7b03c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,5 @@ crossterm = "0.25" libpulse-binding = "2.0" libpulse-simple-binding = "2.25" clap = { version = "4.0.32", features = ["derive"] } +derive_more = "0.99.17" +thiserror = "1.0.48" diff --git a/src/music.rs b/src/music.rs index f616a58..56f5f13 100644 --- a/src/music.rs +++ b/src/music.rs @@ -5,7 +5,8 @@ pub enum Tone { C, Db, D, Eb, E, F, Gb, G, Ab, A, Bb, B } -pub struct ToneError {} +#[derive(Debug, thiserror::Error, derive_more::Display)] +pub struct ToneError(); #[derive(Debug, PartialEq, Clone)] pub struct Note { @@ -13,23 +14,12 @@ pub struct Note { octave: u32, } +#[derive(Debug, thiserror::Error, derive_more::From, derive_more::Display)] pub enum NoteError { InvalidOctave(ParseIntError), InalidNote(ToneError), } -impl From:: for NoteError { - fn from(e: ToneError) -> Self { - NoteError::InalidNote(e) - } -} - -impl From:: for NoteError { - fn from(e: ParseIntError) -> Self { - NoteError::InvalidOctave(e) - } -} - impl FromStr for Note { type Err = NoteError; @@ -52,13 +42,6 @@ impl FromStr for Note { } } -// impl TryFrom:: for Note { -// type Error = NoteError; -// fn try_from(value: String) -> Result { -// value.as_str().parse::() -// } -// } - impl FromStr for Tone { type Err = ToneError; @@ -76,7 +59,7 @@ impl FromStr for Tone { "A" => Ok(Tone::A ), "A#" | "Bb" => Ok(Tone::Bb), "B" => Ok(Tone::B ), - _ => Err(ToneError { }) + _ => Err(ToneError()) } } } @@ -85,15 +68,8 @@ impl Note { pub fn tune_buffer_size(&self, sample_rate: u32) -> u32 { let t = 1.0 / self.tone.freq(self.octave); // periodo ? let buf = (sample_rate as f32) * t; - return (buf * 4.0).round() as u32; + (buf * 4.0).round() as u32 } - - // pub fn tune_sample_rate(&self, octave:u32, buffer_size: u32) -> u32 { - // // TODO does it just work the same way? - // let t = 1.0 / self.freq(octave); // periodo ? - // let buf = (buffer_size as f32) * t; - // return (buf * 4.0).round() as u32; - // } } impl Tone { @@ -122,9 +98,5 @@ impl Tone { } } } - - // pub fn all() -> Vec { - // vec![Note::C, Note::Db, Note::D, Note::Eb, Note::E, Note::F, Note::Gb, Note::G, Note::Ab, Note::A, Note::Bb, Note::B] - // } } diff --git a/src/parser.rs b/src/parser.rs index 6f7f147..aa4ae3b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -8,15 +8,15 @@ // } pub trait SampleParser { - fn oscilloscope(&self, data: &mut [u8], channels: u8) -> Vec>; + fn oscilloscope(&self, data: &[u8], channels: u8) -> Vec>; fn sample_size(&self) -> usize; } -pub struct Signed16PCM {} +pub struct Signed16PCM(); /// TODO these are kinda inefficient, can they be faster? impl SampleParser for Signed16PCM { - fn oscilloscope(&self, data: &mut [u8], channels: u8) -> Vec> { + fn oscilloscope(&self, data: &[u8], channels: u8) -> Vec> { let mut out = vec![vec![]; channels as usize]; let mut channel = 0; for chunk in data.chunks(2) { @@ -27,7 +27,5 @@ impl SampleParser for Signed16PCM { out } - fn sample_size(&self) -> usize { - return 2; // 16 bit, thus 2 bytes - } + fn sample_size(&self) -> usize { 2 } // 16 bit, thus 2 bytes }