Compare commits

...

1 Commits
main ... test

Author SHA1 Message Date
Dandaedre 6d96752983 Adds test indications 2024-09-28 05:18:37 -05:00
5 changed files with 48 additions and 1 deletions

View File

@ -16,6 +16,7 @@ use ui_struct::AppInfo;
use flexi_logger::{FileSpec, Logger, WriteMode};
use log;
// TEST: SKIP
fn main() -> Result<(), TinyError> {
let _logger = Logger::try_with_str("info, my::critical::module=trace")
.unwrap()

View File

@ -40,6 +40,7 @@ pub struct DataPoint {
static EOF: OnceLock<memmem::Finder<'_>> = OnceLock::new();
/// Scan a specific range and returns the scan result
// TEST: CLEANUP
pub(crate) fn scan_with_progress(connection_info: ConnectionInfo) -> Result<(), TinyError> {
let mode = mode_from(connection_info.start, connection_info.stop)?;
let mut port = acquire_port(&connection_info)?;
@ -125,6 +126,8 @@ pub(crate) fn scan_with_progress(connection_info: ConnectionInfo) -> Result<(),
Ok(())
}
/// Acquire a port from the portname/baudrate of the connection info
// TEST: Abstract port / Hard
fn acquire_port(connection_info: &ConnectionInfo) -> Result<Box<dyn SerialPort>, TinyError> {
let port = {
connection_info.update_message("Opening connection");
@ -142,6 +145,7 @@ fn acquire_port(connection_info: &ConnectionInfo) -> Result<Box<dyn SerialPort>,
}
/// Get the mode of operation from the start & stop scan values
// TEST: Easy
fn mode_from(start: usize, stop: usize) -> Result<String, TinyError> {
if start > stop {
return Err(TinyError::BadScanFrequencies(format!(
@ -161,6 +165,7 @@ fn mode_from(start: usize, stop: usize) -> Result<String, TinyError> {
/// A (lame) function used for initialisation:
/// Wait's for a whole timeout for data stuck in the buffer and dumps it
// TEST: Abstract port
fn initialize(port: &mut Box<dyn SerialPort>) {
wait_sync(SHORT_WAIT);
let d = read_port(port);
@ -172,11 +177,13 @@ fn initialize(port: &mut Box<dyn SerialPort>) {
}
/// Bad synchronisation method to ensure data is truthfully sent/received
// TEST: Abstract port
fn wait_sync(duration: u64) {
thread::sleep(Duration::from_millis(duration));
}
/// Scan raw frequencies
// TEST: Abstract port
fn scanraw(
port: &mut Box<dyn SerialPort>,
connection_info: ConnectionInfo,
@ -208,6 +215,7 @@ fn send_cmd(port: &mut Box<dyn SerialPort>, command: &str, wait: u64) -> Result<
/// Send a command without reading the output buffer
/// This command will produce unknown results if the output buffer is never read
// TEST: Abstract port
unsafe fn send_raw(port: &mut Box<dyn SerialPort>, command: &str) -> Result<(), TinyError> {
let output = command.as_bytes();
let _r = port.write(output).map_err(TinyError::SendError)?;
@ -215,6 +223,7 @@ unsafe fn send_raw(port: &mut Box<dyn SerialPort>, command: &str) -> Result<(),
}
/// Reads from port and tries to convert it to string with loss
// TEST: Abstract port
fn read_utf8(port: &mut Box<dyn SerialPort>) -> String {
String::from_utf8_lossy(&read_port(port)).to_string()
}
@ -245,6 +254,7 @@ fn read_port_with_progress(
/// Read port without updating progress
// TODO: Find a way to avoid code duplication
// TEST: Abstract port
fn read_port(port: &mut Box<dyn SerialPort>) -> Vec<u8> {
let mut serial_buf: Vec<u8> = vec![0; 256];
let mut res = Vec::new();
@ -263,6 +273,7 @@ fn read_port(port: &mut Box<dyn SerialPort>) -> Vec<u8> {
/// Waits the normal start Character '{' and begins treating data as normal
///
/// Returns a struct summerizing the information processed
// TEST: Abstract port
fn read_scanraw(
port: &mut Box<dyn SerialPort>,
connection_info: ConnectionInfo,
@ -282,6 +293,7 @@ fn read_scanraw(
/// A utility function used for parsing the raw data of a scan
// TODO: Split save data to another function
// TEST: MANDATOY
pub fn parse_data(
v: Vec<u8>,
points: usize,
@ -354,6 +366,8 @@ pub fn parse_data(
}
/// Utility function converting the received data as u16 to float data
// TEST: Easy
#[inline]
fn u16_to_f64(v: u16) -> f64 {
(v as f64) / 32. - OFFSET as f64
}

View File

@ -69,6 +69,7 @@ pub(crate) enum InputDirection {
Left,
}
// TEST: Easy
/// All implementation of errors happening while in the UI
#[derive(thiserror::Error, Debug)]
pub enum TinyUIError {
@ -101,6 +102,7 @@ pub enum TinyUIError {
SerializeError(#[from] toml::ser::Error),
}
// TEST: Easy
#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub(crate) struct SavedConfig {
/// The default com port
@ -116,6 +118,7 @@ pub(crate) struct SavedConfig {
/// The default number of repeat per point
pub(crate) repeat: usize,
}
// TEST: Easy
impl SavedConfig {
/// Create a config from file or returns an error
pub(crate) fn new() -> Result<Self, TinyError> {
@ -147,6 +150,7 @@ impl SavedConfig {
}
}
// TEST: Easy
/// The various status the connection can take
#[derive(Debug, Clone, Copy)]
pub(crate) enum ConnectionStatus {
@ -156,15 +160,17 @@ pub(crate) enum ConnectionStatus {
Initialised,
/// The connection was established
Connected,
/// The scan was started
ScanStarted,
/// The connection coudn't be done, with a string explaining the error
ConnectionFailure,
/// The connection is closed and the connection was a succes
ClosedWithSuccess,
ScanStarted,
}
type ScanData = Vec<(f64, f64)>;
// TEST: Easy
#[derive(Debug, Clone)]
/// All data needed to handle a connection to the TinySA
pub(crate) struct ConnectionInfo {
@ -196,6 +202,7 @@ pub(crate) struct ConnectionInfo {
pub(crate) error_raised: Arc<Mutex<Option<TinyUIError>>>,
}
// TEST: Easy
impl ConnectionInfo {
/// Create the initial connection containing only the initial mutex for error collection
pub(crate) fn new() -> Self {
@ -303,6 +310,7 @@ impl ConnectionInfo {
// }
}
// TEST: Easy
impl Default for ConnectionInfo {
fn default() -> Self {
Self {

View File

@ -23,6 +23,9 @@ use crate::{
},
};
// TEST: Hard
// TODO: Split flow/tui logic from coding logic
// Isolate impossible to test to one impl
impl<'a> AppInfo<'a> {
/// Create a new app with the port info available
pub(crate) fn new(info: Vec<SerialPortInfo>) -> Self {

View File

@ -18,6 +18,7 @@ pub(crate) const SELECTED_COLOR: Color = Color::White;
/// Color of an unselected item
pub(crate) const UNSELECTED_COLOR: Color = Color::DarkGray;
// TEST: Easy
/// This struct holds the current state of the app.
#[derive(Debug)]
pub(crate) struct AppInfo<'a> {
@ -49,6 +50,7 @@ pub(crate) struct AppInfo<'a> {
pub(crate) logged_message: bool,
}
// TEST: Easy
#[derive(Debug)]
pub(crate) struct UIUsize {
/// Current value held by the usize
@ -61,6 +63,7 @@ pub(crate) struct UIUsize {
max_clamp: usize,
}
// TEST: Easy
impl UIUsize {
/// Change the value depending on selection
pub(crate) fn change_value(&mut self, direction: InputDirection) {
@ -107,6 +110,7 @@ impl UIUsize {
}
}
// TEST: Easy
#[derive(Debug)]
/// The inner state of the app, useful for display and keystrokes
pub(crate) enum AppState {
@ -122,12 +126,14 @@ pub(crate) enum AppState {
Error,
}
// TEST: Easy
impl Default for AppState {
fn default() -> Self {
Self::InitialConfig(SelectedConfig::default())
}
}
// TEST ?
#[macro_export]
/// Shortcut for move right
macro_rules! mright {
@ -139,6 +145,7 @@ macro_rules! mright {
};
}
// TEST ?
#[macro_export]
/// Shortcut to move left
macro_rules! mleft {
@ -152,6 +159,8 @@ macro_rules! mleft {
}
};
}
// TEST ? Do more
/// The selected item in the config
#[derive(Debug, EnumCount, FromRepr, Clone, Copy)]
pub(crate) enum SelectedConfig {
@ -169,6 +178,7 @@ impl Default for SelectedConfig {
}
}
// TEST ?
/// The selected item in the final menu
#[derive(Debug, EnumCount, FromRepr, Copy, Clone)]
pub(crate) enum SelectedFinalConfig {
@ -190,6 +200,7 @@ impl Default for SelectedFinalConfig {
}
}
// TEST: ?
/// The list of all baudrates selectables
#[derive(Debug)]
pub(crate) struct BaudRateList {
@ -200,6 +211,7 @@ pub(crate) struct BaudRateList {
/// The inner state of the widget
pub(crate) state: ListState,
}
// TEST: Easy
impl BaudRateList {
/// Convert into a vec of strings, this is extremely expensive and shouldn't be done that way
pub(crate) fn to_vecstr(&self) -> Vec<String> {
@ -249,6 +261,7 @@ impl BaudRateList {
}
}
// TEST: Easy
impl Default for BaudRateList {
fn default() -> Self {
Self {
@ -258,6 +271,8 @@ impl Default for BaudRateList {
}
}
// TEST: Easy
// TODO: Consider changing to vec
/// A list of possible baudrates
#[derive(Debug, EnumIter, Copy, Clone)]
pub(crate) enum BaudRate {
@ -273,6 +288,7 @@ pub(crate) enum BaudRate {
BAUD3000000 = 3_000_000,
}
// TEST: Easy
/// A wrapper over the list of ports
#[derive(Debug)]
pub(crate) struct PortList {
@ -284,6 +300,7 @@ pub(crate) struct PortList {
pub(crate) state: ListState,
}
// TEST: Easy
impl PortList {
/// Make a port list from a serial reading
pub(crate) fn from_serial(
@ -319,6 +336,7 @@ impl PortList {
}
}
// TEST: Easy
#[derive(Debug, Default)]
/// The static list of all menu items
pub(crate) struct MenuItems<'a> {
@ -336,6 +354,7 @@ pub(crate) struct MenuItems<'a> {
// pub(crate) graph: Option<Chart<'a>>,
}
// TEST: Easy
/// Try to guess the best port to preselect
fn find_best_port(ports: &[String], saved_config: &Result<SavedConfig, TinyError>) -> usize {
let mut best_ports = vec!["/dev/ttyACM0", "COM3", "COM5", "/dev/ttyS16"];
@ -354,6 +373,7 @@ fn find_best_port(ports: &[String], saved_config: &Result<SavedConfig, TinyError
0
}
// TEST: Doable
impl<'a> AppInfo<'a> {
/// Put an item a specified color
pub(crate) fn change_color(
@ -374,6 +394,7 @@ impl<'a> AppInfo<'a> {
}
}
// TEST: Hard
impl<'a> AppInfo<'a> {
/// Update all the colors
pub(crate) fn update_color(&mut self) -> Result<(), TinyUIError> {