From 2337c0497c09b11797794967a1cc68eb759c90f5 Mon Sep 17 00:00:00 2001 From: Noa Aarts Date: Tue, 10 Dec 2024 16:02:56 +0100 Subject: [PATCH 1/4] 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" From e391d3f63f9a630d06fb8a9e6eaefe350fa0faf9 Mon Sep 17 00:00:00 2001 From: Noa Aarts Date: Tue, 10 Dec 2024 16:04:18 +0100 Subject: [PATCH 2/4] allow unreachable in default protocol return --- src/flutclient.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/flutclient.rs b/src/flutclient.rs index fdfb362..4a97511 100644 --- a/src/flutclient.rs +++ b/src/flutclient.rs @@ -28,6 +28,7 @@ macro_rules! build_parser_type_enum { // add code here fn default() -> Self { $( + #[allow(unreachable_code)] #[cfg(feature = $feat)] return ParserTypes::$name(<$t>::default()); )* From 22281e21f81d88a3d13dae51c9d79d2881769ba0 Mon Sep 17 00:00:00 2001 From: Noa Aarts Date: Tue, 10 Dec 2024 17:22:31 +0100 Subject: [PATCH 3/4] use announce macro instead of having to hardcode --- src/flutclient.rs | 31 ++++++++++++++++++++++++------- src/main.rs | 7 ++----- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/flutclient.rs b/src/flutclient.rs index 4a97511..28a7a4f 100644 --- a/src/flutclient.rs +++ b/src/flutclient.rs @@ -19,7 +19,7 @@ macro_rules! build_parser_type_enum { #[derive(Clone)] pub enum ParserTypes { $( - #[cfg(feature = $feat)] + #[cfg(feature = $feat)] $name($t), )* } @@ -28,18 +28,29 @@ macro_rules! build_parser_type_enum { // add code here fn default() -> Self { $( - #[allow(unreachable_code)] #[cfg(feature = $feat)] + #[allow(unreachable_code)] return ParserTypes::$name(<$t>::default()); )* } } + impl ParserTypes { + pub fn announce() { + $( + #[cfg(feature = $feat)] + println!("Enabling {}", $feat); + #[cfg(not(feature = $feat))] + println!("Keeping {} disabled", $feat); + )* + } + } + macro_rules! match_parser { ($pident:ident: $parser:expr => $f:expr) => ( match &mut $parser { $( - #[cfg(feature = $feat)] + #[cfg(feature = $feat)] ParserTypes::$name($pident) => $f, )* } @@ -120,14 +131,20 @@ where fn change_protocol(&mut self, protocol: &Protocol) { match protocol { - #[cfg(feature="text")] + #[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(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();} + Protocol::Binary => { + self.writer.write(b"feature \"binary\" is not enabled."); + self.writer.flush(); + } } } diff --git a/src/main.rs b/src/main.rs index 1d01da4..3581e50 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, ParserTypes}, grid::{self, Flut}, webapi::WebApiContext, AsyncResult, COUNTER, @@ -102,10 +102,7 @@ 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"); + ParserTypes::announce(); let Ok(flut_listener) = TcpListener::bind(HOST).await else { tracing::error!( From 48edab8b135a0156a4788eab9964294c493180ee Mon Sep 17 00:00:00 2001 From: Noa Aarts Date: Tue, 10 Dec 2024 18:03:01 +0100 Subject: [PATCH 4/4] use tracing for enabled/disabled features --- src/flutclient.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/flutclient.rs b/src/flutclient.rs index 28a7a4f..ddd9f2f 100644 --- a/src/flutclient.rs +++ b/src/flutclient.rs @@ -39,9 +39,9 @@ macro_rules! build_parser_type_enum { pub fn announce() { $( #[cfg(feature = $feat)] - println!("Enabling {}", $feat); + tracing::info!("Enabled {}", $feat); #[cfg(not(feature = $feat))] - println!("Keeping {} disabled", $feat); + tracing::info!("Disabled {}", $feat); )* } }