Skip to content

Commit

Permalink
fix tests, fix clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
hamaluik committed Mar 16, 2023
1 parent 526c2c4 commit c01ae94
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Separators {
}

#[derive(Debug)]
pub(crate) struct MSH {
pub(crate) struct Msh {
pub range: Range<usize>,
pub separators: Separators,
pub fields: Vec<Field>,
Expand Down
42 changes: 14 additions & 28 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ impl<'s> Message<'s> {
pub fn segment<S: AsRef<str>>(&'s self, segment: S) -> Option<&'s Segment> {
self.segments
.get(segment.as_ref())
.map(|seg| seg.get(0))
.flatten()
.and_then(|seg| seg.get(0))
}

/// Return the number of times segments identified by `segment` are present in the message
Expand All @@ -119,8 +118,7 @@ impl<'s> Message<'s> {
pub fn segment_n<S: AsRef<str>>(&'s self, segment: S, n: usize) -> Option<&'s Segment> {
self.segments
.get(segment.as_ref())
.map(|seg| seg.get(n))
.flatten()
.and_then(|seg| seg.get(n))
}

/// Directly get the source (not yet decoded) for a given field, if it exists in the message. The
Expand Down Expand Up @@ -245,15 +243,10 @@ impl<'s> Message<'s> {
/// the cursor is located in (if any)
pub fn locate_cursor(&'s self, cursor: usize) -> LocatedData<'s> {
let segment = self.segment_at_cursor(cursor);
let field = segment
.map(|(_, _, segment)| segment.field_at_cursor(cursor))
.flatten();
let component = field
.map(|(_, field)| field.component_at_cursor(cursor))
.flatten();
let sub_component = component
.map(|(_, component)| component.sub_component_at_cursor(cursor))
.flatten();
let field = segment.and_then(|(_, _, segment)| segment.field_at_cursor(cursor));
let component = field.and_then(|(_, field)| field.component_at_cursor(cursor));
let sub_component =
component.and_then(|(_, component)| component.sub_component_at_cursor(cursor));
LocatedData {
segment,
field,
Expand Down Expand Up @@ -325,8 +318,7 @@ impl MessageBuf {
pub fn segment<S: AsRef<str>>(&self, segment: S) -> Option<&Segment> {
self.segments
.get(segment.as_ref())
.map(|seg| seg.get(0))
.flatten()
.and_then(|seg| seg.get(0))
}

/// Return the number of times segments identified by `segment` are present in the message
Expand All @@ -342,8 +334,7 @@ impl MessageBuf {
pub fn segment_n<S: AsRef<str>>(&self, segment: S, n: usize) -> Option<&Segment> {
self.segments
.get(segment.as_ref())
.map(|seg| seg.get(n))
.flatten()
.and_then(|seg| seg.get(n))
}

/// Directly get the source (not yet decoded) for a given field, if it exists in the message. The
Expand Down Expand Up @@ -467,17 +458,12 @@ impl MessageBuf {

/// Deeply locate the cursor by returning the sub-component, component, field, and segment that
/// the cursor is located in (if any)
pub fn locate_cursor<'s>(&'s self, cursor: usize) -> LocatedData<'s> {
pub fn locate_cursor(&self, cursor: usize) -> LocatedData {
let segment = self.segment_at_cursor(cursor);
let field = segment
.map(|(_, _, segment)| segment.field_at_cursor(cursor))
.flatten();
let component = field
.map(|(_, field)| field.component_at_cursor(cursor))
.flatten();
let sub_component = component
.map(|(_, component)| component.sub_component_at_cursor(cursor))
.flatten();
let field = segment.and_then(|(_, _, segment)| segment.field_at_cursor(cursor));
let component = field.and_then(|(_, field)| field.component_at_cursor(cursor));
let sub_component =
component.and_then(|(_, component)| component.sub_component_at_cursor(cursor));
LocatedData {
segment,
field,
Expand Down Expand Up @@ -561,7 +547,7 @@ mod test {
.segment_at_cursor(cursor)
.expect("can get segment at cursor");
assert_eq!(id, "IN1");
assert_eq!(n, 0);
assert_eq!(n, 1);

let (n, field) = seg
.field_at_cursor(cursor)
Expand Down
6 changes: 3 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn parse_separators(s: Span) -> IResult<Span, Separators> {
let (s, source_encoding) = take(4u8)(s)?;

let source_field = source_field.fragment();
let field = source_field.chars().nth(0).unwrap();
let field = source_field.chars().next().unwrap();

let source_encoding = source_encoding.fragment();
let mut ec = source_encoding.chars();
Expand Down Expand Up @@ -113,7 +113,7 @@ fn fields_parser(separators: Separators) -> impl Fn(Span) -> IResult<Span, Vec<F
}
}

fn parse_msh(s: Span) -> IResult<Span, MSH> {
fn parse_msh(s: Span) -> IResult<Span, Msh> {
let (s, start_pos) = position(s)?;

let (s, _) = tag("MSH")(s)?;
Expand All @@ -127,7 +127,7 @@ fn parse_msh(s: Span) -> IResult<Span, MSH> {

Ok((
s,
MSH {
Msh {
range: start_pos.location_offset()..end_pos.location_offset(),
separators,
fields,
Expand Down
4 changes: 2 additions & 2 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ fn parse_segment_id(s: &str) -> VResult<&str, &str> {
}

fn is_digit_base_10(c: char) -> bool {
c.is_digit(10)
c.is_ascii_digit()
}

fn parse_nonzero_integer(s: &str) -> VResult<&str, NonZeroUsize> {
let (_s, val) = preceded(one_of(".- "), take_while1(is_digit_base_10))(s)?;
let val = usize::from_str_radix(val, 10).map_err(|_| {
let val = val.parse::<usize>().map_err(|_| {
nom::Err::Failure(VerboseError {
errors: vec![(
s,
Expand Down
8 changes: 4 additions & 4 deletions src/segment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{num::NonZeroUsize, ops::Range};

use crate::{Field, MSH};
use crate::{Field, Msh};

/// Represents an HL7v2 segment
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -84,9 +84,9 @@ impl FieldAccessor for Option<&Segment> {
}
}

impl From<MSH> for Segment {
fn from(msh: MSH) -> Self {
let MSH {
impl From<Msh> for Segment {
fn from(msh: Msh) -> Self {
let Msh {
range, mut fields, ..
} = msh;
fields.insert(
Expand Down
16 changes: 9 additions & 7 deletions src/time_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ use time::{Date, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset};
/// * `s` - A string slice representing the HL7 timestamp (format: `YYYY[MM[DD[HH[MM[SS[.S[S[S[S]]]]]]]]][+/-ZZZZ]`)
pub fn parse_time<'s>(s: &'s str) -> Result<OffsetDateTime, TimeParseError> {
fn is_decimal_digit(c: char) -> bool {
c.is_digit(10)
c.is_ascii_digit()
}

fn from_digits(i: Span) -> Result<usize, std::num::ParseIntError> {
usize::from_str_radix(i.fragment(), 10)
i.fragment().parse::<usize>()
}

fn digit2(input: Span) -> IResult<Span, usize> {
Expand Down Expand Up @@ -85,7 +85,9 @@ pub fn parse_time<'s>(s: &'s str) -> Result<OffsetDateTime, TimeParseError> {
4 => 100,
_ => panic!("second_fracs.len() not in 1..=4"),
};
let microseconds = usize::from_str_radix(second_fracs.fragment(), 10)
let microseconds = second_fracs
.fragment()
.parse::<usize>()
.expect("can parse fractional seconds as number")
* fracs_multiplier;
let time = Time::from_hms_micro(hour as u8, minute as u8, second as u8, microseconds as u32)
Expand Down Expand Up @@ -122,7 +124,7 @@ mod test {
assert_eq!(ts.day(), 12);
assert_eq!(ts.hour(), 19);
assert_eq!(ts.minute(), 59);
assert_eq!(ts.second(), 05);
assert_eq!(ts.second(), 5);
assert_eq!(ts.microsecond(), 123_400);
assert_eq!(ts.offset().whole_hours(), -7);
assert_eq!(ts.offset().minutes_past_hour(), 0);
Expand All @@ -138,7 +140,7 @@ mod test {
assert_eq!(ts.day(), 12);
assert_eq!(ts.hour(), 19);
assert_eq!(ts.minute(), 59);
assert_eq!(ts.second(), 05);
assert_eq!(ts.second(), 5);
assert_eq!(ts.microsecond(), 123_400);
assert!(ts.offset().is_utc());
}
Expand All @@ -153,7 +155,7 @@ mod test {
assert_eq!(ts.day(), 12);
assert_eq!(ts.hour(), 19);
assert_eq!(ts.minute(), 59);
assert_eq!(ts.second(), 05);
assert_eq!(ts.second(), 5);
assert_eq!(ts.microsecond(), 0);
assert!(ts.offset().is_utc());
}
Expand All @@ -168,7 +170,7 @@ mod test {
assert_eq!(ts.day(), 12);
assert_eq!(ts.hour(), 19);
assert_eq!(ts.minute(), 59);
assert_eq!(ts.second(), 05);
assert_eq!(ts.second(), 5);
assert_eq!(ts.microsecond(), 0);
assert_eq!(ts.offset().whole_hours(), -7);
assert_eq!(ts.offset().minutes_past_hour(), 0);
Expand Down

0 comments on commit c01ae94

Please sign in to comment.