Cleanup #18

Merged
oscargus merged 2 commits from cleanup into main 2023-09-27 07:40:33 +00:00
8 changed files with 41 additions and 41 deletions
Showing only changes of commit 0795465c36 - Show all commits

View file

@ -6,15 +6,13 @@ use super::super::reader::{next_word, WordReader};
use super::types::ParseResult; use super::types::ParseResult;
pub(super) fn digit(chr: u8) -> bool { pub(super) fn digit(chr: u8) -> bool {
let zero = b'0' as u8; let zero = b'0';
let nine = b'9' as u8; let nine = b'9';
let between_zero_and_nine = (chr >= zero) && (nine >= chr); (chr >= zero) && (nine >= chr)
return between_zero_and_nine;
} }
pub(super) fn take_until<'a>(word: &'a str, pattern: u8) -> ParseResult<'a> { pub(super) fn take_until(word: &str, pattern: u8) -> ParseResult<'_> {
let mut new_start = 0; let mut new_start = 0;
for chr in word.as_bytes() { for chr in word.as_bytes() {
@ -25,13 +23,13 @@ pub(super) fn take_until<'a>(word: &'a str, pattern: u8) -> ParseResult<'a> {
} }
} }
return ParseResult { ParseResult {
matched: &word[0..new_start], matched: &word[0..new_start],
residual: &word[new_start..], residual: &word[new_start..],
}; }
} }
pub(super) fn take_while<'a>(word: &'a str, cond: fn(u8) -> bool) -> ParseResult<'a> { pub(super) fn take_while(word: &str, cond: fn(u8) -> bool) -> ParseResult<'_> {
let mut new_start = 0; let mut new_start = 0;
for chr in word.as_bytes() { for chr in word.as_bytes() {
@ -42,10 +40,10 @@ pub(super) fn take_while<'a>(word: &'a str, cond: fn(u8) -> bool) -> ParseResult
} }
} }
return ParseResult { ParseResult {
matched: &word[0..new_start], matched: &word[0..new_start],
residual: &word[new_start..], residual: &word[new_start..],
}; }
} }
pub(super) fn tag<'a>(word: &'a str, pattern: &'a str) -> ParseResult<'a> { pub(super) fn tag<'a>(word: &'a str, pattern: &'a str) -> ParseResult<'a> {
@ -63,10 +61,10 @@ pub(super) fn tag<'a>(word: &'a str, pattern: &'a str) -> ParseResult<'a> {
new_start += 1; new_start += 1;
} }
return ParseResult { ParseResult {
matched: &word[0..new_start], matched: &word[0..new_start],
residual: &word[new_start..], residual: &word[new_start..],
}; }
} }
pub(super) fn ident<R: std::io::Read>( pub(super) fn ident<R: std::io::Read>(
@ -77,9 +75,9 @@ pub(super) fn ident<R: std::io::Read>(
let (word, cursor) = next_word!(word_reader)?; let (word, cursor) = next_word!(word_reader)?;
if word == keyword { if word == keyword {
return Ok(()); Ok(())
} else { } else {
let err = format!("found keyword `{word}` but expected `{keyword}` on {cursor:?}"); let err = format!("found keyword `{word}` but expected `{keyword}` on {cursor:?}");
return Err(err); Err(err)
} }
} }

View file

@ -11,9 +11,9 @@ use super::super::signal::{LsbIdxOfTmstmpValOnTmln, SignalEnum};
use super::super::types::{SignalIdx, VCD}; use super::super::types::{SignalIdx, VCD};
use super::super::utilities::{binary_str_to_vec_u8, BinaryParserErrTypes}; use super::super::utilities::{binary_str_to_vec_u8, BinaryParserErrTypes};
pub(super) fn parse_events<'a, R: std::io::Read>( pub(super) fn parse_events<R: std::io::Read>(
word_reader: &mut WordReader<R>, word_reader: &mut WordReader<R>,
vcd: &'a mut VCD, vcd: &mut VCD,
signal_map: &mut HashMap<String, SignalIdx>, signal_map: &mut HashMap<String, SignalIdx>,
) -> Result<(), String> { ) -> Result<(), String> {
let mut curr_tmstmp_lsb_idx = 0u32; let mut curr_tmstmp_lsb_idx = 0u32;

View file

@ -159,7 +159,7 @@ pub(super) fn parse_version<R: std::io::Read>(
return Ok(Version(version)); return Ok(Version(version));
} else { } else {
version.push_str(word); version.push_str(word);
version.push_str(" "); version.push(' ');
} }
} }
} }
@ -179,7 +179,7 @@ pub(super) fn parse_timescale<R: std::io::Read>(
.map_err(|e| format!("Error near {}:{}. {e}", file!(), line!()))?; .map_err(|e| format!("Error near {}:{}. {e}", file!(), line!()))?;
let timescale = { let timescale = {
if residual == "" { if residual.is_empty() {
let (word, _) = next_word!(word_reader)?; let (word, _) = next_word!(word_reader)?;
let unit = match word { let unit = match word {
"fs" => Ok(Timescale::Fs), "fs" => Ok(Timescale::Fs),
@ -219,7 +219,7 @@ pub(super) fn parse_timescale<R: std::io::Read>(
let (word, _) = next_word!(word_reader)?; let (word, _) = next_word!(word_reader)?;
tag(word, "$end").assert_match()?; tag(word, "$end").assert_match()?;
return Ok(timescale); Ok(timescale)
} }
pub(super) fn parse_metadata<R: std::io::Read>( pub(super) fn parse_metadata<R: std::io::Read>(
@ -331,5 +331,5 @@ pub(super) fn parse_metadata<R: std::io::Read>(
_ => {} _ => {}
} }
} }
return Ok(metadata); Ok(metadata)
} }

View file

@ -14,10 +14,10 @@ use super::super::types::{Scope, ScopeIdx, SignalIdx, VCD};
use super::combinator_atoms::{ident, tag}; use super::combinator_atoms::{ident, tag};
use super::types::ParseResult; use super::types::ParseResult;
pub(super) fn parse_var<'a, R: std::io::Read>( pub(super) fn parse_var<R: std::io::Read>(
word_reader: &mut WordReader<R>, word_reader: &mut WordReader<R>,
parent_scope_idx: ScopeIdx, parent_scope_idx: ScopeIdx,
vcd: &'a mut VCD, vcd: &mut VCD,
signal_map: &mut HashMap<String, SignalIdx>, signal_map: &mut HashMap<String, SignalIdx>,
path: &Vec<String>, path: &Vec<String>,
) -> Result<(), String> { ) -> Result<(), String> {
@ -70,7 +70,9 @@ pub(super) fn parse_var<'a, R: std::io::Read>(
| SigType::Wire | SigType::Wire
| SigType::Tri1 | SigType::Tri1
| SigType::Time => { | SigType::Time => {
let num_bits = word.parse::<usize>().expect(parse_err.as_str()); let num_bits = word
.parse::<usize>()
.unwrap_or_else(|_| panic!("{}", parse_err));
let num_bits = u16::try_from(num_bits).map_err(|_| { let num_bits = u16::try_from(num_bits).map_err(|_| {
format!( format!(
"Error near {}:{} while parsing vcd file at {cursor:?}. \ "Error near {}:{} while parsing vcd file at {cursor:?}. \
@ -162,9 +164,9 @@ pub(super) fn parse_var<'a, R: std::io::Read>(
/// Sometimes, variables can be listed outside of scopes. /// Sometimes, variables can be listed outside of scopes.
/// We call these orphaned vars. /// We call these orphaned vars.
fn parse_orphaned_vars<'a, R: std::io::Read>( fn parse_orphaned_vars<R: std::io::Read>(
word_reader: &mut WordReader<R>, word_reader: &mut WordReader<R>,
vcd: &'a mut VCD, vcd: &mut VCD,
signal_map: &mut HashMap<String, SignalIdx>, signal_map: &mut HashMap<String, SignalIdx>,
) -> Result<(), String> { ) -> Result<(), String> {
// create scope for unscoped signals if such a scope does not // create scope for unscoped signals if such a scope does not
@ -225,10 +227,10 @@ fn parse_orphaned_vars<'a, R: std::io::Read>(
Ok(()) Ok(())
} }
fn parse_scopes_inner<'a, R: std::io::Read>( fn parse_scopes_inner<R: std::io::Read>(
word_reader: &mut WordReader<R>, word_reader: &mut WordReader<R>,
parent_scope_idx: Option<ScopeIdx>, parent_scope_idx: Option<ScopeIdx>,
vcd: &'a mut VCD, vcd: &mut VCD,
signal_map: &mut HashMap<String, SignalIdx>, signal_map: &mut HashMap<String, SignalIdx>,
path: &Vec<String>, path: &Vec<String>,
) -> Result<(), String> { ) -> Result<(), String> {
@ -341,9 +343,9 @@ fn parse_scopes_inner<'a, R: std::io::Read>(
Ok(()) Ok(())
} }
pub(super) fn parse_scopes<'a, R: std::io::Read>( pub(super) fn parse_scopes<R: std::io::Read>(
word_reader: &mut WordReader<R>, word_reader: &mut WordReader<R>,
vcd: &'a mut VCD, vcd: &mut VCD,
signal_map: &mut HashMap<String, SignalIdx>, signal_map: &mut HashMap<String, SignalIdx>,
) -> Result<(), String> { ) -> Result<(), String> {
// get the current word // get the current word

View file

@ -10,18 +10,18 @@ pub(super) struct ParseResult<'a> {
impl<'a> ParseResult<'a> { 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 == "" { if self.matched.is_empty() {
return Err("no match".to_string()); Err("no match".to_string())
} else { } else {
return Ok(self.matched); Ok(self.matched)
} }
} }
pub(super) fn assert_residual(&self) -> Result<&str, String> { pub(super) fn assert_residual(&self) -> Result<&str, String> {
if self.residual == "" { if self.residual.is_empty() {
return Err("no residual".to_string()); Err("no residual".to_string())
} else { } else {
return Ok(self.residual); Ok(self.residual)
} }
} }
} }

View file

@ -319,7 +319,7 @@ impl SignalEnum {
fn bits_required(&self) -> Option<u16> { fn bits_required(&self) -> Option<u16> {
match self { match self {
SignalEnum::Data { num_bits, .. } => num_bits.clone(), SignalEnum::Data { num_bits, .. } => *num_bits,
// TODO: Follow aliases? // TODO: Follow aliases?
SignalEnum::Alias { .. } => None, SignalEnum::Alias { .. } => None,
} }

View file

@ -85,7 +85,7 @@ impl VCD {
let scope = &self.all_scopes[idx]; let scope = &self.all_scopes[idx];
&scope.name &scope.name
} }
pub fn signal_from_signal_idx<'a>(&'a self, idx: SignalIdx) -> Signal<'a> { pub fn signal_from_signal_idx(&self, idx: SignalIdx) -> Signal<'_> {
let SignalIdx(idx) = idx; let SignalIdx(idx) = idx;
let signal_enum = &self.all_signals[idx]; let signal_enum = &self.all_signals[idx];
return Signal(signal_enum); return Signal(signal_enum);

View file

@ -38,7 +38,7 @@ fn base2_str_to_byte(word: &[u8]) -> Result<u8, BinaryParserErrTypes> {
for (idx, chr) in word.iter().rev().enumerate() { for (idx, chr) in word.iter().rev().enumerate() {
match chr { match chr {
b'1' => val = bit_lut[idx] | val, b'1' => val |= bit_lut[idx],
b'0' => {} b'0' => {}
b'x' | b'X' => return Err(BinaryParserErrTypes::XValue), b'x' | b'X' => return Err(BinaryParserErrTypes::XValue),
b'z' | b'Z' => return Err(BinaryParserErrTypes::ZValue), b'z' | b'Z' => return Err(BinaryParserErrTypes::ZValue),
@ -73,13 +73,13 @@ pub(super) fn binary_str_to_vec_u8(binary_str: &str) -> Result<Vec<u8>, BinaryPa
if head_idx < 8 { if head_idx < 8 {
head_idx = 0 head_idx = 0
} else { } else {
head_idx = head_idx - 8; head_idx -= 8;
} }
if tail_idx < 8 { if tail_idx < 8 {
tail_idx = 0 tail_idx = 0
} else { } else {
tail_idx = tail_idx - 8; tail_idx -= 8;
} }
} }
Ok(vec_u8) Ok(vec_u8)