From 0aa2fcbbbb5cb52f87c4c4e4a2ed4f41de92c753 Mon Sep 17 00:00:00 2001 From: Stanislaw Grams Date: Thu, 12 Feb 2026 21:19:42 +0100 Subject: [PATCH] [refactor](trx-protocol): move transport dto out of core Phase 5: relocate ClientCommand/Envelope/Response into trx-protocol and update call sites so trx-core remains domain-focused. Co-authored-by: Codex Signed-off-by: Stanislaw Grams --- src/trx-client/src/remote_client.rs | 2 +- .../trx-frontend/trx-frontend-http/Cargo.toml | 1 + .../trx-frontend-rigctl/Cargo.toml | 1 + .../trx-frontend-rigctl/src/server.rs | 21 ++-------------- src/trx-core/src/lib.rs | 2 -- src/trx-protocol/src/codec.rs | 2 +- src/trx-protocol/src/lib.rs | 2 ++ src/trx-protocol/src/mapping.rs | 24 +++++++++---------- .../client.rs => trx-protocol/src/types.rs} | 4 +++- src/trx-server/src/listener.rs | 8 +++---- 10 files changed, 26 insertions(+), 41 deletions(-) rename src/{trx-core/src/client.rs => trx-protocol/src/types.rs} (93%) diff --git a/src/trx-client/src/remote_client.rs b/src/trx-client/src/remote_client.rs index 42a54e5..a56302d 100644 --- a/src/trx-client/src/remote_client.rs +++ b/src/trx-client/src/remote_client.rs @@ -10,11 +10,11 @@ use tokio::sync::{mpsc, watch}; use tokio::time::{self, Instant}; use tracing::{info, warn}; -use trx_core::client::{ClientCommand, ClientEnvelope, ClientResponse}; use trx_core::rig::request::RigRequest; use trx_core::rig::state::RigState; use trx_core::{RigError, RigResult}; use trx_protocol::rig_command_to_client; +use trx_protocol::{ClientCommand, ClientEnvelope, ClientResponse}; pub struct RemoteClientConfig { pub addr: String, diff --git a/src/trx-client/trx-frontend/trx-frontend-http/Cargo.toml b/src/trx-client/trx-frontend/trx-frontend-http/Cargo.toml index dea7c32..36e80cb 100644 --- a/src/trx-client/trx-frontend/trx-frontend-http/Cargo.toml +++ b/src/trx-client/trx-frontend/trx-frontend-http/Cargo.toml @@ -10,6 +10,7 @@ edition = "2021" [dependencies] trx-core = { path = "../../../trx-core" } trx-frontend = { path = ".." } +trx-protocol = { path = "../../../../src/trx-protocol" } tokio = { workspace = true, features = ["full"] } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } diff --git a/src/trx-client/trx-frontend/trx-frontend-rigctl/Cargo.toml b/src/trx-client/trx-frontend/trx-frontend-rigctl/Cargo.toml index bbab7c6..8d3173e 100644 --- a/src/trx-client/trx-frontend/trx-frontend-rigctl/Cargo.toml +++ b/src/trx-client/trx-frontend/trx-frontend-rigctl/Cargo.toml @@ -12,3 +12,4 @@ tokio = { workspace = true, features = ["full"] } tracing = { workspace = true } trx-core = { path = "../../../trx-core" } trx-frontend = { path = ".." } +trx-protocol = { path = "../../../../src/trx-protocol" } diff --git a/src/trx-client/trx-frontend/trx-frontend-rigctl/src/server.rs b/src/trx-client/trx-frontend/trx-frontend-rigctl/src/server.rs index 98ab896..47c6eb8 100644 --- a/src/trx-client/trx-frontend/trx-frontend-rigctl/src/server.rs +++ b/src/trx-client/trx-frontend/trx-frontend-rigctl/src/server.rs @@ -11,6 +11,7 @@ use tokio::sync::{mpsc, oneshot, watch}; use tokio::task::JoinHandle; use tokio::time::timeout; use tracing::{debug, error, info, warn}; +use trx_protocol::{mode_to_string, parse_mode}; use trx_core::radio::freq::Freq; use trx_core::rig::state::RigSnapshot; @@ -252,26 +253,8 @@ fn current_snapshot(state_rx: &watch::Receiver) -> Option state_rx.borrow().snapshot() } -fn parse_mode(s: &str) -> RigMode { - match s.to_ascii_uppercase().as_str() { - "LSB" => RigMode::LSB, - "USB" => RigMode::USB, - "CW" => RigMode::CW, - "CWR" => RigMode::CWR, - "AM" => RigMode::AM, - "FM" => RigMode::FM, - "WFM" => RigMode::WFM, - "DIG" | "DIGI" => RigMode::DIG, - "PKT" | "PACKET" => RigMode::PKT, - other => RigMode::Other(other.to_string()), - } -} - fn rig_mode_to_str(mode: &RigMode) -> String { - match mode { - RigMode::Other(other) => other.clone(), - other => format!("{:?}", other), - } + mode_to_string(mode) } fn dump_state_lines(_snapshot: &RigSnapshot) -> Vec { diff --git a/src/trx-core/src/lib.rs b/src/trx-core/src/lib.rs index c8b22ae..1d780e7 100644 --- a/src/trx-core/src/lib.rs +++ b/src/trx-core/src/lib.rs @@ -3,7 +3,6 @@ // SPDX-License-Identifier: BSD-2-Clause pub mod audio; -pub mod client; pub mod decode; pub mod math; pub mod radio; @@ -11,7 +10,6 @@ pub mod rig; pub type DynResult = Result>; -pub use client::{ClientCommand, ClientResponse}; pub use rig::command::RigCommand; pub use rig::request::RigRequest; pub use rig::response::{RigError, RigResult}; diff --git a/src/trx-protocol/src/codec.rs b/src/trx-protocol/src/codec.rs index 366e9ac..c610e5d 100644 --- a/src/trx-protocol/src/codec.rs +++ b/src/trx-protocol/src/codec.rs @@ -6,8 +6,8 @@ use serde_json; -use trx_core::client::{ClientCommand, ClientEnvelope}; use trx_core::rig::state::RigMode; +use crate::types::{ClientCommand, ClientEnvelope}; /// Parse a mode string into a RigMode. /// diff --git a/src/trx-protocol/src/lib.rs b/src/trx-protocol/src/lib.rs index 3f0334c..a165e4a 100644 --- a/src/trx-protocol/src/lib.rs +++ b/src/trx-protocol/src/lib.rs @@ -10,8 +10,10 @@ pub mod auth; pub mod codec; pub mod mapping; +pub mod types; // Re-export commonly used items pub use auth::{NoAuthValidator, SimpleTokenValidator, TokenValidator}; pub use codec::{mode_to_string, parse_envelope, parse_mode}; pub use mapping::{client_command_to_rig, rig_command_to_client}; +pub use types::{ClientCommand, ClientEnvelope, ClientResponse}; diff --git a/src/trx-protocol/src/mapping.rs b/src/trx-protocol/src/mapping.rs index 8a81f41..65a8dad 100644 --- a/src/trx-protocol/src/mapping.rs +++ b/src/trx-protocol/src/mapping.rs @@ -6,9 +6,9 @@ use trx_core::radio::freq::Freq; use trx_core::rig::command::RigCommand; -use trx_core::ClientCommand; use crate::codec::{mode_to_string, parse_mode}; +use crate::types::ClientCommand; /// Convert a ClientCommand to a RigCommand. /// @@ -123,7 +123,7 @@ mod tests { fn test_client_command_to_rig_set_ptt() { let cmd = ClientCommand::SetPtt { ptt: true }; if let RigCommand::SetPtt(ptt) = client_command_to_rig(cmd) { - assert_eq!(ptt, true); + assert!(ptt); } else { panic!("Expected SetPtt"); } @@ -203,7 +203,7 @@ mod tests { fn test_client_command_to_rig_set_aprs_decode_enabled() { let cmd = ClientCommand::SetAprsDecodeEnabled { enabled: true }; if let RigCommand::SetAprsDecodeEnabled(enabled) = client_command_to_rig(cmd) { - assert_eq!(enabled, true); + assert!(enabled); } else { panic!("Expected SetAprsDecodeEnabled"); } @@ -213,7 +213,7 @@ mod tests { fn test_client_command_to_rig_set_cw_decode_enabled() { let cmd = ClientCommand::SetCwDecodeEnabled { enabled: false }; if let RigCommand::SetCwDecodeEnabled(enabled) = client_command_to_rig(cmd) { - assert_eq!(enabled, false); + assert!(!enabled); } else { panic!("Expected SetCwDecodeEnabled"); } @@ -223,7 +223,7 @@ mod tests { fn test_client_command_to_rig_set_cw_auto() { let cmd = ClientCommand::SetCwAuto { enabled: true }; if let RigCommand::SetCwAuto(enabled) = client_command_to_rig(cmd) { - assert_eq!(enabled, true); + assert!(enabled); } else { panic!("Expected SetCwAuto"); } @@ -253,7 +253,7 @@ mod tests { fn test_client_command_to_rig_set_ft8_decode_enabled() { let cmd = ClientCommand::SetFt8DecodeEnabled { enabled: true }; if let RigCommand::SetFt8DecodeEnabled(enabled) = client_command_to_rig(cmd) { - assert_eq!(enabled, true); + assert!(enabled); } else { panic!("Expected SetFt8DecodeEnabled"); } @@ -333,7 +333,7 @@ mod tests { fn test_rig_command_to_client_set_ptt() { let cmd = RigCommand::SetPtt(true); if let ClientCommand::SetPtt { ptt } = rig_command_to_client(cmd) { - assert_eq!(ptt, true); + assert!(ptt); } else { panic!("Expected SetPtt"); } @@ -413,7 +413,7 @@ mod tests { fn test_rig_command_to_client_set_aprs_decode_enabled() { let cmd = RigCommand::SetAprsDecodeEnabled(true); if let ClientCommand::SetAprsDecodeEnabled { enabled } = rig_command_to_client(cmd) { - assert_eq!(enabled, true); + assert!(enabled); } else { panic!("Expected SetAprsDecodeEnabled"); } @@ -423,7 +423,7 @@ mod tests { fn test_rig_command_to_client_set_cw_decode_enabled() { let cmd = RigCommand::SetCwDecodeEnabled(false); if let ClientCommand::SetCwDecodeEnabled { enabled } = rig_command_to_client(cmd) { - assert_eq!(enabled, false); + assert!(!enabled); } else { panic!("Expected SetCwDecodeEnabled"); } @@ -433,7 +433,7 @@ mod tests { fn test_rig_command_to_client_set_cw_auto() { let cmd = RigCommand::SetCwAuto(true); if let ClientCommand::SetCwAuto { enabled } = rig_command_to_client(cmd) { - assert_eq!(enabled, true); + assert!(enabled); } else { panic!("Expected SetCwAuto"); } @@ -463,7 +463,7 @@ mod tests { fn test_rig_command_to_client_set_ft8_decode_enabled() { let cmd = RigCommand::SetFt8DecodeEnabled(true); if let ClientCommand::SetFt8DecodeEnabled { enabled } = rig_command_to_client(cmd) { - assert_eq!(enabled, true); + assert!(enabled); } else { panic!("Expected SetFt8DecodeEnabled"); } @@ -534,7 +534,7 @@ mod tests { let client_cmd = rig_command_to_client(rig_cmd); if let ClientCommand::SetPtt { ptt } = client_cmd { - assert_eq!(ptt, false); + assert!(!ptt); } else { panic!("Round trip failed"); } diff --git a/src/trx-core/src/client.rs b/src/trx-protocol/src/types.rs similarity index 93% rename from src/trx-core/src/client.rs rename to src/trx-protocol/src/types.rs index e98fda4..11d8bbc 100644 --- a/src/trx-core/src/client.rs +++ b/src/trx-protocol/src/types.rs @@ -2,9 +2,11 @@ // // SPDX-License-Identifier: BSD-2-Clause +//! Transport DTOs for the JSON line protocol. + use serde::{Deserialize, Serialize}; -use crate::rig::state::RigSnapshot; +use trx_core::rig::state::RigSnapshot; /// Command received from network clients (JSON). #[derive(Debug, Serialize, Deserialize)] diff --git a/src/trx-server/src/listener.rs b/src/trx-server/src/listener.rs index 5e760fc..df83475 100644 --- a/src/trx-server/src/listener.rs +++ b/src/trx-server/src/listener.rs @@ -5,7 +5,7 @@ //! JSON-over-TCP listener for trx-server. //! //! Accepts client connections speaking the `ClientEnvelope`/`ClientResponse` -//! protocol defined in `trx-core::client`. +//! protocol defined in `trx-protocol`. use std::collections::HashSet; use std::net::SocketAddr; @@ -19,11 +19,10 @@ use tracing::{error, info}; use trx_core::rig::command::RigCommand; use trx_core::rig::request::RigRequest; use trx_core::rig::state::RigState; -use trx_core::ClientResponse; - -use trx_protocol::codec::parse_envelope; use trx_protocol::auth::{SimpleTokenValidator, TokenValidator}; +use trx_protocol::codec::parse_envelope; use trx_protocol::mapping; +use trx_protocol::ClientResponse; /// Run the JSON TCP listener, accepting client connections. pub async fn run_listener( @@ -179,4 +178,3 @@ async fn handle_client( Ok(()) } -