Refactor. Returns code to buildable

- Pushes file/stdout specific logic to trait objects.
- Extracts read/write helper
- Extracts conv/block/unblock helper
- Impl block
- WIP: Many failing tests!
This commit is contained in:
Tyler
2021-05-27 15:55:29 -07:00
parent d97672dfd3
commit d0b4fe34c1
4 changed files with 424 additions and 154 deletions
+368 -117
View File
File diff suppressed because it is too large Load Diff
+19 -9
View File
@@ -65,9 +65,8 @@ macro_rules! icf (
{
IConvFlags {
ctable: $ctable,
cbs: None,
block: false,
unblock: false,
block: None,
unblock: None,
swab: false,
sync: false,
noerror: false,
@@ -87,6 +86,7 @@ macro_rules! make_spec_test (
$test_name,
Input {
src: $src,
non_ascii: false,
ibs: 512,
xfer_stats: StatusLevel::None,
cflags: icf!(),
@@ -132,6 +132,7 @@ macro_rules! make_conv_test (
$test_name,
Input {
src: $src,
non_ascii: false,
ibs: 512,
xfer_stats: StatusLevel::None,
cflags: icf!($ctable),
@@ -156,6 +157,7 @@ macro_rules! make_icf_test (
$test_name,
Input {
src: $src,
non_ascii: false,
ibs: 512,
xfer_stats: StatusLevel::None,
cflags: $icf,
@@ -282,6 +284,7 @@ fn all_valid_ascii_ebcdic_ascii_roundtrip_conv_test()
let i = Input {
src: File::open("./test-resources/all-valid-ascii-chars-37eff01866ba3f538421b30b7cbefcac.test").unwrap(),
non_ascii: false,
ibs: 128,
xfer_stats: StatusLevel::None,
cflags: icf!(Some(&ASCII_TO_EBCDIC)),
@@ -303,6 +306,7 @@ fn all_valid_ascii_ebcdic_ascii_roundtrip_conv_test()
let i = Input {
src: File::open(&tmp_fname_ae).unwrap(),
non_ascii: false,
ibs: 256,
xfer_stats: StatusLevel::None,
cflags: icf!(Some(&EBCDIC_TO_ASCII)),
@@ -343,9 +347,8 @@ make_icf_test!(
File::open("./test-resources/seq-byte-values.test").unwrap(),
IConvFlags {
ctable: None,
cbs: None,
block: false,
unblock: false,
block: None,
unblock: None,
swab: true,
sync: false,
noerror: false,
@@ -359,12 +362,19 @@ make_icf_test!(
File::open("./test-resources/seq-byte-values-odd.test").unwrap(),
IConvFlags {
ctable: None,
cbs: None,
block: false,
unblock: false,
block: None,
unblock: None,
swab: true,
sync: false,
noerror: false,
},
File::open("./test-resources/seq-byte-values-odd.spec").unwrap()
);
fn block_test_basic()
{
let mut buf = vec![0u8, 1u8, 2u8, 3u8];
let res = block(&buf, 4);
assert_eq!(res, vec![buf]);
}
+35 -25
View File
@@ -25,6 +25,7 @@ pub enum ParseError
NoMatchingMultiplier(String),
ByteStringContainsNoValue(String),
MultiplierStringWouldOverflow(String),
BlockUnblockWithoutCBS,
}
impl std::fmt::Display for ParseError
@@ -298,7 +299,6 @@ fn parse_cbs(matches: &getopts::Matches) -> Result<Option<usize>, ParseError>
pub fn parse_status_level(matches: &getopts::Matches) -> Result<StatusLevel, ParseError>
{
// TODO: Impl
unimplemented!()
}
@@ -322,7 +322,7 @@ fn parse_ctable(fmt: Option<ConvFlag>, case: Option<ConvFlag>) -> Option<&'stati
{
match (fmt, case)
{
// Both specified
// Both [ascii | ebcdic | ibm] and [lcase | ucase] specified
(Some(fmt), Some(case)) =>
match (fmt, case)
{
@@ -341,7 +341,7 @@ fn parse_ctable(fmt: Option<ConvFlag>, case: Option<ConvFlag>) -> Option<&'stati
(_, _) =>
None,
},
// Only one of {ascii, ebcdic, ibm} specified
// Only [ascii | ebcdic | ibm] specified
(Some(fmt), None) =>
match fmt
{
@@ -354,7 +354,7 @@ fn parse_ctable(fmt: Option<ConvFlag>, case: Option<ConvFlag>) -> Option<&'stati
_ =>
None,
},
// Only one of {ucase, lcase} specified
// Only [lcase | ucase] specified
(None, Some(ConvFlag::UCase)) =>
Some(&ASCII_LCASE_TO_UCASE),
(None, Some(ConvFlag::LCase)) =>
@@ -389,8 +389,8 @@ pub fn parse_conv_flag_input(matches: &getopts::Matches) -> Result<IConvFlags, P
let mut fmt = None;
let mut case = None;
let mut block = false;
let mut unblock = false;
let mut block = None;
let mut unblock = None;
let mut swab = false;
let mut sync = false;
let mut noerror = false;
@@ -445,22 +445,24 @@ pub fn parse_conv_flag_input(matches: &getopts::Matches) -> Result<IConvFlags, P
case = Some(flag)
},
ConvFlag::Block =>
if !unblock
match (cbs, unblock)
{
block = true;
}
else
{
return Err(ParseError::MultipleBlockUnblock);
(Some(cbs), None) =>
block = Some(cbs),
(None, _) =>
return Err(ParseError::BlockUnblockWithoutCBS),
(_, Some(_)) =>
return Err(ParseError::MultipleBlockUnblock),
},
ConvFlag::Unblock =>
if !block
match (cbs, block)
{
unblock = true;
}
else
{
return Err(ParseError::MultipleBlockUnblock);
(Some(cbs), None) =>
unblock = Some(cbs),
(None, _) =>
return Err(ParseError::BlockUnblockWithoutCBS),
(_, Some(_)) =>
return Err(ParseError::MultipleBlockUnblock),
},
ConvFlag::Swab =>
swab = true,
@@ -476,7 +478,6 @@ pub fn parse_conv_flag_input(matches: &getopts::Matches) -> Result<IConvFlags, P
Ok(IConvFlags {
ctable,
cbs,
block,
unblock,
swab,
@@ -580,8 +581,6 @@ pub fn parse_iflags(matches: &getopts::Matches) -> Result<IFlags, ParseError>
sync = true,
Flag::NoCache =>
nocache = true,
Flag::NoCache =>
nocache = true,
Flag::NonBlock =>
nonblock = true,
Flag::NoATime =>
@@ -665,8 +664,6 @@ pub fn parse_oflags(matches: &getopts::Matches) -> Result<OFlags, ParseError>
sync = true,
Flag::NoCache =>
nocache = true,
Flag::NoCache =>
nocache = true,
Flag::NonBlock =>
nonblock = true,
Flag::NoATime =>
@@ -718,7 +715,7 @@ pub fn parse_skip_amt(ibs: &usize, iflags: &IFlags, matches: &getopts::Matches)
}
else
{
let n = parse_bytes_only(&amt)?;
let n = parse_bytes_with_opt_multiplier(amt)?;
Ok(Some(ibs*n))
}
}
@@ -740,7 +737,7 @@ pub fn parse_seek_amt(obs: &usize, oflags: &OFlags, matches: &getopts::Matches)
}
else
{
let n = parse_bytes_only(&amt)?;
let n = parse_bytes_with_opt_multiplier(amt)?;
Ok(Some(obs*n))
}
}
@@ -749,3 +746,16 @@ pub fn parse_seek_amt(obs: &usize, oflags: &OFlags, matches: &getopts::Matches)
Ok(None)
}
}
/// Parse whether the args indicate the input is not ascii
pub fn parse_input_non_ascii(matches: &getopts::Matches) -> Result<bool, ParseError>
{
if let Some(conv_opts) = matches.opt_str("conv")
{
Ok(conv_opts.contains("ascii"))
}
else
{
Ok(false)
}
}
+2 -3
View File
@@ -14,9 +14,8 @@ fn build_icf()
{
let icf_expd = IConvFlags {
ctable: Some(&ASCII_TO_IBM),
cbs: None,
block: false,
unblock: false,
block: None,
unblock: None,
swab: false,
sync: false,
noerror: false,