From 5758e77371d83b62ec9feeb46f3e1ec082bce2aa Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Tue, 26 Sep 2023 13:16:14 +0200 Subject: [PATCH] Run cargo-fmt --- examples/parse_vcd.rs | 9 ++++++--- examples/vcd.rs | 23 ++++++++++------------- src/lib.rs | 4 ++-- src/vcd.rs | 6 +++--- src/vcd/parse.rs | 7 +++---- src/vcd/parse/events.rs | 9 ++++----- src/vcd/parse/metadata.rs | 16 ++++++++++------ src/vcd/parse/scopes.rs | 35 +++++++++++++++++++++++------------ src/vcd/parse/types.rs | 26 ++++++++++++-------------- src/vcd/types.rs | 6 +++--- tests/files.rs | 8 ++++---- tests/integration_test.rs | 2 +- 12 files changed, 81 insertions(+), 70 deletions(-) diff --git a/examples/parse_vcd.rs b/examples/parse_vcd.rs index 4d7f5da..66b6146 100644 --- a/examples/parse_vcd.rs +++ b/examples/parse_vcd.rs @@ -18,14 +18,17 @@ fn main() -> std::io::Result<()> { let args = Cli::parse(); use std::time::Instant; - + let now = Instant::now(); let file = File::open(&args.path)?; parse_vcd(file).unwrap(); let elapsed = now.elapsed(); - println!("Parsed VCD file {} : {:.2?}", &args.path.as_os_str().to_str().unwrap(), elapsed); - + println!( + "Parsed VCD file {} : {:.2?}", + &args.path.as_os_str().to_str().unwrap(), + elapsed + ); Ok(()) } diff --git a/examples/vcd.rs b/examples/vcd.rs index d89d76a..968bf09 100644 --- a/examples/vcd.rs +++ b/examples/vcd.rs @@ -4,18 +4,20 @@ // the root of the folder containing the sources for this program. use std::fs::File; -use fastwave_backend::{ScopeIdx, VCD, parse_vcd, SignalIdx}; +use fastwave_backend::{parse_vcd, ScopeIdx, SignalIdx, VCD}; -fn indented_print(indent : u8, name : &String) { - for _ in 0..indent {print!(" |");} +fn indented_print(indent: u8, name: &String) { + for _ in 0..indent { + print!(" |"); + } print!("---"); println!("{name}"); } -// TODO: refactor into more general visitor pattern that takes a +// TODO: refactor into more general visitor pattern that takes a // function as an argument. fn visit_all_scopes(vcd: &VCD) { - fn visit_all_scope_children(root_idx: ScopeIdx, vcd: &VCD, indent : u8) { + fn visit_all_scope_children(root_idx: ScopeIdx, vcd: &VCD, indent: u8) { if vcd.child_scopes_by_idx(root_idx).is_empty() { } else { for child_scope_idx in vcd.child_scopes_by_idx(root_idx) { @@ -36,9 +38,8 @@ fn visit_all_scopes(vcd: &VCD) { } fn main() -> std::io::Result<()> { - use std::time::Instant; - + // we start by printing out the entire signal tree of // a parsed VCD let now = Instant::now(); @@ -53,8 +54,7 @@ fn main() -> std::io::Result<()> { println!("Done Printing Scopes"); println!(); - - // we then parse another VCD, print its signal tree and + // we then parse another VCD, print its signal tree and // query some values on its timeline let now = Instant::now(); let file_path = "tests/vcd-files/amaranth/up_counter.vcd"; @@ -73,11 +73,8 @@ fn main() -> std::io::Result<()> { let timestamps = vec![31499_000u32, 31500_000u32, 57760_000u32]; for timestamp in timestamps { let time = num::BigUint::from(timestamp); - let val = state_signal - .query_string_val_on_tmln(&time, &vcd) - .unwrap(); + let val = state_signal.query_string_val_on_tmln(&time, &vcd).unwrap(); println!("Signal `{name}` has value `{val}` at time `{time}`"); - } Ok(()) diff --git a/src/lib.rs b/src/lib.rs index 1ab2b83..fc306fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,8 +5,8 @@ mod vcd; pub use vcd::parse::parse_vcd; -pub use vcd::types::{ScopeIdx, SignalIdx, VCD}; -pub use vcd::types::{Metadata, Timescale, Version}; pub use vcd::signal::{Signal, SignalValue}; +pub use vcd::types::{Metadata, Timescale, Version}; +pub use vcd::types::{ScopeIdx, SignalIdx, VCD}; pub use num::BigUint; diff --git a/src/vcd.rs b/src/vcd.rs index fc58b13..53641e1 100644 --- a/src/vcd.rs +++ b/src/vcd.rs @@ -3,8 +3,8 @@ // and the YEHOWSHUA license, both of which can be found at // the root of the folder containing the sources for this program. -mod reader; -pub(crate) mod types; pub(crate) mod parse; +mod reader; pub(crate) mod signal; -mod utilities; \ No newline at end of file +pub(crate) mod types; +mod utilities; diff --git a/src/vcd/parse.rs b/src/vcd/parse.rs index 7229045..7ff2f89 100644 --- a/src/vcd/parse.rs +++ b/src/vcd/parse.rs @@ -4,11 +4,10 @@ // the root of the folder containing the sources for this program. mod combinator_atoms; -mod types; +mod events; mod metadata; mod scopes; -mod events; - +mod types; pub fn parse_vcd(file: impl std::io::Read) -> Result { let mut word_gen = super::reader::WordReader::new(file); @@ -26,7 +25,7 @@ pub fn parse_vcd(file: impl std::io::Read) -> Result all_signals: vec![], all_scopes: vec![], root_scopes: vec![], - largest_timestamp: None + largest_timestamp: None, }; scopes::parse_scopes(&mut word_gen, &mut vcd, &mut signal_map)?; diff --git a/src/vcd/parse/events.rs b/src/vcd/parse/events.rs index 1a75e73..315f7a1 100644 --- a/src/vcd/parse/events.rs +++ b/src/vcd/parse/events.rs @@ -3,14 +3,13 @@ // and the YEHOWSHUA license, both of which can be found at // the root of the folder containing the sources for this program. -use std::collections::HashMap; use num::BigUint; +use std::collections::HashMap; -use super::super::utilities::{BinaryParserErrTypes, binary_str_to_vec_u8}; -use super::super::signal::{SignalEnum, LsbIdxOfTmstmpValOnTmln}; -use super::super::reader::{WordReader, Cursor, Line, Word, next_word}; +use super::super::reader::{next_word, Cursor, Line, Word, WordReader}; +use super::super::signal::{LsbIdxOfTmstmpValOnTmln, SignalEnum}; use super::super::types::{SignalIdx, VCD}; - +use super::super::utilities::{binary_str_to_vec_u8, BinaryParserErrTypes}; pub(super) fn parse_events<'a, R: std::io::Read>( word_reader: &mut WordReader, diff --git a/src/vcd/parse/metadata.rs b/src/vcd/parse/metadata.rs index 126a334..7ee64ad 100644 --- a/src/vcd/parse/metadata.rs +++ b/src/vcd/parse/metadata.rs @@ -5,11 +5,11 @@ use chrono::prelude::{DateTime, Utc}; use itertools::Itertools; -use super::super::reader::{Cursor, WordReader, next_word}; -use super::super::types::{Timescale, Version, Metadata}; +use super::super::reader::{next_word, Cursor, WordReader}; +use super::super::types::{Metadata, Timescale, Version}; -use super::combinator_atoms::{take_until, take_while, digit, tag}; -use super::types::{ParseResult}; +use super::combinator_atoms::{digit, tag, take_until, take_while}; +use super::types::ParseResult; pub(super) fn parse_date( word_and_ctx1: (&str, &Cursor), @@ -145,7 +145,9 @@ pub(super) fn parse_date( )) } -pub(super) fn parse_version(word_reader: &mut WordReader) -> Result { +pub(super) fn parse_version( + word_reader: &mut WordReader, +) -> Result { let mut version = String::new(); loop { @@ -220,7 +222,9 @@ pub(super) fn parse_timescale( return Ok(timescale); } -pub(super) fn parse_metadata(word_reader: &mut WordReader) -> Result { +pub(super) fn parse_metadata( + word_reader: &mut WordReader, +) -> Result { let mut metadata = Metadata { date: None, version: None, diff --git a/src/vcd/parse/scopes.rs b/src/vcd/parse/scopes.rs index 2edf54b..9931fa5 100644 --- a/src/vcd/parse/scopes.rs +++ b/src/vcd/parse/scopes.rs @@ -5,15 +5,14 @@ /// part of the vcd parser that handles parsing the signal tree and /// building the resulting signal tree - use std::collections::HashMap; -use super::super::reader::{WordReader, next_word, curr_word}; -use super::super::types::{VCD, Scope, ScopeIdx, SignalIdx}; +use super::super::reader::{curr_word, next_word, WordReader}; use super::super::signal::{SigType, SignalEnum}; +use super::super::types::{Scope, ScopeIdx, SignalIdx, VCD}; -use super::combinator_atoms::{tag, ident}; -use super::types::{ParseResult}; +use super::combinator_atoms::{ident, tag}; +use super::types::ParseResult; pub(super) fn parse_var<'a, R: std::io::Read>( word_reader: &mut WordReader, @@ -98,7 +97,7 @@ pub(super) fn parse_var<'a, R: std::io::Read>( let (word, _) = next_word!(word_reader)?; match word { "$end" => break, - _ => full_signal_name.push(word.to_string()) + _ => full_signal_name.push(word.to_string()), } } let full_signal_name = full_signal_name.join(" "); @@ -118,7 +117,11 @@ pub(super) fn parse_var<'a, R: std::io::Read>( let signal_idx = SignalIdx(vcd.all_signals.len()); let signal = SignalEnum::Alias { name: full_signal_name.clone(), - path: path.iter().cloned().chain([full_signal_name]).collect::>(), + path: path + .iter() + .cloned() + .chain([full_signal_name]) + .collect::>(), signal_alias: *ref_signal_idx, }; (signal, signal_idx) @@ -128,7 +131,11 @@ pub(super) fn parse_var<'a, R: std::io::Read>( signal_map.insert(signal_alias.to_string(), signal_idx); let signal = SignalEnum::Data { name: full_signal_name.clone(), - path: path.iter().cloned().chain([full_signal_name]).collect::>(), + path: path + .iter() + .cloned() + .chain([full_signal_name]) + .collect::>(), sig_type: var_type, signal_error: None, num_bits, @@ -223,7 +230,7 @@ fn parse_scopes_inner<'a, R: std::io::Read>( parent_scope_idx: Option, vcd: &'a mut VCD, signal_map: &mut HashMap, - path: &Vec + path: &Vec, ) -> Result<(), String> { // $scope module reg_mag_i $end // ^^^^^^ - module keyword @@ -275,8 +282,6 @@ fn parse_scopes_inner<'a, R: std::io::Read>( // ^^^^ - end keyword ident(word_reader, "$end")?; - - loop { let (word, cursor) = next_word!(word_reader)?; let ParseResult { matched, residual } = tag(word, "$"); @@ -286,7 +291,13 @@ fn parse_scopes_inner<'a, R: std::io::Read>( match residual { "scope" => { // recursive - parse inside of current scope tree - parse_scopes_inner(word_reader, Some(curr_scope_idx), vcd, signal_map, &path)?; + parse_scopes_inner( + word_reader, + Some(curr_scope_idx), + vcd, + signal_map, + &path, + )?; } "var" => { parse_var(word_reader, curr_scope_idx, vcd, signal_map, &path)?; diff --git a/src/vcd/parse/types.rs b/src/vcd/parse/types.rs index c54eb3b..3bb272b 100644 --- a/src/vcd/parse/types.rs +++ b/src/vcd/parse/types.rs @@ -4,26 +4,24 @@ // the root of the folder containing the sources for this program. #[derive(Debug)] pub(super) struct ParseResult<'a> { - pub(super) matched : &'a str, - pub(super) residual : &'a str} + pub(super) matched: &'a str, + pub(super) residual: &'a str, +} impl<'a> ParseResult<'a> { - - pub(super) fn assert_match(& self) -> Result<&str, String> { + pub(super) fn assert_match(&self) -> Result<&str, String> { if self.matched == "" { - return Err("no match".to_string()) - } - else { - return Ok(self.matched) + return Err("no match".to_string()); + } else { + return Ok(self.matched); } } - pub(super) fn assert_residual(& self) -> Result<&str, String> { + pub(super) fn assert_residual(&self) -> Result<&str, String> { if self.residual == "" { - return Err("no residual".to_string()) - } - else { - return Ok(self.residual) + return Err("no residual".to_string()); + } else { + return Ok(self.residual); } } -} \ No newline at end of file +} diff --git a/src/vcd/types.rs b/src/vcd/types.rs index a53fb6c..b184f1d 100644 --- a/src/vcd/types.rs +++ b/src/vcd/types.rs @@ -4,9 +4,9 @@ // This program is distributed under both the GPLV3 license // and the YEHOWSHUA license, both of which can be found at // the root of the folder containing the sources for this program. +use super::signal::{Signal, SignalEnum}; use chrono::prelude::{DateTime, Utc}; use num::BigUint; -use super::signal::{Signal, SignalEnum}; #[derive(Debug)] pub struct Version(pub String); @@ -63,7 +63,7 @@ pub struct VCD { pub(super) all_signals: Vec, pub(super) all_scopes: Vec, pub(super) root_scopes: Vec, - pub(super) largest_timestamp: Option + pub(super) largest_timestamp: Option, } impl VCD { @@ -106,7 +106,7 @@ impl VCD { // dereference signal if Signal::Alias, or keep idx if Signal::Data let signal_idx = match signal { SignalEnum::Data { self_idx, .. } => *self_idx, - SignalEnum::Alias {signal_alias, .. } => *signal_alias, + SignalEnum::Alias { signal_alias, .. } => *signal_alias, }; // Should now point to Signal::Data variant, or else there's an error diff --git a/tests/files.rs b/tests/files.rs index 7832a85..38c08b2 100644 --- a/tests/files.rs +++ b/tests/files.rs @@ -5,7 +5,7 @@ // TODO: we should eventually be able to only test on just // the files const -pub const FILES : [&str; 31] = [ +pub const FILES: [&str; 31] = [ "./tests/vcd-files/aldec/SPI_Write.vcd", "./tests/vcd-files/ghdl/alu.vcd", "./tests/vcd-files/ghdl/idea.vcd", @@ -40,7 +40,7 @@ pub const FILES : [&str; 31] = [ "./tests/vcd-files/scope_with_comment.vcd", ]; -pub const GOOD_DATE_FILES : [&str; 24] = [ +pub const GOOD_DATE_FILES: [&str; 24] = [ "./test-vcd-files/aldec/SPI_Write.vcd", "./test-vcd-files/ghdl/alu.vcd", "./test-vcd-files/ghdl/idea.vcd", @@ -64,10 +64,10 @@ pub const GOOD_DATE_FILES : [&str; 24] = [ "./test-vcd-files/verilator/vlt_dump.vcd", "./test-vcd-files/xilinx_isim/test.vcd", "./test-vcd-files/xilinx_isim/test1.vcd", - "./test-vcd-files/xilinx_isim/test2x2_regex22_string1.vcd" + "./test-vcd-files/xilinx_isim/test2x2_regex22_string1.vcd", ]; -pub const BAD_DATE_FILES : [&str; 6] = [ +pub const BAD_DATE_FILES: [&str; 6] = [ "./test-vcd-files/ncsim/ffdiv_32bit_tb.vcd", "./test-vcd-files/quartus/mipsHardware.vcd", "./test-vcd-files/quartus/wave_registradores.vcd", diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 78f4822..48e13d9 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -19,4 +19,4 @@ fn parse_all_VCDs() { vcd.unwrap(); } } -} \ No newline at end of file +}