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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
use arrow::{ array::{ ArrayBuilder, StructArray, }, datatypes::DataType, }; use super::{ action_state, frame, game, primitives::{Direction, Port}, }; use peppi_arrow::{Arrow, Context, SlippiVersion}; use peppi_derive::Peppi; #[derive(Clone, Copy, Debug)] pub struct Opts { pub avro_compatible: bool, } macro_rules! arrow { ( $( $type: ty : $arrow_type: ty ),* $(,)? ) => { $( impl Arrow for $type { type Builder = <$arrow_type as Arrow>::Builder; fn data_type<C: Context>(context: C) -> DataType { <$arrow_type>::data_type(context) } fn is_nullable() -> bool { <$arrow_type>::is_nullable() } fn builder<C: Context>(len: usize, context: C) -> Self::Builder { <$arrow_type>::builder(len, context) } fn append<C: Context>(&self, builder: &mut dyn ArrayBuilder, context: C) { <$arrow_type>::from(*self).append(builder, context) } fn append_null<C: Context>(builder: &mut dyn ArrayBuilder, context: C) { <$arrow_type>::append_null(builder, context) } } )* } } arrow!( Port: u8, Direction: u8, action_state::State: u16, ); #[derive(Clone, Copy, Debug)] struct PeppiContext { slippi_version: SlippiVersion, avro_compatible: bool, } impl Context for PeppiContext { fn slippi_version(&self) -> SlippiVersion { self.slippi_version } fn avro_compatible_field_names(&self) -> bool { self.avro_compatible } } fn _frames<const N: usize>(frames: &Vec<frame::Frame<N>>, context: PeppiContext) -> StructArray { let mut builder = frame::Frame::<N>::builder(frames.len(), context); for frame in frames { frame.append(&mut builder, context); } builder.finish() } fn context(game: &game::Game, opts: Option<Opts>) -> PeppiContext { let v = game.start.slippi.version; PeppiContext { slippi_version: SlippiVersion(v.0, v.1, v.2), avro_compatible: opts.map(|o| o.avro_compatible).unwrap_or(false), } } pub fn frames(game: &game::Game, opts: Option<Opts>) -> StructArray { use game::Frames::*; let c = context(game, opts); match &game.frames { P1(f) => _frames(f, c), P2(f) => _frames(f, c), P3(f) => _frames(f, c), P4(f) => _frames(f, c), } } #[derive(Peppi)] struct FrameItem { frame_index: u32, item: frame::Item, } fn _items<const N: usize>(frames: &Vec<frame::Frame<N>>, context: PeppiContext) -> Option<StructArray> { if frames[0].items.is_some() { let len = frames.iter().map(|f| f.items.as_ref().unwrap().len()).sum(); let mut builder = FrameItem::builder(len, context); for (idx, frame) in frames.iter().enumerate() { for item in frame.items.as_ref().unwrap() { FrameItem { frame_index: idx as u32, item: *item, }.append(&mut builder, context); } } Some(builder.finish()) } else { None } } pub fn items(game: &game::Game, opts: Option<Opts>) -> Option<StructArray> { use game::Frames::*; let c = context(game, opts); match &game.frames { P1(f) => _items(f, c), P2(f) => _items(f, c), P3(f) => _items(f, c), P4(f) => _items(f, c), } }