From 2337c0497c09b11797794967a1cc68eb759c90f5 Mon Sep 17 00:00:00 2001 From: Noa Aarts Date: Tue, 10 Dec 2024 16:02:56 +0100 Subject: [PATCH] turn text and binary protocols into features --- Cargo.toml | 5 +++++ src/flutclient.rs | 32 ++++++++++++++++++++++++++------ src/main.rs | 7 ++++++- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 87d6260..7f87449 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,11 @@ tower-http = { version = "0.6.1", features = ["fs", "trace"] } tracing = "0.1.40" tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } +[features] +default = ["text", "binary"] +text = [] +binary = [] + [dev-dependencies] tempfile = "*" test-case = "*" diff --git a/src/flutclient.rs b/src/flutclient.rs index 22e6c70..fdfb362 100644 --- a/src/flutclient.rs +++ b/src/flutclient.rs @@ -14,17 +14,31 @@ use crate::{ }; macro_rules! build_parser_type_enum { - ($($name:ident: $t:ty,)*) => { + ($($name:ident: $t:ty: $feat:expr,)*) => { #[derive(Clone)] - enum ParserTypes { - $($name($t),)* + pub enum ParserTypes { + $( + #[cfg(feature = $feat)] + $name($t), + )* + } + + impl std::default::Default for ParserTypes { + // add code here + fn default() -> Self { + $( + #[cfg(feature = $feat)] + return ParserTypes::$name(<$t>::default()); + )* + } } macro_rules! match_parser { ($pident:ident: $parser:expr => $f:expr) => ( match &mut $parser { $( + #[cfg(feature = $feat)] ParserTypes::$name($pident) => $f, )* } @@ -34,8 +48,8 @@ macro_rules! build_parser_type_enum { } build_parser_type_enum! { - TextParser: TextParser, - BinaryParser: BinaryParser, + TextParser: TextParser: "text", + BinaryParser: BinaryParser: "binary", } pub struct FlutClient @@ -105,8 +119,14 @@ where fn change_protocol(&mut self, protocol: &Protocol) { match protocol { + #[cfg(feature="text")] Protocol::Text => self.parser = ParserTypes::TextParser(TextParser::default()), + #[cfg(not(feature="text"))] + Protocol::Text => {self.writer.write(b"feature \"text\" is not enabled."); self.writer.flush();} + #[cfg(feature = "binary")] Protocol::Binary => self.parser = ParserTypes::BinaryParser(BinaryParser::default()), + #[cfg(not(feature = "binary"))] + Protocol::Binary => {self.writer.write(b"feature \"binary\" is not enabled."); self.writer.flush();} } } @@ -115,7 +135,7 @@ where reader: BufReader::new(reader), writer: BufWriter::new(writer), grids, - parser: ParserTypes::TextParser(TextParser::default()), + parser: ParserTypes::default(), counter: 0, } } diff --git a/src/main.rs b/src/main.rs index 3112eb9..1d01da4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use std::{ use chrono::Local; use flurry::{ config::{GRID_LENGTH, HOST, IMAGE_SAVE_INTERVAL, JPEG_UPDATE_INTERVAL}, - flutclient::FlutClient, + flutclient::{FlutClient, }, grid::{self, Flut}, webapi::WebApiContext, AsyncResult, COUNTER, @@ -102,6 +102,11 @@ async fn main() { let grids: Arc<[Flut; GRID_LENGTH]> = [grid::Flut::init(800, 600, 0xff_00_ff_ff)].into(); tracing::trace!("created grids"); + #[cfg(feature = "text")] + println!("Enabling text"); + #[cfg(feature = "binary")] + println!("Enabling binary"); + let Ok(flut_listener) = TcpListener::bind(HOST).await else { tracing::error!( "Was unable to bind to {HOST}, please check if a different process is bound"