-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1dadc4f
commit 31079e4
Showing
7 changed files
with
130 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::convert::TryFrom; | ||
|
||
#[derive(Debug)] | ||
pub enum LengthError { | ||
TooLong, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Clone, Copy)] | ||
pub enum FieldLength { | ||
One = 1, | ||
TwoDigit = 2, | ||
Abbreviated = 3, | ||
Wide = 4, | ||
Narrow = 5, | ||
Six = 6, | ||
} | ||
|
||
macro_rules! try_field_length { | ||
($i:ty) => { | ||
impl TryFrom<$i> for FieldLength { | ||
type Error = LengthError; | ||
|
||
fn try_from(input: $i) -> Result<Self, Self::Error> { | ||
Ok(match input { | ||
1 => Self::One, | ||
2 => Self::TwoDigit, | ||
3 => Self::Abbreviated, | ||
4 => Self::Wide, | ||
5 => Self::Narrow, | ||
6 => Self::Six, | ||
_ => return Err(LengthError::TooLong), | ||
}) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
try_field_length!(u8); | ||
try_field_length!(usize); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
mod length; | ||
mod symbols; | ||
|
||
pub use length::FieldLength; | ||
pub use symbols::*; | ||
|
||
use std::convert::{TryFrom, TryInto}; | ||
|
||
#[derive(Debug)] | ||
pub enum Error { | ||
TooLong(FieldSymbol), | ||
} | ||
|
||
#[derive(Debug, PartialEq, Clone, Copy)] | ||
pub struct Field { | ||
pub symbol: FieldSymbol, | ||
pub length: FieldLength, | ||
} | ||
|
||
impl Field {} | ||
|
||
impl From<(FieldSymbol, FieldLength)> for Field { | ||
fn from(input: (FieldSymbol, FieldLength)) -> Self { | ||
Field { | ||
symbol: input.0, | ||
length: input.1, | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<(FieldSymbol, usize)> for Field { | ||
type Error = Error; | ||
fn try_from(input: (FieldSymbol, usize)) -> Result<Self, Self::Error> { | ||
let (symbol, length) = ( | ||
input.0, | ||
input | ||
.1 | ||
.try_into() | ||
.map_err(|_| Self::Error::TooLong(input.0))?, | ||
); | ||
Ok(Field { symbol, length }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters