Skip to content

Commit

Permalink
feat: top level messages implement From for all messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Leadbetter committed May 12, 2024
1 parent 7269e6b commit dd905c3
Show file tree
Hide file tree
Showing 48 changed files with 190 additions and 73 deletions.
51 changes: 50 additions & 1 deletion midi2_proc/src/generate_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ struct GenerateMessageArgs {
fixed_size: bool,
min_size_ump: Option<usize>,
min_size_bytes: Option<usize>,
via: Option<syn::Type>,
}

impl GenerateMessageArgs {
Expand All @@ -124,7 +125,9 @@ impl syn::parse::Parse for GenerateMessageArgs {
break;
};

let ident = ident.to_string();
if ident == "Via" {
args.via = Some(parse_via_args(input));
}
if ident == "FixedSize" {
args.fixed_size = true;
}
Expand Down Expand Up @@ -163,6 +166,22 @@ fn parse_fixed_size(input: syn::parse::ParseStream) -> usize {
.expect("Valid base 10 literal size")
}

fn parse_via_args(input: syn::parse::ParseStream) -> syn::Type {
let syn::ExprParen { expr, .. } = input
.parse()
.expect("Bracketed expression should follow size arg");

let syn::Expr::Path(path) = *expr
else {
panic!("Via argument should contain a path type");
};

syn::Type::Path(syn::TypePath{
qself: path.qself,
path: path.path,
})
}

fn imports() -> TokenStream {
quote! {
use crate::buffer::UnitPrivate as UnitPrivateGenMessage;
Expand Down Expand Up @@ -692,6 +711,26 @@ fn try_from_ump_impl(root_ident: &syn::Ident, properties: &Vec<Property>) -> Tok
}
}

fn ump_message_via(root_ident: &syn::Ident, via_type: &syn::Type) -> TokenStream {
quote! {
impl<B: crate::buffer::Ump> core::convert::From<#root_ident<B>> for crate::message::UmpMessage<B> {
fn from(value: #root_ident<B>) -> Self {
<#via_type<B> as core::convert::From<#root_ident<B>>>::from(value).into()
}
}
}
}

fn bytes_message_via(root_ident: &syn::Ident, via_type: &syn::Type) -> TokenStream {
quote! {
impl<B: crate::buffer::Bytes> core::convert::From<#root_ident<B>> for crate::message::BytesMessage<B> {
fn from(value: #root_ident<B>) -> Self {
<#via_type<B> as core::convert::From<#root_ident<B>>>::from(value).into()
}
}
}
}

pub fn generate_message(attrs: TokenStream1, item: TokenStream1) -> TokenStream1 {
let input = syn::parse_macro_input!(item as syn::ItemStruct);
let args = syn::parse_macro_input!(attrs as GenerateMessageArgs);
Expand Down Expand Up @@ -753,6 +792,16 @@ pub fn generate_message(attrs: TokenStream1, item: TokenStream1) -> TokenStream1
tokens.extend(try_from_ump_impl(root_ident, &properties));
}
}
if let Some(via_type) = args.via.as_ref() {
match args.representation() {
Representation::Ump => tokens.extend(ump_message_via(root_ident, &via_type)),
Representation::Bytes => tokens.extend(bytes_message_via(root_ident, &via_type)),
Representation::UmpOrBytes => {
tokens.extend(ump_message_via(root_ident, &via_type));
tokens.extend(bytes_message_via(root_ident, &via_type));
}
}
}

tokens.into()
}
2 changes: 1 addition & 1 deletion src/channel_voice1/channel_pressure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b1101;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(1), MinSizeBytes(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice1::ChannelVoice1), FixedSize, MinSizeUmp(1), MinSizeBytes(2))]
struct ChannelPressure {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
7 changes: 6 additions & 1 deletion src/channel_voice1/control_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ use crate::{

pub(crate) const STATUS: u8 = 0b1011;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(1), MinSizeBytes(3))]
#[midi2_proc::generate_message(
Via(crate::channel_voice1::ChannelVoice1),
FixedSize,
MinSizeUmp(1),
MinSizeBytes(3)
)]
struct ControlChange {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice1/key_pressure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b1010;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(1), MinSizeBytes(3))]
#[midi2_proc::generate_message(Via(crate::channel_voice1::ChannelVoice1), FixedSize, MinSizeUmp(1), MinSizeBytes(3))]
struct KeyPressure {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice1/note_off.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b1000;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(1), MinSizeBytes(3))]
#[midi2_proc::generate_message(Via(crate::channel_voice1::ChannelVoice1), FixedSize, MinSizeUmp(1), MinSizeBytes(3))]
struct NoteOff {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice1/note_on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b1001;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(1), MinSizeBytes(3))]
#[midi2_proc::generate_message(Via(crate::channel_voice1::ChannelVoice1), FixedSize, MinSizeUmp(1), MinSizeBytes(3))]
struct NoteOn {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice1/pitch_bend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b1110;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(1), MinSizeBytes(3))]
#[midi2_proc::generate_message(Via(crate::channel_voice1::ChannelVoice1), FixedSize, MinSizeUmp(1), MinSizeBytes(3))]
struct PitchBend {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice1/program_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b1100;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(1), MinSizeBytes(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice1::ChannelVoice1), FixedSize, MinSizeUmp(1), MinSizeBytes(2))]
struct ProgramChange {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice2/assignable_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b0011;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(2))]
struct AssignableController {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice2/assignable_per_note_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b0001;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(2))]
struct AssignablePerNoteController {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice2/channel_pitch_bend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b1110;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(2))]
struct ChannelPitchBend {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice2/channel_pressure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b1101;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(2))]
struct ChannelPressure {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice2/control_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b1011;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(2))]
struct ControlChange {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice2/key_pressure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b1010;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(2))]
struct KeyPressure {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice2/note_off.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b1000;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(2))]
struct NoteOff {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice2/note_on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) const STATUS: u8 = 0b1001;
///
/// assert_eq!(message.data(), &[0x4898_5E03, 0x6A14_E98A]);
/// ```
#[midi2_proc::generate_message(FixedSize, MinSizeUmp(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(2))]
struct NoteOn {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice2/per_note_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b1111;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(1))]
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(1))]
struct PerNoteManagement {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice2/per_note_pitch_bend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b0110;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(2))]
struct PerNotePitchBend {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice2/program_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b1100;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(2))]
struct ProgramChange {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice2/registered_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b0010;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(2))]
struct RegisteredController {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice2/registered_per_note_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b0000;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(2))]
struct RegisteredPerNoteController {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice2/relative_assignable_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b0101;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(2))]
struct RelativeAssignableController {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
2 changes: 1 addition & 1 deletion src/channel_voice2/relative_registered_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

pub(crate) const STATUS: u8 = 0b0100;

#[midi2_proc::generate_message(FixedSize, MinSizeUmp(2))]
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(2))]
struct RelativeRegisteredController {
#[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
ump_type: (),
Expand Down
Loading

0 comments on commit dd905c3

Please sign in to comment.