rytm_rs/query/
pattern.rs

1use super::ObjectQuery;
2use crate::{
3    error::{ParameterError, RytmError},
4    sysex::{AnySysexType, SysexType},
5};
6use rytm_rs_macro::parameter_range;
7
8#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
9/// A query to retrieve a [`Pattern`](crate::object::Pattern) object from rytm.
10pub struct PatternQuery {
11    /// Pattern index
12    object_number: usize,
13    sysex_type: SysexType,
14    device_id: u8,
15}
16
17impl PatternQuery {
18    /// Creates a new pattern query.
19    ///
20    /// Accepts a pattern index in the range of `0..=127`.
21    #[parameter_range(range = "pattern_index:0..=127")]
22    pub fn new(pattern_index: usize) -> Result<Self, RytmError> {
23        Ok(Self {
24            object_number: pattern_index,
25            sysex_type: SysexType::Pattern,
26            device_id: 0,
27        })
28    }
29
30    /// Creates a new pattern query.
31    ///
32    /// Accepts a pattern index in the range of `0..=127`.
33    ///
34    /// Accepts a device id in the range of `0..=255`.
35    #[parameter_range(range = "pattern_index:0..=127")]
36    pub fn new_with_device_id(pattern_index: usize, device_id: u8) -> Result<Self, RytmError> {
37        Ok(Self {
38            object_number: pattern_index,
39            sysex_type: SysexType::Pattern,
40            device_id,
41        })
42    }
43
44    /// Creates a new pattern query for the pattern in the work buffer.
45    pub const fn new_targeting_work_buffer() -> Self {
46        Self {
47            object_number: 0b1000_0000,
48            sysex_type: SysexType::Pattern,
49            device_id: 0,
50        }
51    }
52
53    /// Creates a new pattern query for the pattern in the work buffer.
54    ///
55    /// Accepts a device id in the range of `0..=255`.
56    pub const fn new_targeting_work_buffer_with_device_id(device_id: u8) -> Self {
57        Self {
58            object_number: 0b1000_0000,
59            sysex_type: SysexType::Pattern,
60            device_id,
61        }
62    }
63}
64
65impl ObjectQuery for PatternQuery {
66    fn sysex_type(&self) -> AnySysexType {
67        self.sysex_type.into()
68    }
69
70    fn device_id(&self) -> u8 {
71        self.device_id
72    }
73
74    fn obj_nr(&self) -> u16 {
75        self.object_number as u16
76    }
77}