Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V0.0.2 dev #28

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions core/src/chords/dyad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
Contrib: FL03 <[email protected]>
*/
use crate::notes::Note;
use crate::{Intervals, Pair};
use crate::{Interval, Pair};

fn _interval<A, B>(lhs: A, rhs: B) -> Intervals
fn _interval<A, B>(lhs: A, rhs: B) -> Interval
where
A: core::ops::Sub<B, Output = Intervals>,
A: core::ops::Sub<B, Output = Interval>,
{
lhs - rhs
}
Expand All @@ -16,19 +16,19 @@ where
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Dyad {
chord: Pair<Note>,
interval: Intervals,
interval: Interval,
}

impl Dyad {
pub fn new(src: Note, tgt: Note) -> Self {
let chord = Pair::new(src, tgt);
let interval = Intervals::new(src, tgt);
let interval = Interval::new(src, tgt);
Self { chord, interval }
}

pub fn from_tuple((lhs, rhs): (Note, Note)) -> Self {
let chord = Pair::new(lhs, rhs);
let interval = Intervals::new(lhs, rhs);
let interval = Interval::new(lhs, rhs);
Self { chord, interval }
}

Expand All @@ -40,11 +40,11 @@ impl Dyad {
&mut self.chord
}

pub const fn interval(&self) -> &Intervals {
pub const fn interval(&self) -> &Interval {
&self.interval
}

pub fn interval_mut(&mut self) -> &mut Intervals {
pub fn interval_mut(&mut self) -> &mut Interval {
&mut self.interval
}
}
53 changes: 1 addition & 52 deletions core/src/error/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,12 @@
Appellation: err <module>
Contrib: FL03 <[email protected]>
*/

pub trait FluoError: core::fmt::Debug + core::fmt::Display + Send + Sync + 'static {}

impl<T> FluoError for T where T: core::fmt::Debug + core::fmt::Display + Send + Sync + 'static {}
use super::MusicalError;

pub trait ErrorKind: core::fmt::Debug + core::fmt::Display {}

impl<T> ErrorKind for T where T: core::fmt::Debug + core::fmt::Display {}

#[derive(
Clone,
Copy,
Debug,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
strum::AsRefStr,
strum::Display,
strum::EnumIs,
strum::EnumString,
strum::VariantNames,
)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(rename_all = "PascalCase")
)]
#[strum(serialize_all = "PascalCase")]
pub enum MusicalError {
InvalidInterval,
InvalidPitch,
}

#[cfg(feature = "std")]
impl std::error::Error for MusicalError {}

unsafe impl Send for MusicalError {}

unsafe impl Sync for MusicalError {}

#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(transparent)]
Expand Down Expand Up @@ -148,18 +112,3 @@ unsafe impl<K> Sync for Error<K> where K: ErrorKind {}

#[cfg(feature = "std")]
impl<K> std::error::Error for Error<K> where K: ErrorKind {}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_error() {
let err = Error::new(
MusicalError::InvalidInterval,
"Invalid interval".to_string(),
);
assert_eq!(err.kind(), &MusicalError::InvalidInterval);
assert_eq!(err.msg(), "Invalid interval");
}
}
37 changes: 37 additions & 0 deletions core/src/error/kinds/music.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Appellation: music <module>
Contrib: FL03 <[email protected]>
*/

#[derive(
Clone,
Copy,
Debug,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
strum::AsRefStr,
strum::Display,
strum::EnumIs,
strum::EnumString,
strum::VariantNames,
)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(rename_all = "PascalCase")
)]
#[strum(serialize_all = "PascalCase")]
pub enum MusicalError {
InvalidInterval,
InvalidPitch,
}

#[cfg(feature = "std")]
impl std::error::Error for MusicalError {}

unsafe impl Send for MusicalError {}

unsafe impl Sync for MusicalError {}
28 changes: 25 additions & 3 deletions core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,36 @@
Appellation: errors <module>
Contrib: FL03 <[email protected]>
*/
pub use self::err::*;
pub use self::{err::*, kinds::*};

pub(crate) mod err;

/// A type alias for `Result<T, Error>`.
pub type Result<T = ()> = core::result::Result<T, Error>;
pub mod kinds {
pub use self::music::*;

pub(crate) mod music;
}

pub(crate) mod prelude {
pub use super::err::*;
pub use super::kinds::*;
pub use super::Result;
}

/// A type alias for `Result<T, Error>`.
pub type Result<T = ()> = core::result::Result<T, Error>;

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_error() {
let err = Error::new(
MusicalError::InvalidInterval,
"Invalid interval".to_string(),
);
assert_eq!(err.kind(), &MusicalError::InvalidInterval);
assert_eq!(err.msg(), "Invalid interval");
}
}
57 changes: 0 additions & 57 deletions core/src/intervals/interval.rs

This file was deleted.

51 changes: 26 additions & 25 deletions core/src/intervals/kinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{IntoPitch, Pitch};
)]
#[repr(u8)]
#[strum(serialize_all = "lowercase")]
pub enum Intervals {
pub enum Interval {
#[default]
Semitone = 1,
Tone = 2,
Expand All @@ -39,19 +39,19 @@ pub enum Intervals {
Octave = 12,
}

impl Intervals {
impl Interval {
pub fn new<A, B, C>(lhs: A, rhs: B) -> Self
where
A: core::ops::Sub<B, Output = C>,
C: Into<Intervals>,
C: Into<Interval>,
{
(lhs - rhs).into()
}
/// Use the difference between two pitches to determine the interval.
pub fn from_value(value: impl IntoPitch) -> Self {
use Intervals::*;
use Interval::*;
let pitch = value.into_pitch();
match *pitch {
match *pitch.absmod() {
1 => Semitone,
2 => Tone,
3 => Thirds(Third::Minor),
Expand All @@ -69,31 +69,32 @@ impl Intervals {
}
/// A convenience method for constructing a new instance of the [Octave](Intervals::Octave) variant.
pub fn octave() -> Self {
Intervals::Octave
Interval::Octave
}
/// A convenience method for constructing a new instance of the [Semitone](Intervals::Semitone) variant.
pub fn semitone() -> Self {
Intervals::Semitone
Interval::Semitone
}
/// A convenience method for constructing a new instance of the [Tone](Intervals::Tone) variant.
pub fn tone() -> Self {
Intervals::Tone
Interval::Tone
}
/// A convenience method for constructing a new variant, [`Thirds`](Intervals::Thirds).
pub fn third(third: Third) -> Self {
Intervals::Thirds(third)
Interval::Thirds(third)
}

/// A convenience method for constructing a new variant, [`Fourths`](Intervals::Fourths).
pub fn fourth(fourth: Fourth) -> Self {
Intervals::Fourths(fourth)
Interval::Fourths(fourth)
}
/// A convenience method for constructing a new variant, [`Fifths`](Intervals::Fifths).
pub fn fifth(fifth: Fifth) -> Self {
Intervals::Fifths(fifth)
Interval::Fifths(fifth)
}
/// A convenience method for constructing a new variant, [`Sevenths`](Intervals::Sevenths).
pub fn seventh(seventh: Seventh) -> Self {
Intervals::Sevenths(seventh)
Interval::Sevenths(seventh)
}
/// Interpret the current interval as a pitch.
pub fn as_pitch(&self) -> Pitch {
Expand All @@ -106,13 +107,13 @@ impl Intervals {
/// Returns the value of the selected interval.
pub fn value(&self) -> i8 {
match *self {
Intervals::Semitone => 1,
Intervals::Tone => 2,
Intervals::Thirds(third) => third as i8,
Intervals::Fourths(fourth) => fourth as i8,
Intervals::Fifths(fifth) => fifth as i8,
Intervals::Sevenths(seventh) => seventh as i8,
Intervals::Octave => 12,
Interval::Semitone => 1,
Interval::Tone => 2,
Interval::Thirds(third) => third as i8,
Interval::Fourths(fourth) => fourth as i8,
Interval::Fifths(fifth) => fifth as i8,
Interval::Sevenths(seventh) => seventh as i8,
Interval::Octave => 12,
}
}
}
Expand All @@ -132,20 +133,20 @@ macro_rules! impl_from_value {
};
}

impl<P> From<P> for Intervals
impl<P> From<P> for Interval
where
P: IntoPitch,
{
fn from(value: P) -> Self {
Intervals::from_value(value)
Interval::from_value(value)
}
}

impl_from_value! {
Intervals::Thirds(Third),
Intervals::Fourths(Fourth),
Intervals::Fifths(Fifth),
Intervals::Sevenths(Seventh),
Interval::Thirds(Third),
Interval::Fourths(Fourth),
Interval::Fifths(Fifth),
Interval::Sevenths(Seventh),
}

interval! {
Expand Down
Loading