diff --git a/benches/simple_parse.rs b/benches/simple_parse.rs index d18d4ce..d003a6e 100644 --- a/benches/simple_parse.rs +++ b/benches/simple_parse.rs @@ -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(); @@ -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(); @@ -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); diff --git a/src/fields/mod.rs b/src/fields/mod.rs index ddc3918..13ef7b3 100644 --- a/src/fields/mod.rs +++ b/src/fields/mod.rs @@ -15,7 +15,10 @@ pub struct Field<'a> { impl<'a> Field<'a> { /// Convert the given line of text into a field. - pub fn parse>(input: S, delims: &Separators) -> Result, Hl7ParseError> { + pub fn parse>( + input: S, + delims: &Separators, + ) -> Result, Hl7ParseError> { let input = input.into(); let components = input.split(delims.component).collect::>(); let subcomponents = components @@ -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::>(); @@ -84,7 +88,7 @@ impl<'a> Field<'a> { .collect::(); let idx: usize = stringnums.parse().unwrap(); - &self[idx - 1] + self[idx - 1] } else if parts.len() == 2 { let stringnums = parts[0] .chars() @@ -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 { "" } @@ -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 for Field<'a> { type Output = &'a str; fn index(&self, sidx: String) -> &Self::Output { @@ -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)] } diff --git a/src/message.rs b/src/message.rs index abe06f8..96feeb8 100644 --- a/src/message.rs +++ b/src/message.rs @@ -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 @@ -111,7 +112,7 @@ impl<'a> Message<'a> { match seg_index { //TODO: What is this doing... Some(_) => {} - None => return &"", + None => return "", } let seg = &self.segments[seg_index.unwrap()]; @@ -119,19 +120,18 @@ impl<'a> Message<'a> { // 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> { @@ -204,7 +204,7 @@ impl<'a> Index 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(); @@ -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)] } diff --git a/src/segments/generic.rs b/src/segments/generic.rs index 9e4588a..d7f55f4 100644 --- a/src/segments/generic.rs +++ b/src/segments/generic.rs @@ -12,7 +12,10 @@ pub struct GenericSegment<'a> { impl<'a> GenericSegment<'a> { /// Convert the given line of text into a GenericSegment. - pub fn parse>(input: S, delims: &Separators) -> Result, Hl7ParseError> { + pub fn parse>( + input: S, + delims: &Separators, + ) -> Result, Hl7ParseError> { let input = input.into(); let fields: Result>, Hl7ParseError> = input @@ -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::>(); @@ -49,7 +53,7 @@ impl<'a> GenericSegment<'a> { .filter(|c| c.is_digit(10)) .collect::(); let idx: usize = stringnum.parse().unwrap(); - &self[idx] + self[idx] } _ => { let stringnum = sections[0] @@ -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)] } @@ -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"), ), diff --git a/src/segments/mod.rs b/src/segments/mod.rs index e84de02..af0a51a 100644 --- a/src/segments/mod.rs +++ b/src/segments/mod.rs @@ -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)] @@ -17,7 +17,10 @@ pub enum Segment<'a> { impl<'a> Segment<'a> { /// Convert the given line of text into a Segment. - pub fn parse>(input: S, delims: &Separators) -> Result, Hl7ParseError> { + pub fn parse>( + input: S, + delims: &Separators, + ) -> Result, Hl7ParseError> { let input = input.into(); let fields: Result>, Hl7ParseError> = input diff --git a/src/segments/msh.rs b/src/segments/msh.rs index a3de7b0..d9b8403 100644 --- a/src/segments/msh.rs +++ b/src/segments/msh.rs @@ -38,7 +38,10 @@ pub struct MshSegment<'a> { } impl<'a> MshSegment<'a> { - pub fn parse>(input: S, delims: &Separators) -> Result, Hl7ParseError> { + pub fn parse>( + input: S, + delims: &Separators, + ) -> Result, Hl7ParseError> { let input = input.into(); let mut fields = input.split(delims.field);