Skip to content

Commit 44af7ee

Browse files
authored
refactor(ColumnData): Add implementation for f64() casting and string Display format
2 parents c8f9d02 + efd2973 commit 44af7ee

File tree

3 files changed

+52
-77
lines changed

3 files changed

+52
-77
lines changed

twinleaf-tools/src/bin/tio-monitor.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,19 @@ impl ColumnFormatter {
123123
desc_width: usize,
124124
) -> std::io::Result<()> {
125125
use data::ColumnData;
126-
let (fval, fmtval) = match col.value {
127-
ColumnData::Int(x) => (x as f64, format!("{:10} ", x)),
128-
ColumnData::UInt(x) => (x as f64, format!("{:10} ", x)),
129-
ColumnData::Float(x) => (
130-
x,
126+
127+
let fval = col.value.try_as_f64().unwrap_or(f64::NAN);
128+
let fmtval = match col.value {
129+
ColumnData::Int(x) => format!("{:10} ", x),
130+
ColumnData::UInt(x) => format!("{:10} ", x),
131+
ColumnData::Float(x) => {
131132
if x.is_nan() {
132133
format!("{:10} ", x)
133134
} else {
134135
format!("{:15.4} ", x)
135-
},
136-
),
137-
ColumnData::Unknown => (f64::NAN, format!("{:>15} ", "unsupported")),
136+
}
137+
}
138+
ColumnData::Unknown => format!("{:>15} ", "unsupported"),
138139
};
139140

140141
stdout.queue(cursor::MoveToNextLine(1))?;

twinleaf-tools/src/bin/tio-tool.rs

Lines changed: 20 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use tio::proto::DeviceRoute;
22
use tio::proxy;
33
use tio::util;
4-
use twinleaf::data::{ColumnData, DeviceDataParser};
4+
use twinleaf::data::{DeviceDataParser};
55
use twinleaf::tio;
66
use twinleaf_tools::{tio_opts, tio_parseopts};
77

@@ -515,16 +515,6 @@ fn log_data_dump(args: &[String]) -> Result<(), ()> {
515515
Ok(())
516516
}
517517

518-
fn match_value(data: ColumnData) -> String {
519-
let data_type = match data {
520-
ColumnData::Int(x) => format!("{}", x),
521-
ColumnData::UInt(x) => format!("{}", x),
522-
ColumnData::Float(x) => format!("{}", x),
523-
ColumnData::Unknown => "?".to_string(),
524-
};
525-
data_type
526-
}
527-
528518
fn log_csv(args: &[String]) -> Result<(), ()> {
529519
let mut parser = DeviceDataParser::new(args.len() > 1);
530520
let id: u8 = args[1].parse().unwrap();
@@ -538,61 +528,33 @@ fn log_csv(args: &[String]) -> Result<(), ()> {
538528
.create(true)
539529
.open(path)
540530
.or(Err(()))?;
541-
let mut streamhead: bool = false;
542-
let mut first: bool = true;
531+
532+
let mut header_written: bool = false;
543533

544534
for path in &args[2..] {
545535
let mut rest: &[u8] = &std::fs::read(path).unwrap();
546536
while rest.len() > 0 {
547537
let (pkt, len) = tio::Packet::deserialize(rest).unwrap();
548538
rest = &rest[len..];
549539
for sample in parser.process_packet(&pkt) {
550-
//match stream id
551-
if sample.stream.stream_id == id as u8 {
552-
//iterate through values
553-
for col in &sample.columns {
554-
let time = format!("{:.6}", sample.timestamp_end());
555-
let value = match_value(col.value.clone());
556-
557-
//write in column names
558-
if !streamhead {
559-
let timehead = format!("{},", "time");
560-
let _ = file.write_all(timehead.as_bytes());
561-
562-
for col in &sample.columns {
563-
let mut header = format!("{},", col.desc.name);
564-
565-
if col.desc.name
566-
== sample.columns[&sample.columns.len() - 1].desc.name.clone()
567-
{
568-
header = format!("{}", col.desc.name);
569-
}
570-
571-
file.write_all(header.as_bytes()).or(Err(()))?;
572-
}
573-
file.write_all(b"\n").or(Err(()))?;
574-
streamhead = true;
575-
}
576-
577-
//write in data
578-
let timefmt = format!("{},", time);
579-
let mut formatted_value = format!("{},", value);
580-
if first {
581-
let _ = file.write_all(timefmt.as_bytes());
582-
first = false;
583-
}
584-
585-
if value
586-
== match_value(sample.columns[&sample.columns.len() - 1].value.clone())
587-
{
588-
formatted_value = format!("{}", value);
589-
}
590-
591-
file.write_all(formatted_value.as_bytes()).or(Err(()))?;
592-
}
593-
file.write_all(b"\n").or(Err(()))?;
594-
first = true;
540+
if sample.stream.stream_id != id {
541+
continue;
595542
}
543+
544+
if !header_written {
545+
let mut headers: Vec<String> = vec!["time".to_string()];
546+
headers.extend(sample.columns.iter().map(|col| col.desc.name.clone()));
547+
548+
writeln!(file, "{}", headers.join(",")).or(Err(()))?;
549+
header_written = true;
550+
}
551+
552+
let mut values: Vec<String> = Vec::new();
553+
values.push(format!("{:.6}", sample.timestamp_end()));
554+
555+
values.extend(sample.columns.iter().map(|col| col.value.to_string()));
556+
557+
writeln!(file, "{}", values.join(",")).or(Err(()))?;
596558
}
597559
}
598560
}

twinleaf/src/data/sample.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ pub enum ColumnData {
1111
Unknown,
1212
}
1313

14+
impl ColumnData {
15+
pub fn try_as_f64(&self) -> Option<f64> {
16+
match *self {
17+
ColumnData::Int(i) => Some(i as f64),
18+
ColumnData::UInt(u) => Some(u as f64),
19+
ColumnData::Float(f) => Some(f),
20+
ColumnData::Unknown => None,
21+
}
22+
}
23+
}
24+
25+
impl std::fmt::Display for ColumnData {
26+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27+
match *self {
28+
ColumnData::Int(x) => write!(f, "{}", x),
29+
ColumnData::UInt(x) => write!(f, "{}", x),
30+
ColumnData::Float(x) => write!(f, "{}", x),
31+
ColumnData::Unknown => write!(f, "?"),
32+
}
33+
}
34+
}
35+
1436
#[derive(Debug, Clone)]
1537
pub struct Column {
1638
pub value: ColumnData,
@@ -100,17 +122,7 @@ impl std::fmt::Display for Sample {
100122
self.timestamp_end()
101123
)?;
102124
for col in &self.columns {
103-
write!(
104-
f,
105-
" {}: {}",
106-
col.desc.name,
107-
match col.value {
108-
ColumnData::Int(x) => format!("{}", x),
109-
ColumnData::UInt(x) => format!("{}", x),
110-
ColumnData::Float(x) => format!("{}", x),
111-
ColumnData::Unknown => "?".to_string(),
112-
}
113-
)?;
125+
write!(f, " {}: {}", col.desc.name, col.value)?;
114126
}
115127
write!(f, " [#{}]", self.n)
116128
}

0 commit comments

Comments
 (0)