From 2bdee01c989ff058cb57438a1e630b667bf24ef9 Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Fri, 26 May 2023 14:29:04 +0200 Subject: [PATCH 1/2] Add serde feature for de/ser of kord data structures --- Cargo.toml | 2 ++ src/core/chord.rs | 4 ++++ src/core/interval.rs | 4 ++++ src/core/known_chord.rs | 4 ++++ src/core/modifier.rs | 6 ++++++ src/core/named_pitch.rs | 4 ++++ src/core/note.rs | 4 ++++ src/core/octave.rs | 4 ++++ src/core/pitch.rs | 4 ++++ 9 files changed, 36 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 5f133b0..6e4c860 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,6 +51,8 @@ ml_train = ["ml_base", "rand", "rayon", "burn-autodiff", "burn/train", "burn/std ml_infer = ["ml_base", "burn", "burn-ndarray"] ml_gpu = ["ml_train", "burn-tch"] +serde = ["dep:serde"] + wasm = ["rodio/wasm-bindgen", "wasm-bindgen", "wasm-bindgen-futures", "js-sys", "console_error_panic_hook", "wee_alloc", "gloo-timers"] plot = ["plotters"] diff --git a/src/core/chord.rs b/src/core/chord.rs index 61935f3..8e181a8 100644 --- a/src/core/chord.rs +++ b/src/core/chord.rs @@ -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::{ @@ -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. diff --git a/src/core/interval.rs b/src/core/interval.rs index 89200d6..29df7a5 100644 --- a/src/core/interval.rs +++ b/src/core/interval.rs @@ -5,6 +5,9 @@ use std::fmt::{Display, Formatter, Error}; #[cfg(feature = "wasm")] use wasm_bindgen::prelude::*; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + use crate::core::octave::{HasOctave, Octave}; // Traits. @@ -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 { diff --git a/src/core/known_chord.rs b/src/core/known_chord.rs index 0535825..bac9c79 100644 --- a/src/core/known_chord.rs +++ b/src/core/known_chord.rs @@ -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. @@ -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. diff --git a/src/core/modifier.rs b/src/core/modifier.rs index 70dae69..0d42126 100644 --- a/src/core/modifier.rs +++ b/src/core/modifier.rs @@ -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. @@ -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. @@ -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. @@ -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. diff --git a/src/core/named_pitch.rs b/src/core/named_pitch.rs index 6abeade..5ab3404 100644 --- a/src/core/named_pitch.rs +++ b/src/core/named_pitch.rs @@ -2,6 +2,9 @@ use std::ops::{Add, Sub}; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + use crate::core::{ base::HasStaticName, pitch::{HasPitch, Pitch}, @@ -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. diff --git a/src/core/note.rs b/src/core/note.rs index 80d030b..625abea 100644 --- a/src/core/note.rs +++ b/src/core/note.rs @@ -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`]. @@ -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. diff --git a/src/core/octave.rs b/src/core/octave.rs index cd08817..c98b9f0 100644 --- a/src/core/octave.rs +++ b/src/core/octave.rs @@ -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. @@ -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, Ord, PartialOrd)] #[repr(u8)] pub enum Octave { diff --git a/src/core/pitch.rs b/src/core/pitch.rs index 3903ef7..c467704 100644 --- a/src/core/pitch.rs +++ b/src/core/pitch.rs @@ -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`]). @@ -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 { From 5e0e7e6946fd4234fd8ad188da6d37b9ec0d51cf Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Tue, 30 May 2023 21:45:12 +0200 Subject: [PATCH 2/2] Remove unneeded "dep:serde" --- Cargo.toml | 2 -- 1 file changed, 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6e4c860..5f133b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,8 +51,6 @@ ml_train = ["ml_base", "rand", "rayon", "burn-autodiff", "burn/train", "burn/std ml_infer = ["ml_base", "burn", "burn-ndarray"] ml_gpu = ["ml_train", "burn-tch"] -serde = ["dep:serde"] - wasm = ["rodio/wasm-bindgen", "wasm-bindgen", "wasm-bindgen-futures", "js-sys", "console_error_panic_hook", "wee_alloc", "gloo-timers"] plot = ["plotters"]