Skip to content

Commit

Permalink
chore: Cargo clippy and fmt pass
Browse files Browse the repository at this point in the history
  • Loading branch information
wokket authored and RageLtMan committed Aug 18, 2021
1 parent eaef4a9 commit a553051
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 34 deletions.
16 changes: 9 additions & 7 deletions benches/simple_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ fn get_segments_by_name(c: &mut Criterion) {
}

fn get_msh_and_read_field(c: &mut Criterion) {

c.bench_function("Read Field from MSH (variable)", |b| {
let m = Message::try_from(get_sample_message()).unwrap();

Expand All @@ -35,14 +34,13 @@ fn get_msh_and_read_field(c: &mut Criterion) {

if let Some(Segment::MSH(msh)) = seg {
let _app = msh.msh_3_sending_application.as_ref().unwrap(); // direct variable access
//println!("{}", _app.value());
//println!("{}", _app.value());
}
})
});
}

fn get_pid_and_read_field_via_vec(c: &mut Criterion) {

c.bench_function("Read Field from PID (lookup)", |b| {
let m = Message::try_from(get_sample_message()).unwrap();

Expand All @@ -58,18 +56,22 @@ fn get_pid_and_read_field_via_vec(c: &mut Criterion) {
}

fn get_pid_and_read_field_via_query(c: &mut Criterion) {

c.bench_function("Read Field from PID (query)", |b| {
let m = Message::try_from(get_sample_message()).unwrap();

b.iter(|| {
let _val = m.query("PID.F3"); // query via Message
assert_eq!(_val, "555-44-4444"); // lookup from vec

})
});
}


criterion_group!(benches, message_parse, get_segments_by_name, get_msh_and_read_field, get_pid_and_read_field_via_vec, get_pid_and_read_field_via_query);
criterion_group!(
benches,
message_parse,
get_segments_by_name,
get_msh_and_read_field,
get_pid_and_read_field_via_vec,
get_pid_and_read_field_via_query
);
criterion_main!(benches);
18 changes: 11 additions & 7 deletions src/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ pub struct Field<'a> {

impl<'a> Field<'a> {
/// Convert the given line of text into a field.
pub fn parse<S: Into<&'a str>>(input: S, delims: &Separators) -> Result<Field<'a>, Hl7ParseError> {
pub fn parse<S: Into<&'a str>>(
input: S,
delims: &Separators,
) -> Result<Field<'a>, Hl7ParseError> {
let input = input.into();
let components = input.split(delims.component).collect::<Vec<&'a str>>();
let subcomponents = components
Expand Down Expand Up @@ -72,8 +75,9 @@ impl<'a> Field<'a> {
/// Access string reference of a Field component by String index
/// Adjust the index by one as medical people do not count from zero
pub fn query<'b, S>(&self, sidx: S) -> &'a str
where S: Into<&'b str> {

where
S: Into<&'b str>,
{
let sidx = sidx.into();
let parts = sidx.split('.').collect::<Vec<&str>>();

Expand All @@ -84,7 +88,7 @@ impl<'a> Field<'a> {
.collect::<String>();
let idx: usize = stringnums.parse().unwrap();

&self[idx - 1]
self[idx - 1]
} else if parts.len() == 2 {
let stringnums = parts[0]
.chars()
Expand All @@ -100,7 +104,7 @@ impl<'a> Field<'a> {

let idx1: usize = stringnums.parse().unwrap();

&self[(idx0 - 1, idx1 - 1)]
self[(idx0 - 1, idx1 - 1)]
} else {
""
}
Expand Down Expand Up @@ -148,7 +152,7 @@ impl<'a> Index<(usize, usize)> for Field<'a> {
/// DEPRECATED. Access string reference of a Field component by String index
/// Adjust the index by one as medical people do not count from zero
#[allow(useless_deprecated)]
#[deprecated(note="This will be removed in a future version")]
#[deprecated(note = "This will be removed in a future version")]
impl<'a> Index<String> for Field<'a> {
type Output = &'a str;
fn index(&self, sidx: String) -> &Self::Output {
Expand Down Expand Up @@ -189,7 +193,7 @@ impl<'a> Index<&str> for Field<'a> {

/// DEPRECATED. Access Segment, Field, or sub-field string references by string index
#[allow(useless_deprecated)]
#[deprecated(note="This will be removed in a future version")]
#[deprecated(note = "This will be removed in a future version")]
fn index(&self, idx: &str) -> &Self::Output {
&self[String::from(idx)]
}
Expand Down
20 changes: 10 additions & 10 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ impl<'a> Message<'a> {
}

/// Access Segment, Field, or sub-field string references by string index
pub fn query<'b, S>(&self, idx: S) -> &'a str
where S: Into<&'b str> {

pub fn query<'b, S>(&self, idx: S) -> &'a str
where
S: Into<&'b str>,
{
let idx = idx.into();

// Parse index elements
Expand All @@ -111,27 +112,26 @@ impl<'a> Message<'a> {
match seg_index {
//TODO: What is this doing...
Some(_) => {}
None => return &"",
None => return "",
}

let seg = &self.segments[seg_index.unwrap()];

// Return the appropriate source reference
match seg {
// Short circuit for now
Segment::MSH(m) => &m.source,
Segment::MSH(m) => m.source,
// Parse out slice depth
Segment::Generic(g) => {
if indices.len() < 2 {
&g.source
g.source
} else {
let query = indices[1..].join(".");
&g.query(&*query)
g.query(&*query)
}
}
}
}

}

impl<'a> TryFrom<&'a str> for Message<'a> {
Expand Down Expand Up @@ -204,7 +204,7 @@ impl<'a> Index<String> for Message<'a> {

/// DEPRECATED. Access Segment, Field, or sub-field string references by string index
#[allow(useless_deprecated)]
#[deprecated(note="This will be removed in a future version")]
#[deprecated(note = "This will be removed in a future version")]
fn index(&self, idx: String) -> &Self::Output {
// Parse index elements
let indices: Vec<&str> = idx.split('.').collect();
Expand Down Expand Up @@ -240,7 +240,7 @@ impl<'a> Index<&str> for Message<'a> {

/// DEPRECATED. Access Segment, Field, or sub-field string references by string index
#[allow(useless_deprecated)]
#[deprecated(note="This will be removed in a future version")]
#[deprecated(note = "This will be removed in a future version")]
fn index(&self, idx: &str) -> &Self::Output {
&self[String::from(idx)]
}
Expand Down
18 changes: 11 additions & 7 deletions src/segments/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ pub struct GenericSegment<'a> {

impl<'a> GenericSegment<'a> {
/// Convert the given line of text into a GenericSegment.
pub fn parse<S: Into<&'a str>>(input: S, delims: &Separators) -> Result<GenericSegment<'a>, Hl7ParseError> {
pub fn parse<S: Into<&'a str>>(
input: S,
delims: &Separators,
) -> Result<GenericSegment<'a>, Hl7ParseError> {
let input = input.into();

let fields: Result<Vec<Field<'a>>, Hl7ParseError> = input
Expand All @@ -37,8 +40,9 @@ impl<'a> GenericSegment<'a> {

/// Access Field as string reference
pub fn query<'b, S>(&self, fidx: S) -> &'a str
where S: Into<&'b str> {

where
S: Into<&'b str>,
{
let fidx = fidx.into();
let sections = fidx.split('.').collect::<Vec<&str>>();

Expand All @@ -49,7 +53,7 @@ impl<'a> GenericSegment<'a> {
.filter(|c| c.is_digit(10))
.collect::<String>();
let idx: usize = stringnum.parse().unwrap();
&self[idx]
self[idx]
}
_ => {
let stringnum = sections[0]
Expand Down Expand Up @@ -140,7 +144,7 @@ impl<'a> Index<&str> for GenericSegment<'a> {

/// DEPRECATED. Access Segment, Field, or sub-field string references by string index
#[allow(useless_deprecated)]
#[deprecated(note="This will be removed in a future version")]
#[deprecated(note = "This will be removed in a future version")]
fn index(&self, idx: &str) -> &Self::Output {
&self[String::from(idx)]
}
Expand Down Expand Up @@ -171,8 +175,8 @@ mod tests {
let msg = Message::try_from(hl7).unwrap();
let (f, c, s, oob) = match &msg.segments[1] {
Segment::Generic(x) => (
x.query("F1"), //&str
x.query("F1.R2"), // &str
x.query("F1"), //&str
x.query("F1.R2"), // &str
x.query(&*String::from("F1.R2.C1")), //String
String::from(x.query("F10")) + x.query("F1.R10") + x.query("F1.R2.C10"),
),
Expand Down
7 changes: 5 additions & 2 deletions src/segments/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
pub mod generic;
pub mod msh;

use std::fmt::Display;
use super::fields::Field;
use super::separators::Separators;
use super::*;
use generic::GenericSegment;
use msh::MshSegment;
use std::fmt::Display;

/// A single segment, 0x13 delimited line from a source HL7 message consisting of multiple fields.
#[derive(Debug, PartialEq)]
Expand All @@ -17,7 +17,10 @@ pub enum Segment<'a> {

impl<'a> Segment<'a> {
/// Convert the given line of text into a Segment.
pub fn parse<S: Into<&'a str>>(input: S, delims: &Separators) -> Result<Segment<'a>, Hl7ParseError> {
pub fn parse<S: Into<&'a str>>(
input: S,
delims: &Separators,
) -> Result<Segment<'a>, Hl7ParseError> {
let input = input.into();

let fields: Result<Vec<Field<'a>>, Hl7ParseError> = input
Expand Down
5 changes: 4 additions & 1 deletion src/segments/msh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ pub struct MshSegment<'a> {
}

impl<'a> MshSegment<'a> {
pub fn parse<S: Into<&'a str>>(input: S, delims: &Separators) -> Result<MshSegment<'a>, Hl7ParseError> {
pub fn parse<S: Into<&'a str>>(
input: S,
delims: &Separators,
) -> Result<MshSegment<'a>, Hl7ParseError> {
let input = input.into();

let mut fields = input.split(delims.field);
Expand Down

0 comments on commit a553051

Please sign in to comment.