From d5cdb5475d73eab10e4e25af0b3568ec310d2040 Mon Sep 17 00:00:00 2001 From: Martin Ling Date: Fri, 14 Jun 2024 17:11:15 +0100 Subject: [PATCH] Remove library target and special test features. --- .github/workflows/rust.yml | 2 +- Cargo.toml | 2 -- src/lib.rs | 23 ----------------------- src/main.rs | 36 ++++++++++++++++++++++++++++++++---- src/model/mod.rs | 12 ++++++------ src/record_ui.rs | 8 ++++---- src/test_cynthion.rs | 6 +++--- src/test_replay.rs | 10 +++++----- src/tree_list_model.rs | 8 ++++---- src/ui.rs | 30 +++++++++++++++--------------- 10 files changed, 70 insertions(+), 67 deletions(-) delete mode 100644 src/lib.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 94943621..cf351474 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -92,7 +92,7 @@ jobs: - name: Test uses: GabrielBB/xvfb-action@v1 with: - run: cargo test --features=test-ui-replay + run: cargo test - uses: actions/upload-artifact@v2 with: diff --git a/Cargo.toml b/Cargo.toml index 96e6c80a..28c7c98a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,6 +46,4 @@ ctor = "0.2.8" [features] step-decoder = [] record-ui-test = ["serde", "serde_json"] -test-ui-replay = ["serde", "serde_json"] debug-region-map = [] -test-cynthion = [] diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index 957e9b12..00000000 --- a/src/lib.rs +++ /dev/null @@ -1,23 +0,0 @@ -#[macro_use] -extern crate bitfield; - -pub mod backend; -pub mod capture; -mod compact_index; -mod data_stream; -pub mod decoder; -mod expander; -mod id; -mod index_stream; -pub mod model; -mod rcu; -pub mod row_data; -mod stream; -mod tree_list_model; -pub mod ui; -mod usb; -mod util; -mod vec_map; - -#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] -pub mod record_ui; diff --git a/src/main.rs b/src/main.rs index 06f8ed5a..1794039f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,46 @@ +// On Windows, produce a GUI app rather than a console one. #![windows_subsystem = "windows"] +// We need the bitfield macro. +#[macro_use] +extern crate bitfield; + // We need the ctor macro for the replay test on macOS. #[cfg(all(test, target_os="macos"))] #[allow(unused_imports)] #[macro_use] extern crate ctor; +// Declare all modules used. +mod backend; +mod capture; +mod compact_index; +mod data_stream; +mod decoder; +mod expander; +mod id; +mod index_stream; +mod model; +mod rcu; +mod row_data; +mod stream; +mod test_cynthion; +mod tree_list_model; +mod ui; +mod usb; +mod util; +mod vec_map; + +// Declare optional modules. +#[cfg(any(test, feature="record-ui-test"))] +mod record_ui; +#[cfg(test)] +mod test_replay; + use gtk::prelude::*; use gtk::gio::ApplicationFlags; -use packetry::ui::{ +use ui::{ activate, display_error, stop_cynthion @@ -29,6 +60,3 @@ fn main() { } } -mod test_cynthion; -#[cfg(test)] -mod test_replay; diff --git a/src/model/mod.rs b/src/model/mod.rs index 29a0d049..114afcfd 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -2,7 +2,7 @@ mod imp; -#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] +#[cfg(any(test, feature="record-ui-test"))] use { std::cell::RefCell, std::rc::Rc, @@ -26,7 +26,7 @@ glib::wrapper! { pub trait GenericModel where Self: Sized { fn new(capture: CaptureReader, - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] on_item_update: Rc>) -> Result; fn set_expanded(&self, @@ -41,14 +41,14 @@ pub trait GenericModel where Self: Sized { impl GenericModel for TrafficModel { fn new(capture: CaptureReader, - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] on_item_update: Rc>) -> Result { let model: TrafficModel = glib::Object::new::(); let tree = TreeListModel::new( capture, - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] on_item_update)?; model.imp().tree.replace(Some(tree)); Ok(model) @@ -86,14 +86,14 @@ impl GenericModel for TrafficModel { impl GenericModel for DeviceModel { fn new(capture: CaptureReader, - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] on_item_update: Rc>) -> Result { let model: DeviceModel = glib::Object::new::(); let tree = TreeListModel::new( capture, - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] on_item_update)?; model.imp().tree.replace(Some(tree)); Ok(model) diff --git a/src/record_ui.rs b/src/record_ui.rs index 0ae886cb..019fefcb 100644 --- a/src/record_ui.rs +++ b/src/record_ui.rs @@ -42,7 +42,7 @@ pub struct Recording { action_log: File, #[cfg(feature="record-ui-test")] output_log: File, - #[cfg(feature="test-ui-replay")] + #[cfg(test)] output_log: Option, view_items: HashMap>, } @@ -66,13 +66,13 @@ impl Recording { .truncate(true) .open("output.txt") .expect("Failed to open UI output log file"), - #[cfg(feature="test-ui-replay")] + #[cfg(test)] output_log: None, view_items: HashMap::new(), } } - #[cfg(feature="test-ui-replay")] + #[cfg(test)] pub fn set_output(&mut self, file: File) { self.output_log = Some(file) } @@ -98,7 +98,7 @@ impl Recording { fn log_output(&mut self, string: String) { #[cfg(feature="record-ui-test")] let output_log = &mut self.output_log; - #[cfg(feature="test-ui-replay")] + #[cfg(test)] let output_log = self.output_log .as_mut() .expect("Recording has no output file set"); diff --git a/src/test_cynthion.rs b/src/test_cynthion.rs index 3a69427c..71ea4337 100644 --- a/src/test_cynthion.rs +++ b/src/test_cynthion.rs @@ -1,6 +1,6 @@ -use packetry::backend::cynthion::{CynthionDevice, CynthionUsability, Speed}; -use packetry::capture::{create_capture, CaptureReader, DeviceId, EndpointId, EndpointTransferId}; -use packetry::decoder::Decoder; +use crate::backend::cynthion::{CynthionDevice, CynthionUsability, Speed}; +use crate::capture::{create_capture, CaptureReader, DeviceId, EndpointId, EndpointTransferId}; +use crate::decoder::Decoder; use anyhow::{Context, Error}; use futures_lite::future::block_on; diff --git a/src/test_replay.rs b/src/test_replay.rs index 8b78aa6f..4b03616f 100644 --- a/src/test_replay.rs +++ b/src/test_replay.rs @@ -8,11 +8,11 @@ use itertools::assert_equal; use pcap_file::pcap::PcapReader; use serde_json::Deserializer; -use packetry::decoder::Decoder; -use packetry::model::GenericModel; -use packetry::row_data::{GenericRowData, TrafficRowData, DeviceRowData}; -use packetry::record_ui::UiAction; -use packetry::ui::{ +use crate::decoder::Decoder; +use crate::model::GenericModel; +use crate::row_data::{GenericRowData, TrafficRowData, DeviceRowData}; +use crate::record_ui::UiAction; +use crate::ui::{ UserInterface, activate, reset_capture, diff --git a/src/tree_list_model.rs b/src/tree_list_model.rs index d396dee7..93a02512 100644 --- a/src/tree_list_model.rs +++ b/src/tree_list_model.rs @@ -351,7 +351,7 @@ pub struct TreeListModel { capture: RefCell, root: RootNodeRc, regions: RefCell>>, - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] on_item_update: Rc>, } @@ -362,7 +362,7 @@ where Item: 'static + Copy + Debug, CaptureReader: ItemSource, { pub fn new(mut capture: CaptureReader, - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] on_item_update: Rc>) -> Result { @@ -375,7 +375,7 @@ where Item: 'static + Copy + Debug, complete: completion.is_complete(), })), regions: RefCell::new(BTreeMap::new()), - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] on_item_update, }) } @@ -774,7 +774,7 @@ where Item: 'static + Copy + Debug, if item_updated { // The node's description may change. let summary = cap.summary(&item_node.item)?; - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] if let Ok(position) = u32::try_from(position) { let mut on_item_update = self.on_item_update.borrow_mut(); on_item_update(position, summary.clone()); diff --git a/src/ui.rs b/src/ui.rs index 1d6c2644..0cf5d2c8 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -37,7 +37,7 @@ use gtk::{ Orientation, }; -#[cfg(not(feature="test-ui-replay"))] +#[cfg(not(test))] use gtk::{ MessageDialog, DialogFlags, @@ -76,7 +76,7 @@ use crate::row_data::{ DeviceRowData}; use crate::util::{fmt_count, fmt_size}; -#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] +#[cfg(any(test, feature="record-ui-test"))] use { std::rc::Rc, crate::record_ui::Recording, @@ -272,7 +272,7 @@ pub struct UserInterface { capture_button: Button, stop_button: Button, status_label: Label, - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] pub recording: Rc>, } @@ -336,7 +336,7 @@ pub fn activate(application: &Application) -> Result<(), Error> { action_bar.pack_start(&stop_button); action_bar.pack_start(&selector.container); - #[cfg(not(feature="test-ui-replay"))] + #[cfg(not(test))] window.show(); WINDOW.with(|win_opt| win_opt.replace(Some(window.clone()))); @@ -403,7 +403,7 @@ pub fn activate(application: &Application) -> Result<(), Error> { UI.with(|cell| { cell.borrow_mut().replace( UserInterface { - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] recording: Rc::new(RefCell::new( Recording::new(capture.clone()))), capture, @@ -446,7 +446,7 @@ pub fn activate(application: &Application) -> Result<(), Error> { fn create_view( title: &str, capture: &CaptureReader, - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] recording_args: (&Rc>, &'static str)) -> (Model, ColumnView) where @@ -456,14 +456,14 @@ fn create_view( CaptureReader: ItemSource, Object: ToGenericRowData { - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] let (name, expand_rec, update_rec, changed_rec) = { let (recording, name) = recording_args; (name, recording.clone(), recording.clone(), recording.clone()) }; let model = Model::new( capture.clone(), - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] Rc::new( RefCell::new( move |position, summary| @@ -505,13 +505,13 @@ fn create_view( let model = bind_model.clone(); let node_ref = node_ref.clone(); let list_item = list_item.clone(); - #[cfg(any(feature="test-ui-replay", + #[cfg(any(test, feature="record-ui-test"))] let recording = expand_rec.clone(); let handler = expander.connect_expanded_notify(move |expander| { let position = list_item.position(); let expanded = expander.is_expanded(); - #[cfg(any(feature="test-ui-replay", + #[cfg(any(test, feature="record-ui-test"))] recording.borrow_mut().log_item_expanded( name, position, expanded); @@ -561,7 +561,7 @@ fn create_view( view.append_column(&column); view.add_css_class("data-table"); - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] model.connect_items_changed(move |model, position, removed, added| changed_rec.borrow_mut().log_items_changed( name, model, position, removed, added)); @@ -576,14 +576,14 @@ pub fn reset_capture() -> Result { create_view::( "Traffic", &reader, - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] (&ui.recording, "traffic") ); let (device_model, device_view) = create_view::( "Devices", &reader, - #[cfg(any(feature="test-ui-replay", feature="record-ui-test"))] + #[cfg(any(test, feature="record-ui-test"))] (&ui.recording, "devices") ); ui.capture = reader; @@ -910,7 +910,7 @@ pub fn stop_cynthion() -> Result<(), Error> { } pub fn display_error(result: Result<(), Error>) { - #[cfg(not(feature="test-ui-replay"))] + #[cfg(not(test))] if let Err(e) = result { use std::fmt::Write; let mut message = format!("{e}"); @@ -943,6 +943,6 @@ pub fn display_error(result: Result<(), Error>) { }); }); } - #[cfg(feature="test-ui-replay")] + #[cfg(test)] result.unwrap(); }