Skip to content

Commit

Permalink
guess
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Jul 15, 2024
1 parent cdbed82 commit 486d68e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
24 changes: 12 additions & 12 deletions src/cli/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,32 +84,32 @@ macro_rules! run_x {
for f in &mut files {
match self.input {
InputFormat::Single => {
let mut f = crate::$m::Limited::new(f, crate::$m::Limits::none());
let t = crate::$m::Type::read_xdr_to_end(r#type, &mut f)?;
let mut l = crate::$m::Limited::new(f, crate::$m::Limits::none());
let t = crate::$m::Type::read_xdr_to_end(r#type, &mut l)?;
self.out(&t)?;
}
InputFormat::SingleBase64 => {
let f = SkipWhitespace::new(f);
let mut f = crate::$m::Limited::new(f, crate::$m::Limits::none());
let t = crate::$m::Type::read_xdr_base64_to_end(r#type, &mut f)?;
let sw = SkipWhitespace::new(f);
let mut l = crate::$m::Limited::new(sw, crate::$m::Limits::none());
let t = crate::$m::Type::read_xdr_base64_to_end(r#type, &mut l)?;
self.out(&t)?;
}
InputFormat::Stream => {
let mut f = crate::$m::Limited::new(f, crate::$m::Limits::none());
for t in crate::$m::Type::read_xdr_iter(r#type, &mut f) {
let mut l = crate::$m::Limited::new(f, crate::$m::Limits::none());
for t in crate::$m::Type::read_xdr_iter(r#type, &mut l) {
self.out(&t?)?;
}
}
InputFormat::StreamBase64 => {
let f = SkipWhitespace::new(f);
let mut f = crate::$m::Limited::new(f, crate::$m::Limits::none());
for t in crate::$m::Type::read_xdr_base64_iter(r#type, &mut f) {
let sw = SkipWhitespace::new(f);
let mut l = crate::$m::Limited::new(sw, crate::$m::Limits::none());
for t in crate::$m::Type::read_xdr_base64_iter(r#type, &mut l) {
self.out(&t?)?;
}
}
InputFormat::StreamFramed => {
let mut f = crate::$m::Limited::new(f, crate::$m::Limits::none());
for t in crate::$m::Type::read_xdr_framed_iter(r#type, &mut f) {
let mut l = crate::$m::Limited::new(f, crate::$m::Limits::none());
for t in crate::$m::Type::read_xdr_framed_iter(r#type, &mut l) {
self.out(&t?)?;
}
}
Expand Down
45 changes: 28 additions & 17 deletions src/cli/guess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{

use clap::{Args, ValueEnum};

use crate::cli::Channel;
use crate::cli::{skip_whitespace::SkipWhitespace, Channel};

#[derive(thiserror::Error, Debug)]
#[allow(clippy::enum_variant_names)]
Expand Down Expand Up @@ -69,21 +69,29 @@ impl Default for OutputFormat {
macro_rules! run_x {
($f:ident, $m:ident) => {
fn $f(&self) -> Result<(), Error> {
let mut f =
crate::$m::Limited::new(ResetRead::new(self.file()?), crate::$m::Limits::none());
let mut rr = ResetRead::new(self.file()?);
'variants: for v in crate::$m::TypeVariant::VARIANTS {
f.inner.reset();
rr.reset();
let count: usize = match self.input {
InputFormat::Single => crate::$m::Type::read_xdr_to_end(v, &mut f)
.ok()
.map(|_| 1)
.unwrap_or_default(),
InputFormat::SingleBase64 => crate::$m::Type::read_xdr_base64_to_end(v, &mut f)
.ok()
.map(|_| 1)
.unwrap_or_default(),
InputFormat::Single => {
let mut l = crate::$m::Limited::new(&mut rr, crate::$m::Limits::none());
crate::$m::Type::read_xdr_to_end(v, &mut l)
.ok()
.map(|_| 1)
.unwrap_or_default()
}
InputFormat::SingleBase64 => {
let sw = SkipWhitespace::new(&mut rr);
let mut l = crate::$m::Limited::new(sw, crate::$m::Limits::none());
crate::$m::Type::read_xdr_base64_to_end(v, &mut l)
.ok()
.map(|_| 1)
.unwrap_or_default()
}
InputFormat::Stream => {
let iter = crate::$m::Type::read_xdr_iter(v, &mut f).take(self.certainty);
let mut l = crate::$m::Limited::new(&mut rr, crate::$m::Limits::none());
let iter = crate::$m::Type::read_xdr_iter(v, &mut l);
let iter = iter.take(self.certainty);
let mut count = 0;
for v in iter {
match v {
Expand All @@ -94,8 +102,10 @@ macro_rules! run_x {
count
}
InputFormat::StreamBase64 => {
let iter =
crate::$m::Type::read_xdr_base64_iter(v, &mut f).take(self.certainty);
let sw = SkipWhitespace::new(&mut rr);
let mut l = crate::$m::Limited::new(sw, crate::$m::Limits::none());
let iter = crate::$m::Type::read_xdr_base64_iter(v, &mut l);
let iter = iter.take(self.certainty);
let mut count = 0;
for v in iter {
match v {
Expand All @@ -106,8 +116,9 @@ macro_rules! run_x {
count
}
InputFormat::StreamFramed => {
let iter =
crate::$m::Type::read_xdr_framed_iter(v, &mut f).take(self.certainty);
let mut l = crate::$m::Limited::new(&mut rr, crate::$m::Limits::none());
let iter = crate::$m::Type::read_xdr_framed_iter(v, &mut l);
let iter = iter.take(self.certainty);
let mut count = 0;
for v in iter {
match v {
Expand Down
6 changes: 3 additions & 3 deletions src/cli/skip_whitespace.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::io::Read;

/// Forwards read operations to the wrapped object, skipping over any whitespace
/// in what is written to buf when the function returns.
/// Forwards read operations to the wrapped object, skipping over any
/// whitespace.
pub struct SkipWhitespace<R: Read> {
inner: R,
pub inner: R,
}

impl<R: Read> SkipWhitespace<R> {
Expand Down

0 comments on commit 486d68e

Please sign in to comment.