Skip to content

Commit

Permalink
Merge pull request #6 from barafael/serde-feature
Browse files Browse the repository at this point in the history
Add serde feature for de/ser of kord data structures
  • Loading branch information
twitchax committed May 31, 2023
2 parents 2dbd9d4 + 5e0e7e6 commit eba8cce
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/chord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

use std::{cmp::Ordering, collections::HashSet, fmt::Display};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use pest::Parser;

use crate::core::{
Expand Down Expand Up @@ -234,6 +237,7 @@ pub trait HasDomninantDegree {
// Struct.

/// The primary chord struct.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Chord {
/// The root note of the chord.
Expand Down
4 changes: 4 additions & 0 deletions src/core/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use std::fmt::{Display, Error, Formatter};
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::core::octave::{HasOctave, Octave};

// Traits.
Expand All @@ -31,6 +34,7 @@ pub trait CanReduceFrame {

/// An enum representing the interval between two notes.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(u8)]
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = KordInterval))]
pub enum Interval {
Expand Down
4 changes: 4 additions & 0 deletions src/core/known_chord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use crate::core::{
modifier::Degree,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// Traits.

/// A trait for types that have a relative scale.
Expand All @@ -32,6 +35,7 @@ pub trait HasRelativeChord {

/// An enum representing a known chord.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(u8)]
pub enum KnownChord {
/// An unknown chord.
Expand Down
6 changes: 6 additions & 0 deletions src/core/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use once_cell::sync::Lazy;
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::core::base::HasStaticName;

// Traits.
Expand All @@ -19,6 +22,7 @@ pub trait HasIsDominant {

/// An enum representing the degree of a dominant chord.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(u8)]
pub enum Degree {
/// Seventh degree.
Expand All @@ -38,6 +42,7 @@ pub enum Degree {
/// _just_ a dominant chord with a flat 9 extension, but rather a chord that is
/// represented by an entirely specific scale (half/whole/half diminished).
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(u8)]
pub enum Modifier {
/// Minor modifier.
Expand Down Expand Up @@ -72,6 +77,7 @@ pub enum Modifier {
/// and the chord is still interpreted as a major chord.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Ord, PartialOrd)]
#[repr(u8)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = KordExtension))]
pub enum Extension {
/// Sus2 extension.
Expand Down
4 changes: 4 additions & 0 deletions src/core/named_pitch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

use std::ops::{Add, Sub};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::core::{
base::HasStaticName,
pitch::{HasPitch, Pitch},
Expand Down Expand Up @@ -29,6 +32,7 @@ pub trait HasLetter {
/// While a [`Pitch`] is a pitch that has a frequency, a [`NamedPitch`] is a pitch that has an
/// enharmonic name (could share the same pitch with another).
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(u8)]
pub enum NamedPitch {
/// The pitch F triple flat.
Expand Down
4 changes: 4 additions & 0 deletions src/core/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ use pest::Parser;

use super::interval::ALL_INTERVALS;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// Macros.

/// Defines a note from a [`NamedPitch`].
Expand Down Expand Up @@ -159,6 +162,7 @@ pub trait ToUniversal {
///
/// This is a named pitch with an octave. This type allows for correctly attributing octave changes
/// across an interval from one [`Note`] to another.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub struct Note {
/// The octave of the note.
Expand Down
4 changes: 4 additions & 0 deletions src/core/octave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use once_cell::sync::Lazy;

use crate::core::base::HasStaticName;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// Traits.

/// A trait for types that have an octave property.
Expand All @@ -17,6 +20,7 @@ pub trait HasOctave {
// Enum.

/// An enum representing the octave of a note.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Default, Ord, PartialOrd)]
#[repr(u8)]
pub enum Octave {
Expand Down
4 changes: 4 additions & 0 deletions src/core/pitch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use once_cell::sync::Lazy;

use super::helpers::mel;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// A trait for types that have a pitch property.
pub trait HasPitch {
/// Returns the pitch of the type (usually a [`Note`]).
Expand Down Expand Up @@ -38,6 +41,7 @@ pub trait HasMel: HasFrequency {
///
/// The frequencies of the pitches are based on the [A4 frequency](https://en.wikipedia.org/wiki/A4_(pitch_standard)).
/// There is no enharmonic representation here, so all of the sharps are represented.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(PartialEq, Eq, Clone, Copy, Hash, Debug, Ord, PartialOrd)]
#[repr(u8)]
pub enum Pitch {
Expand Down

0 comments on commit eba8cce

Please sign in to comment.