1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/// Musical accidentals (sharp, flat, natural, etc.).
///
/// Represents the accidentals that modify a note's pitch:
/// - Sharp (♯): raises the pitch by one semitone
/// - Flat (♭): lowers the pitch by one semitone
/// - Double sharp (𝄪): raises the pitch by two semitones
/// - Double flat (𝄫): lowers the pitch by two semitones
/// - Natural (♮): cancels previous accidentals
/// - Null: no accidental specified (uses key signature)
///
/// # Examples
///
/// ```
/// # use klavier_core::sharp_flat::SharpFlat;
/// let sharp = SharpFlat::Sharp; // Raises by 1 semitone
/// let flat = SharpFlat::Flat; // Lowers by 1 semitone
/// let natural = SharpFlat::Natural; // Cancels accidentals
/// ```
#[derive(serde::Deserialize, serde::Serialize)]
#[derive(Debug, PartialEq, Copy, Clone, Eq, Hash)]
pub enum SharpFlat {
/// Sharp (♯) - raises pitch by one semitone
Sharp,
/// Double sharp (𝄪) - raises pitch by two semitones
DoubleSharp,
/// Flat (♭) - lowers pitch by one semitone
Flat,
/// Double flat (𝄫) - lowers pitch by two semitones
DoubleFlat,
/// Natural (♮) - cancels previous accidentals
Natural,
/// No accidental specified (uses key signature)
Null,
}
impl SharpFlat {
/// Returns the pitch offset in semitones.
///
/// - Sharp: +1
/// - DoubleSharp: +2
/// - Flat: -1
/// - DoubleFlat: -2
/// - Natural: 0
/// - Null: 0
pub const fn offset(self) -> i32 {
match self {
Self::Sharp => 1,
Self::DoubleSharp => 2,
Self::Flat => -1,
Self::DoubleFlat => -2,
Self::Natural => 0,
Self::Null => 0,
}
}
}
#[cfg(test)]
mod tests {
use crate::sharp_flat::SharpFlat;
#[test]
fn offset_is_valid() {
assert_eq!(SharpFlat::Sharp.offset(), 1);
assert_eq!(SharpFlat::DoubleSharp.offset(), 2);
assert_eq!(SharpFlat::Flat.offset(), -1);
assert_eq!(SharpFlat::DoubleFlat.offset(), -2);
assert_eq!(SharpFlat::Natural.offset(), 0);
assert_eq!(SharpFlat::Null.offset(), 0);
}
}