Skip to content

Commit

Permalink
use layer struct to parse some macros
Browse files Browse the repository at this point in the history
  • Loading branch information
Univa committed Mar 30, 2024
1 parent e17567c commit 4ee41ef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 45 deletions.
25 changes: 9 additions & 16 deletions rumcake-macros/src/backlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::{braced, Ident, Token};

use crate::keyboard::{MatrixLike, OptionalItem};
use crate::keyboard::{Layer, MatrixLike, OptionalItem};
use crate::TuplePair;

pub fn led_layout(input: MatrixLike<OptionalItem<TuplePair>>) -> TokenStream {
Expand Down Expand Up @@ -67,23 +67,15 @@ pub fn led_flags(input: MatrixLike<OptionalItem<LEDFlags>>) -> TokenStream {

#[derive(Debug)]
pub struct BacklightMatrixMacroInput {
pub led_layout_brace: syn::token::Brace,
pub led_layout: MatrixLike<OptionalItem<TuplePair>>,
pub led_flags_brace: syn::token::Brace,
pub led_flags: MatrixLike<OptionalItem<LEDFlags>>,
pub led_layout: Layer<OptionalItem<TuplePair>>,
pub led_flags: Layer<OptionalItem<LEDFlags>>,
}

impl Parse for BacklightMatrixMacroInput {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let led_layout_content;
let led_layout_brace = braced!(led_layout_content in input);
let led_flags_content;
let led_flags_brace = braced!(led_flags_content in input);
Ok(BacklightMatrixMacroInput {
led_layout_brace,
led_layout: led_layout_content.parse()?,
led_flags_brace,
led_flags: led_flags_content.parse()?,
led_layout: input.parse()?,
led_flags: input.parse()?,
})
}
}
Expand All @@ -96,15 +88,16 @@ pub fn setup_backlight_matrix(input: BacklightMatrixMacroInput) -> TokenStream {
} = input;

let first_row = led_layout
.layer
.rows
.first()
.expect_or_abort("Expected at least one row to be defined.");

let row_count = led_layout.rows.len();
let row_count = led_layout.layer.rows.len();
let col_count = first_row.cols.len();

let led_layout = self::led_layout(led_layout);
let led_flags = self::led_flags(led_flags);
let led_layout = self::led_layout(led_layout.layer);
let led_flags = self::led_flags(led_flags.layer);

quote! {
const LIGHTING_COLS: usize = #col_count;
Expand Down
42 changes: 13 additions & 29 deletions rumcake-macros/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,38 +929,29 @@ pub fn build_direct_pin_matrix(input: MatrixLike<OptionalItem<Ident>>) -> TokenS

#[derive(Debug)]
pub struct AnalogMatrixDefinition {
pub pos_to_ch_brace: syn::token::Brace,
pub pos_to_ch: MatrixLike<OptionalItem<TuplePair>>,
pub ranges_brace: syn::token::Brace,
pub ranges: MatrixLike<OptionalItem<ExprRange>>,
pub pos_to_ch: Layer<OptionalItem<TuplePair>>,
pub ranges: Layer<OptionalItem<ExprRange>>,
}

impl syn::parse::Parse for AnalogMatrixDefinition {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let pos_to_ch_content;
let pos_to_ch_brace = braced!(pos_to_ch_content in input);
let ranges_content;
let ranges_brace = braced!(ranges_content in input);

Ok(Self {
pos_to_ch_brace,
pos_to_ch: pos_to_ch_content.parse()?,
ranges_brace,
ranges: ranges_content.parse()?,
pos_to_ch: input.parse()?,
ranges: input.parse()?,
})
}
}

pub fn build_analog_matrix(input: AnalogMatrixDefinition) -> TokenStream {
let pos_to_ch = input.pos_to_ch.rows.iter().map(|row| {
let pos_to_ch = input.pos_to_ch.layer.rows.iter().map(|row| {
let items = row.cols.iter().map(|item| match item {
OptionalItem::None => quote! { (0, 0) },
OptionalItem::Some(tuple) => quote! { #tuple },
});
quote! { #(#items),* }
});

let ranges = input.ranges.rows.iter().map(|row| {
let ranges = input.ranges.layer.rows.iter().map(|row| {
let items = row.cols.iter().map(|item| match item {
OptionalItem::None => quote! { 0..0 },
OptionalItem::Some(range) => quote! { #range },
Expand All @@ -971,6 +962,7 @@ pub fn build_analog_matrix(input: AnalogMatrixDefinition) -> TokenStream {
let row_count = pos_to_ch.len();
let col_count = input
.pos_to_ch
.layer
.rows
.first()
.expect_or_abort("At least one row must be specified")
Expand Down Expand Up @@ -1143,29 +1135,21 @@ pub fn build_layout(raw: TokenStream, layers: LayoutLike<TokenTree>) -> TokenStr
}

pub struct RemapMacroInput {
pub original_matrix_brace: syn::token::Brace,
pub original_matrix: MatrixLike<OptionalItem<Ident>>,
pub remapped_matrix_brace: syn::token::Brace,
pub remapped_matrix: MatrixLike<Ident>,
pub original_matrix: Layer<OptionalItem<Ident>>,
pub remapped_matrix: Layer<Ident>,
}

impl Parse for RemapMacroInput {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let original_matrix_content;
let original_matrix_brace = braced!(original_matrix_content in input);
let remapped_matrix_content;
let remapped_matrix_brace = braced!(remapped_matrix_content in input);
Ok(RemapMacroInput {
original_matrix_brace,
original_matrix: original_matrix_content.parse()?,
remapped_matrix_brace,
remapped_matrix: remapped_matrix_content.parse()?,
original_matrix: input.parse()?,
remapped_matrix: input.parse()?,
})
}
}

pub fn remap_matrix(input: RemapMacroInput) -> TokenStream {
let old = input.original_matrix.rows.iter().map(|row| {
let old = input.original_matrix.layer.rows.iter().map(|row| {
let items = row.cols.iter().map(|col| match col {
OptionalItem::None => quote! { No },
OptionalItem::Some(ident) => quote! { $#ident },
Expand All @@ -1175,7 +1159,7 @@ pub fn remap_matrix(input: RemapMacroInput) -> TokenStream {
});
let old2 = old.clone();

let new = input.remapped_matrix.rows.iter().map(|row| {
let new = input.remapped_matrix.layer.rows.iter().map(|row| {
let items = row.cols.iter().map(|col| quote! { $#col:tt });
quote! { [ #(#items)* ] }
});
Expand Down

0 comments on commit 4ee41ef

Please sign in to comment.