diff --git a/binex/README.md b/binex/README.md index b2b8a09a..81646006 100644 --- a/binex/README.md +++ b/binex/README.md @@ -22,6 +22,14 @@ or undisclosed elements. (private prototypes) * or use Message::decode to work on your own buffer directly. +Current limitations +=================== + ++ Big endian streams are fully validated & tested ++ Little endian streams are tested & verified but we don't have a dataset to confirm yet ++ Enhanced CRC (robust messaging) is not supported yet ++ MD5 checksum (very lengthy message prototypes) is implemented but not verified yet + Message Decoding ================ diff --git a/binex/src/decoder.rs b/binex/src/decoder.rs index 8d6a77a5..903307cc 100644 --- a/binex/src/decoder.rs +++ b/binex/src/decoder.rs @@ -85,7 +85,7 @@ impl<'a, R: Read> Decoder<'a, R> { /// // and user should react accordingly, /// break; /// }, - /// Error::ReversedStream | Error::LittleEndianStream => { + /// Error::ReversedStream => { /// // this library is currently limited: /// // - reversed streams are not supported yet /// // - little endian streams are not supported yet @@ -138,7 +138,7 @@ impl<'a, R: Read> Decoder<'a, R> { /// // and user should react accordingly, /// break; /// }, - /// Error::ReversedStream | Error::LittleEndianStream => { + /// Error::ReversedStream => { /// // this library is currently limited: /// // - reversed streams are not supported yet /// // - little endian streams are not supported yet @@ -198,7 +198,7 @@ impl<'a, R: Read> Iterator for Decoder<'a, R> { // we can safely discard everything self.wr_ptr = 0; self.rd_ptr = 0; - if self.eos == true { + if self.eos { // consumed everything and EOS has been reached return None; } @@ -211,7 +211,7 @@ impl<'a, R: Read> Iterator for Decoder<'a, R> { self.wr_ptr = 0; } - if self.eos == true { + if self.eos { // consumed everything and EOS has been reached return None; } diff --git a/binex/src/lib.rs b/binex/src/lib.rs index 3b6a744f..e88a16f9 100644 --- a/binex/src/lib.rs +++ b/binex/src/lib.rs @@ -16,7 +16,7 @@ pub mod prelude { message::{ EphemerisFrame, GALEphemeris, GLOEphemeris, GPSEphemeris, GPSRaw, Message, Meta, MonumentGeoMetadata, MonumentGeoRecord, PositionEcef3d, PositionGeo3d, Record, - SBASEphemeris, Solutions, TemporalSolution, Velocity3d, VelocityNED3d, + SBASEphemeris, Solutions, SolutionsFrame, TemporalSolution, Velocity3d, VelocityNED3d, }, stream::{ClosedSourceElement, Provider, StreamElement}, ClosedSourceMeta, Error, @@ -58,8 +58,6 @@ pub enum Error { // InvalidStartofStream, /// Library limitation: reversed streams are not supported ReversedStream, - /// Library limitation: little endian streams are not verified yet - LittleEndianStream, /// Library limitation: enhanced CRC is not supported yet EnhancedCrc, /// Found an unsupported timescale that we cannot interprate. diff --git a/binex/src/message/checksum.rs b/binex/src/message/checksum.rs index c5906f74..8ad3a7d6 100644 --- a/binex/src/message/checksum.rs +++ b/binex/src/message/checksum.rs @@ -84,21 +84,19 @@ impl Checksum { if enhanced { if mlen < 128 { Self::XOR16 - } else if mlen < 1048575 { + } else if mlen < 1048576 { Self::XOR32 } else { Self::MD5 } + } else if mlen < 128 { + Self::XOR8 + } else if mlen < 4096 { + Self::XOR16 + } else if mlen < 1048576 { + Self::XOR32 } else { - if mlen < 128 { - Self::XOR8 - } else if mlen < 4096 { - Self::XOR16 - } else if mlen < 1048575 { - Self::XOR32 - } else { - Self::MD5 - } + Self::MD5 } } /// Length we need to decode/encode this type of Checksum @@ -129,8 +127,18 @@ impl Checksum { u32::from_le_bytes([slice[0], slice[1], slice[2], slice[3]]) }; val_u32 as u128 + } else if big_endian { + u128::from_be_bytes([ + slice[0], slice[1], slice[2], slice[3], slice[4], slice[5], slice[6], slice[7], + slice[8], slice[9], slice[10], slice[11], slice[12], slice[13], slice[14], + slice[15], + ]) } else { - unimplemented!("md5"); + u128::from_le_bytes([ + slice[0], slice[1], slice[2], slice[3], slice[4], slice[5], slice[6], slice[7], + slice[8], slice[9], slice[10], slice[11], slice[12], slice[13], slice[14], + slice[15], + ]) } } /// Calculates expected Checksum for this msg @@ -273,4 +281,10 @@ mod test { assert_eq!(Checksum::XOR16.calc(&buf, buf.len()), 0x5376); } + #[test] + fn test_md5() { + let ck = Checksum::MD5; + let bytes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + let _ = ck.calc(&bytes, bytes.len()); + } } diff --git a/binex/src/message/meta.rs b/binex/src/message/meta.rs index 3a0c288b..6a2af9ff 100644 --- a/binex/src/message/meta.rs +++ b/binex/src/message/meta.rs @@ -40,27 +40,21 @@ impl Meta { } else { Self::REV_SYNC_BE_STANDARD_CRC } + } else if self.enhanced_crc { + Self::REV_SYNC_LE_ENHANCED_CRC } else { - if self.enhanced_crc { - Self::REV_SYNC_LE_ENHANCED_CRC - } else { - Self::REV_SYNC_LE_STANDARD_CRC - } + Self::REV_SYNC_LE_STANDARD_CRC } - } else { - if self.big_endian { - if self.enhanced_crc { - Self::FWD_SYNC_BE_ENHANCED_CRC - } else { - Self::FWD_SYNC_BE_STANDARD_CRC - } + } else if self.big_endian { + if self.enhanced_crc { + Self::FWD_SYNC_BE_ENHANCED_CRC } else { - if self.enhanced_crc { - Self::FWD_SYNC_LE_ENHANCED_CRC - } else { - Self::FWD_SYNC_LE_STANDARD_CRC - } + Self::FWD_SYNC_BE_STANDARD_CRC } + } else if self.enhanced_crc { + Self::FWD_SYNC_LE_ENHANCED_CRC + } else { + Self::FWD_SYNC_LE_STANDARD_CRC } } /// Locate SYNC byte in provided buffer @@ -192,65 +186,65 @@ mod test { // FWD +LE +STANDARD let (meta, size) = meta.expect("did not detect sync byte"); assert_eq!(size, 1); - assert_eq!(meta.reversed, false); - assert_eq!(meta.big_endian, false); - assert_eq!(meta.enhanced_crc, false); + assert!(!meta.reversed); + assert!(!meta.big_endian); + assert!(!meta.enhanced_crc); }, 0xe2 => { // FWD +BE +STANDARD let (meta, size) = meta.expect("did not detect sync byte"); assert_eq!(size, 1); - assert_eq!(meta.reversed, false); - assert_eq!(meta.big_endian, true); - assert_eq!(meta.enhanced_crc, false); + assert!(!meta.reversed); + assert!(meta.big_endian); + assert!(!meta.enhanced_crc); }, 0xc8 => { // FWD +LE +ENHANCED let (meta, size) = meta.expect("did not detect sync byte"); assert_eq!(size, 1); - assert_eq!(meta.reversed, false); - assert_eq!(meta.big_endian, false); - assert_eq!(meta.enhanced_crc, true); + assert!(!meta.reversed); + assert!(!meta.big_endian); + assert!(meta.enhanced_crc); }, 0xe8 => { // FWD +BE +ENHANCED let (meta, size) = meta.expect("did not detect sync byte"); assert_eq!(size, 1); - assert_eq!(meta.reversed, false); - assert_eq!(meta.big_endian, true); - assert_eq!(meta.enhanced_crc, true); + assert!(!meta.reversed); + assert!(meta.big_endian); + assert!(meta.enhanced_crc); }, 0xd2 => { // REV +LE +STANDARD let (meta, size) = meta.expect("did not detect sync byte"); assert_eq!(size, 1); - assert_eq!(meta.reversed, true); - assert_eq!(meta.big_endian, false); - assert_eq!(meta.enhanced_crc, false); + assert!(meta.reversed); + assert!(!meta.big_endian); + assert!(!meta.enhanced_crc); }, 0xf2 => { // REV +BE + STANDARD let (meta, size) = meta.expect("did not detect sync byte"); assert_eq!(size, 1); - assert_eq!(meta.reversed, true); - assert_eq!(meta.big_endian, true); - assert_eq!(meta.enhanced_crc, false); + assert!(meta.reversed); + assert!(meta.big_endian); + assert!(!meta.enhanced_crc); }, 0xd8 => { // REV +LE +ENHANCED let (meta, size) = meta.expect("did not detect sync byte"); assert_eq!(size, 1); - assert_eq!(meta.reversed, true); - assert_eq!(meta.big_endian, false); - assert_eq!(meta.enhanced_crc, true); + assert!(meta.reversed); + assert!(!meta.big_endian); + assert!(meta.enhanced_crc); }, 0xf8 => { // REV +BE +ENHANCED let (meta, size) = meta.expect("did not detect sync byte"); assert_eq!(size, 1); - assert_eq!(meta.reversed, true); - assert_eq!(meta.big_endian, true); - assert_eq!(meta.enhanced_crc, true); + assert!(meta.reversed); + assert!(meta.big_endian); + assert!(meta.enhanced_crc); }, _ => { assert!(meta.is_none(), "found invalid sync byte for 0x{:01x}", val8); diff --git a/binex/src/message/mid.rs b/binex/src/message/mid.rs index 8cf9779a..1c8ef25b 100644 --- a/binex/src/message/mid.rs +++ b/binex/src/message/mid.rs @@ -9,20 +9,20 @@ pub enum MessageID { SiteMonumentMarker = 0, /// Decode Ephemeris frame Ephemeris = 1, - /// Observation time tag and receiver info - ObservationTimeTagRxInfo = 2, + /// Observation, time tag and receiver info + Observation = 2, /// Local Meteorological and Geophysical information Meteo = 3, /// Receiver info: BINEX specific ReceiverInfo = 4, /// Processed Solutions like PVT ProcessedSolutions = 5, - // Receiver info prototyping: BINEX specific - ReceiverInfoPrototyping = 125, - /// Meteo prototyping: BINEX specific - MeteoPrototyping = 126, - /// Observation time tag prototyping: BINEX specific - ObservationTimeTagRxPrototyping = 127, + // Receiver info prototype: BINEX specific + ReceiverInfoPrototype = 125, + /// Meteo prototype: BINEX specific + MeteoPrototype = 126, + /// Observation, time tag and receiver info prototype + ObservationPrototype = 127, // Unknown / unsupported message #[default] Unknown = 0xffffffff, @@ -33,13 +33,13 @@ impl From for MessageID { match val { 0 => Self::SiteMonumentMarker, 1 => Self::Ephemeris, - 2 => Self::ObservationTimeTagRxInfo, + 2 => Self::Observation, 3 => Self::Meteo, 4 => Self::ReceiverInfo, 5 => Self::ProcessedSolutions, - 125 => Self::ReceiverInfoPrototyping, - 126 => Self::MeteoPrototyping, - 127 => Self::ObservationTimeTagRxPrototyping, + 125 => Self::ReceiverInfoPrototype, + 126 => Self::MeteoPrototype, + 127 => Self::ObservationPrototype, _ => Self::Unknown, } } @@ -50,14 +50,14 @@ impl From for u32 { match val { MessageID::SiteMonumentMarker => 0, MessageID::Ephemeris => 1, - // MessageID::ObservationTimeTagRxInfo => 0x02, - // MessageID::Meteo => 0x03, - // MessageID::ReceiverInfo => 0x04, - // MessageID::ProcessedSolutions => 0x05, - // MessageID::ReceiverInfoPrototyping => 0x7d, - // MessageID::MeteoPrototyping => 0x7e, - // MessageID::ObservationTimeTagRxPrototyping => 0x7f, - _ => 0xffffffff, + MessageID::Observation => 2, + MessageID::Meteo => 3, + MessageID::ReceiverInfo => 4, + MessageID::ProcessedSolutions => 5, + MessageID::ReceiverInfoPrototype => 125, + MessageID::MeteoPrototype => 126, + MessageID::ObservationPrototype => 127, + _ => 0xff, } } } diff --git a/binex/src/message/mod.rs b/binex/src/message/mod.rs index 4f55247b..c679fdbd 100644 --- a/binex/src/message/mod.rs +++ b/binex/src/message/mod.rs @@ -11,7 +11,6 @@ pub use record::{ }; pub use meta::Meta; -pub use time::TimeResolution; pub(crate) use mid::MessageID; @@ -46,14 +45,15 @@ impl Message { let mut total = 1; // SYNC let mid = self.record.to_message_id() as u32; - total += Self::bnxi_encoding_size(mid); + let mid_1_4 = Self::bnxi_encoding_size(mid); + total += mid_1_4; let mlen = self.record.encoding_size(); - total += Self::bnxi_encoding_size(mlen as u32); + let mlen_1_4 = Self::bnxi_encoding_size(mlen as u32); + total += mlen_1_4; + total += mlen; - total += self.record.encoding_size(); - - let ck = Checksum::from_len(mlen, self.meta.enhanced_crc); + let ck = Checksum::from_len(mlen_1_4 + mlen + mid_1_4, self.meta.enhanced_crc); total += ck.len(); total @@ -82,7 +82,6 @@ impl Message { ///////////////////////////////////// // TODO: current library limitations ///////////////////////////////////// - if reversed { // Reversed streams: not understood return Err(Error::ReversedStream); @@ -91,10 +90,6 @@ impl Message { // Enhanced CRC scheme not implemented return Err(Error::EnhancedCrc); } - if !big_endian { - // Little endianess not tested - return Err(Error::LittleEndianStream); - } // make sure we can parse up to 4 byte MID if buf_len - sync_off < 4 { @@ -131,7 +126,7 @@ impl Message { // 4. parse RECORD let record = match mid { MessageID::SiteMonumentMarker => { - let rec = MonumentGeoRecord::decode(mlen as usize, big_endian, &buf[ptr..])?; + let rec = MonumentGeoRecord::decode(mlen, big_endian, &buf[ptr..])?; Record::new_monument_geo(rec) }, MessageID::Ephemeris => { @@ -139,7 +134,7 @@ impl Message { Record::new_ephemeris_frame(fr) }, MessageID::ProcessedSolutions => { - let solutions = Solutions::decode(mlen as usize, big_endian, &buf[ptr..])?; + let solutions = Solutions::decode(mlen, big_endian, &buf[ptr..])?; Record::new_solutions(solutions) }, MessageID::Unknown => { diff --git a/binex/src/message/record/ephemeris/galileo.rs b/binex/src/message/record/ephemeris/galileo.rs index c3a81aff..0b2d4fc6 100644 --- a/binex/src/message/record/ephemeris/galileo.rs +++ b/binex/src/message/record/ephemeris/galileo.rs @@ -367,55 +367,55 @@ mod test { #[test] fn gal_ephemeris() { - let buf = [0; 128]; - - let eph = GALEphemeris::decode(true, &buf).unwrap(); - - // test mirror - let mut target = [0; 100]; - assert!(eph.encode(true, &mut target).is_err()); - - let mut target = [0; 128]; - let size = eph.encode(true, &mut target).unwrap(); - assert_eq!(size, 128); - assert_eq!(buf, target); - - let eph = GALEphemeris { - sv_prn: 10, - clock_offset: 123.0, - clock_drift_rate: 130.0, - clock_drift: 150.0, - sqrt_a: 56.0, - m0_rad: 0.1, - e: 0.2, - cic: 0.3, - crc: 0.4, - cis: 0.5, - crs: 0.6, - cuc: 0.7, - cus: 0.8, - omega_0_rad: 0.9, - omega_rad: 59.0, - i0_rad: 61.0, - toe_week: 112, - tow: -10, - toe_s: -32, - bgd_e5a_e1_s: -3.14, - bgd_e5b_e1_s: -6.18, - iodnav: -25, - delta_n_semi_circles_s: 150.0, - omega_dot_semi_circles: 160.0, - idot_semi_circles_s: 5000.0, - sisa: 1000.0, - sv_health: 155, - source: 156, - }; - - let mut target = [0; 128]; - eph.encode(true, &mut target).unwrap(); - - let decoded = GALEphemeris::decode(true, &target).unwrap(); - - assert_eq!(eph, decoded); + for big_endian in [true, false] { + let buf = [0; 128]; + let eph = GALEphemeris::decode(big_endian, &buf).unwrap(); + + // test mirror + let mut target = [0; 100]; + assert!(eph.encode(big_endian, &mut target).is_err()); + + let mut target = [0; 128]; + let size = eph.encode(big_endian, &mut target).unwrap(); + assert_eq!(size, 128); + assert_eq!(buf, target); + + let eph = GALEphemeris { + sv_prn: 10, + clock_offset: 123.0, + clock_drift_rate: 130.0, + clock_drift: 150.0, + sqrt_a: 56.0, + m0_rad: 0.1, + e: 0.2, + cic: 0.3, + crc: 0.4, + cis: 0.5, + crs: 0.6, + cuc: 0.7, + cus: 0.8, + omega_0_rad: 0.9, + omega_rad: 59.0, + i0_rad: 61.0, + toe_week: 112, + tow: -10, + toe_s: -32, + bgd_e5a_e1_s: -3.14, + bgd_e5b_e1_s: -6.18, + iodnav: -25, + delta_n_semi_circles_s: 150.0, + omega_dot_semi_circles: 160.0, + idot_semi_circles_s: 5000.0, + sisa: 1000.0, + sv_health: 155, + source: 156, + }; + + let mut target = [0; 128]; + eph.encode(big_endian, &mut target).unwrap(); + + let decoded = GALEphemeris::decode(big_endian, &target).unwrap(); + assert_eq!(eph, decoded); + } } } diff --git a/binex/src/message/record/ephemeris/glonass.rs b/binex/src/message/record/ephemeris/glonass.rs index 785b5aa5..35256745 100644 --- a/binex/src/message/record/ephemeris/glonass.rs +++ b/binex/src/message/record/ephemeris/glonass.rs @@ -251,47 +251,48 @@ mod test { #[test] fn glo_ephemeris() { - let buf = [0; 135]; - let eph = GLOEphemeris::decode(true, &buf).unwrap(); - - // test mirror - let mut encoded = [0; 100]; - assert!(eph.encode(true, &mut encoded).is_err()); - - let mut encoded = [0; 135]; - let size = eph.encode(true, &mut encoded).unwrap(); - assert_eq!(size, 135); - assert_eq!(buf, encoded); - - let eph = GLOEphemeris { - t_k_sec: 0, - slot: 1, - day: 2, - tod_s: 3, - clock_offset_s: 1.0, - clock_rel_freq_bias: 2.0, - x_km: 3.0, - vel_x_km: 4.0, - acc_x_km: 4.0, - y_km: 5.0, - vel_y_km: 6.0, - acc_y_km: 7.0, - z_km: 8.0, - vel_z_km: 9.0, - acc_z_km: 10.0, - sv_health: 100, - freq_channel: -20, - age_op_days: 123, - leap_s: 124, - tau_gps_s: 3.14, - l1_l2_gd: 6.28, - }; - - let mut encoded = [0; 135]; - eph.encode(true, &mut encoded).unwrap(); - - let decoded = GLOEphemeris::decode(true, &encoded).unwrap(); - - assert_eq!(eph, decoded); + for big_endian in [true, false] { + let buf = [0; 135]; + let eph = GLOEphemeris::decode(big_endian, &buf).unwrap(); + + // test mirror + let mut encoded = [0; 100]; + assert!(eph.encode(big_endian, &mut encoded).is_err()); + + let mut encoded = [0; 135]; + let size = eph.encode(big_endian, &mut encoded).unwrap(); + assert_eq!(size, 135); + assert_eq!(buf, encoded); + + let eph = GLOEphemeris { + t_k_sec: 0, + slot: 1, + day: 2, + tod_s: 3, + clock_offset_s: 1.0, + clock_rel_freq_bias: 2.0, + x_km: 3.0, + vel_x_km: 4.0, + acc_x_km: 4.0, + y_km: 5.0, + vel_y_km: 6.0, + acc_y_km: 7.0, + z_km: 8.0, + vel_z_km: 9.0, + acc_z_km: 10.0, + sv_health: 100, + freq_channel: -20, + age_op_days: 123, + leap_s: 124, + tau_gps_s: 3.14, + l1_l2_gd: 6.28, + }; + + let mut encoded = [0; 135]; + eph.encode(big_endian, &mut encoded).unwrap(); + + let decoded = GLOEphemeris::decode(big_endian, &encoded).unwrap(); + assert_eq!(eph, decoded); + } } } diff --git a/binex/src/message/record/ephemeris/gps/eph.rs b/binex/src/message/record/ephemeris/gps/eph.rs index 2db90685..49b5f865 100644 --- a/binex/src/message/record/ephemeris/gps/eph.rs +++ b/binex/src/message/record/ephemeris/gps/eph.rs @@ -390,55 +390,56 @@ mod test { #[test] fn gps_ephemeris() { - let buf = [0; 128]; - - let eph = GPSEphemeris::decode(true, &buf).unwrap(); - - // test mirror - let mut target = [0; 100]; - assert!(eph.encode(true, &mut target).is_err()); - - let mut target = [0; 128]; - let size = eph.encode(true, &mut target).unwrap(); - assert_eq!(size, 128); - assert_eq!(buf, target); - - let eph = GPSEphemeris { - sv_prn: 10, - toe: 1000, - tow: 120, - toc: 130, - tgd: 10.0, - iodc: 24, - clock_offset: 123.0, - clock_drift_rate: 130.0, - clock_drift: 150.0, - sqrt_a: 56.0, - iode: -2000, - delta_n_rad_s: 12.0, - m0_rad: 0.1, - e: 0.2, - cic: 0.3, - crc: 0.4, - cis: 0.5, - crs: 0.6, - cuc: 0.7, - cus: 0.8, - omega_0_rad: 0.9, - omega_rad: 59.0, - i0_rad: 61.0, - omega_dot_rad_s: 62.0, - i_dot_rad_s: 74.0, - ura_m: 75.0, - sv_health: 16, - uint2: 17, - }; - - let mut target = [0; 153]; - eph.encode(true, &mut target).unwrap(); - - let decoded = GPSEphemeris::decode(true, &target).unwrap(); - - assert_eq!(eph, decoded); + for big_endian in [true, false] { + let buf = [0; 128]; + + let eph = GPSEphemeris::decode(big_endian, &buf).unwrap(); + + // test mirror + let mut target = [0; 100]; + assert!(eph.encode(big_endian, &mut target).is_err()); + + let mut target = [0; 128]; + let size = eph.encode(big_endian, &mut target).unwrap(); + assert_eq!(size, 128); + assert_eq!(buf, target); + + let eph = GPSEphemeris { + sv_prn: 10, + toe: 1000, + tow: 120, + toc: 130, + tgd: 10.0, + iodc: 24, + clock_offset: 123.0, + clock_drift_rate: 130.0, + clock_drift: 150.0, + sqrt_a: 56.0, + iode: -2000, + delta_n_rad_s: 12.0, + m0_rad: 0.1, + e: 0.2, + cic: 0.3, + crc: 0.4, + cis: 0.5, + crs: 0.6, + cuc: 0.7, + cus: 0.8, + omega_0_rad: 0.9, + omega_rad: 59.0, + i0_rad: 61.0, + omega_dot_rad_s: 62.0, + i_dot_rad_s: 74.0, + ura_m: 75.0, + sv_health: 16, + uint2: 17, + }; + + let mut target = [0; 153]; + eph.encode(big_endian, &mut target).unwrap(); + + let decoded = GPSEphemeris::decode(big_endian, &target).unwrap(); + assert_eq!(eph, decoded); + } } } diff --git a/binex/src/message/record/ephemeris/gps/raw.rs b/binex/src/message/record/ephemeris/gps/raw.rs index 4de7064e..f60ec85b 100644 --- a/binex/src/message/record/ephemeris/gps/raw.rs +++ b/binex/src/message/record/ephemeris/gps/raw.rs @@ -83,32 +83,32 @@ mod test { #[test] fn gps_raw() { - let big_endian = true; - - let buf = [0; 64]; - let decode = GPSRaw::decode(big_endian, &buf); - assert!(decode.is_err()); - - let mut buf = [0; 78]; - buf[0] = 10; - buf[1] = 1; - buf[6] = 10; - buf[7] = 11; - buf[6 + 71] = 123; - - let decoded = GPSRaw::decode(big_endian, &buf).unwrap(); - - assert_eq!(decoded.svid1, 10); - assert_eq!(decoded.uint1, 1); - assert_eq!(decoded.bytes.len(), 72); - - assert_eq!(decoded.bytes[0], 10); - assert_eq!(decoded.bytes[1], 11); - assert_eq!(decoded.bytes[71], 123); - - let mut encoded = [0; 78]; - let size = decoded.encode(big_endian, &mut encoded).unwrap(); - assert_eq!(size, 78); - assert_eq!(buf, encoded) + for big_endian in [true, false] { + let buf = [0; 64]; + let decode = GPSRaw::decode(big_endian, &buf); + assert!(decode.is_err()); + + let mut buf = [0; 78]; + buf[0] = 10; + buf[1] = 1; + buf[6] = 10; + buf[7] = 11; + buf[6 + 71] = 123; + + let decoded = GPSRaw::decode(big_endian, &buf).unwrap(); + + assert_eq!(decoded.svid1, 10); + assert_eq!(decoded.uint1, 1); + assert_eq!(decoded.bytes.len(), 72); + + assert_eq!(decoded.bytes[0], 10); + assert_eq!(decoded.bytes[1], 11); + assert_eq!(decoded.bytes[71], 123); + + let mut encoded = [0; 78]; + let size = decoded.encode(big_endian, &mut encoded).unwrap(); + assert_eq!(size, 78); + assert_eq!(buf, encoded) + } } } diff --git a/binex/src/message/record/ephemeris/mod.rs b/binex/src/message/record/ephemeris/mod.rs index 8ce8d94f..a7f0b30a 100644 --- a/binex/src/message/record/ephemeris/mod.rs +++ b/binex/src/message/record/ephemeris/mod.rs @@ -64,12 +64,12 @@ impl EphemerisFrame { /// [EphemerisFrame] decoding attempt from given [FieldID] pub(crate) fn decode(big_endian: bool, buf: &[u8]) -> Result { // cant decode 1-4b - if buf.len() < 1 { + if buf.is_empty() { return Err(Error::NotEnoughBytes); } // decode FID - let (bnxi, size) = Message::decode_bnxi(&buf, big_endian); + let (bnxi, size) = Message::decode_bnxi(buf, big_endian); let fid = FieldID::from(bnxi); match fid { @@ -151,130 +151,130 @@ mod test { #[test] fn gps_eph() { - let eph = GPSEphemeris { - sqrt_a: 1.0, - sv_health: 2, - sv_prn: 3, - toe: 4, - tow: 5, - toc: 6, - tgd: 7.0, - iodc: 8, - clock_drift: 9.0, - clock_drift_rate: 10.0, - clock_offset: 11.0, - iode: 13, - delta_n_rad_s: 14.0, - m0_rad: 15.0, - e: 16.0, - cic: 17.0, - crc: 18.0, - cis: 19.0, - crs: 20.0, - cuc: 21.0, - cus: 22.0, - omega_0_rad: 23.0, - omega_dot_rad_s: 24.0, - omega_rad: 25.0, - i0_rad: 27.0, - i_dot_rad_s: 28.0, - ura_m: 29.0, - uint2: 30, - }; - - assert_eq!(GPSEphemeris::encoding_size(), 128); - - let big_endian = true; - - let mut encoded = [0; 77]; - assert!(eph.encode(big_endian, &mut encoded).is_err()); - - let mut encoded = [0; 128]; - let size = eph.encode(big_endian, &mut encoded).unwrap(); - assert_eq!(size, 128); - - let decoded = GPSEphemeris::decode(big_endian, &encoded).unwrap(); - - assert_eq!(decoded, eph); + for big_endian in [true, false] { + let eph = GPSEphemeris { + sqrt_a: 1.0, + sv_health: 2, + sv_prn: 3, + toe: 4, + tow: 5, + toc: 6, + tgd: 7.0, + iodc: 8, + clock_drift: 9.0, + clock_drift_rate: 10.0, + clock_offset: 11.0, + iode: 13, + delta_n_rad_s: 14.0, + m0_rad: 15.0, + e: 16.0, + cic: 17.0, + crc: 18.0, + cis: 19.0, + crs: 20.0, + cuc: 21.0, + cus: 22.0, + omega_0_rad: 23.0, + omega_dot_rad_s: 24.0, + omega_rad: 25.0, + i0_rad: 27.0, + i_dot_rad_s: 28.0, + ura_m: 29.0, + uint2: 30, + }; + + assert_eq!(GPSEphemeris::encoding_size(), 128); + + let mut encoded = [0; 77]; + assert!(eph.encode(big_endian, &mut encoded).is_err()); + + let mut encoded = [0; 128]; + let size = eph.encode(big_endian, &mut encoded).unwrap(); + assert_eq!(size, 128); + + let decoded = GPSEphemeris::decode(big_endian, &encoded).unwrap(); + + assert_eq!(decoded, eph); + } } #[test] fn gps_raw() { - let raw: GPSRaw = GPSRaw::default(); - assert_eq!(GPSRaw::encoding_size(), 1 + 1 + 4 + 72); + for big_endian in [true, false] { + let raw: GPSRaw = GPSRaw::default(); + assert_eq!(GPSRaw::encoding_size(), 1 + 1 + 4 + 72); - let big_endian = true; + let mut buf = [0; 78]; + let size = raw.encode(big_endian, &mut buf).unwrap(); - let mut buf = [0; 78]; - let size = raw.encode(big_endian, &mut buf).unwrap(); + assert_eq!(size, GPSRaw::encoding_size()); + assert_eq!(buf, [0; 78]); - assert_eq!(size, GPSRaw::encoding_size()); - assert_eq!(buf, [0; 78]); - - let decoded = GPSRaw::decode(big_endian, &buf).unwrap(); - assert_eq!(decoded, raw); + let decoded = GPSRaw::decode(big_endian, &buf).unwrap(); + assert_eq!(decoded, raw); + } } #[test] fn gal() { - let gal = GALEphemeris { - toe_s: 1, - sv_health: 2, - sv_prn: 3, - toe_week: 4, - tow: 5, - bgd_e5a_e1_s: 6.0, - bgd_e5b_e1_s: 7.0, - iodnav: 8, - clock_drift: 9.0, - clock_drift_rate: 10.0, - clock_offset: 11.0, - delta_n_semi_circles_s: 12.0, - m0_rad: 12.0, - e: 13.0, - sqrt_a: 14.0, - cic: 15.0, - crc: 16.0, - cis: 17.0, - crs: 18.0, - cus: 19.0, - cuc: 20.0, - omega_0_rad: 21.0, - omega_dot_semi_circles: 22.0, - omega_rad: 23.0, - i0_rad: 25.0, - idot_semi_circles_s: 26.0, - sisa: 33.0, - source: 34, - }; - - assert_eq!(GALEphemeris::encoding_size(), 128); - - let big_endian = true; - - let mut buf = [0; 128]; - let size = gal.encode(big_endian, &mut buf).unwrap(); - - assert_eq!(size, GALEphemeris::encoding_size()); - - let decoded = GALEphemeris::decode(big_endian, &buf).unwrap(); - assert_eq!(decoded, gal); + for big_endian in [true, false] { + let gal = GALEphemeris { + toe_s: 1, + sv_health: 2, + sv_prn: 3, + toe_week: 4, + tow: 5, + bgd_e5a_e1_s: 6.0, + bgd_e5b_e1_s: 7.0, + iodnav: 8, + clock_drift: 9.0, + clock_drift_rate: 10.0, + clock_offset: 11.0, + delta_n_semi_circles_s: 12.0, + m0_rad: 12.0, + e: 13.0, + sqrt_a: 14.0, + cic: 15.0, + crc: 16.0, + cis: 17.0, + crs: 18.0, + cus: 19.0, + cuc: 20.0, + omega_0_rad: 21.0, + omega_dot_semi_circles: 22.0, + omega_rad: 23.0, + i0_rad: 25.0, + idot_semi_circles_s: 26.0, + sisa: 33.0, + source: 34, + }; + + assert_eq!(GALEphemeris::encoding_size(), 128); + + let mut buf = [0; 128]; + let size = gal.encode(big_endian, &mut buf).unwrap(); + + assert_eq!(size, GALEphemeris::encoding_size()); + + let decoded = GALEphemeris::decode(big_endian, &buf).unwrap(); + assert_eq!(decoded, gal); + } } #[test] fn sbas() { - let sbas = SBASEphemeris::default(); - assert_eq!(SBASEphemeris::encoding_size(), 98); + for big_endian in [true, false] { + let sbas = SBASEphemeris::default(); + assert_eq!(SBASEphemeris::encoding_size(), 98); - let big_endian = true; + let mut buf = [0; 98]; + let size = sbas.encode(big_endian, &mut buf).unwrap(); - let mut buf = [0; 98]; - let size = sbas.encode(big_endian, &mut buf).unwrap(); + assert_eq!(size, SBASEphemeris::encoding_size()); + assert_eq!(buf, [0; 98]); - assert_eq!(size, SBASEphemeris::encoding_size()); - assert_eq!(buf, [0; 98]); - - let decoded = SBASEphemeris::decode(big_endian, &buf).unwrap(); - assert_eq!(decoded, sbas); + let decoded = SBASEphemeris::decode(big_endian, &buf).unwrap(); + assert_eq!(decoded, sbas); + } } } diff --git a/binex/src/message/record/ephemeris/sbas.rs b/binex/src/message/record/ephemeris/sbas.rs index b0b0a68f..1475c40e 100644 --- a/binex/src/message/record/ephemeris/sbas.rs +++ b/binex/src/message/record/ephemeris/sbas.rs @@ -210,43 +210,44 @@ mod test { #[test] fn sbas_ephemeris() { - let buf = [0; 100]; - let eph = SBASEphemeris::decode(true, &buf).unwrap(); - - // test mirror - let mut target = [0; 64]; - assert!(eph.encode(true, &mut target).is_err()); - - let mut target = [0; 100]; - let size = eph.encode(true, &mut target).unwrap(); - assert_eq!(size, 98); - assert_eq!(buf, target); - - let eph = SBASEphemeris { - sbas_prn: 10, - toe: 11, - tow: 12, - clock_drift: 0.1, - clock_offset: 0.2, - x_km: 1.4, - vel_x_km: 1.5, - acc_x_km: 1.6, - y_km: 2.4, - vel_y_km: 2.5, - acc_y_km: 2.6, - z_km: 3.1, - vel_z_km: 3.2, - acc_z_km: 3.3, - uint1: 4, - ura: 5, - iodn: 6, - }; - - let mut target = [0; 100]; - eph.encode(true, &mut target).unwrap(); - - let decoded = SBASEphemeris::decode(true, &target).unwrap(); - - assert_eq!(eph, decoded); + for big_endian in [true, false] { + let buf = [0; 100]; + let eph = SBASEphemeris::decode(big_endian, &buf).unwrap(); + + // test mirror + let mut target = [0; 64]; + assert!(eph.encode(big_endian, &mut target).is_err()); + + let mut target = [0; 100]; + let size = eph.encode(big_endian, &mut target).unwrap(); + assert_eq!(size, 98); + assert_eq!(buf, target); + + let eph = SBASEphemeris { + sbas_prn: 10, + toe: 11, + tow: 12, + clock_drift: 0.1, + clock_offset: 0.2, + x_km: 1.4, + vel_x_km: 1.5, + acc_x_km: 1.6, + y_km: 2.4, + vel_y_km: 2.5, + acc_y_km: 2.6, + z_km: 3.1, + vel_z_km: 3.2, + acc_z_km: 3.3, + uint1: 4, + ura: 5, + iodn: 6, + }; + + let mut target = [0; 100]; + eph.encode(big_endian, &mut target).unwrap(); + + let decoded = SBASEphemeris::decode(big_endian, &target).unwrap(); + assert_eq!(eph, decoded); + } } } diff --git a/binex/src/message/record/monument/mod.rs b/binex/src/message/record/monument/mod.rs index 5651b62f..e97b71e5 100644 --- a/binex/src/message/record/monument/mod.rs +++ b/binex/src/message/record/monument/mod.rs @@ -242,7 +242,7 @@ impl MonumentGeoRecord { } // decode timestamp - ret.epoch = decode_gpst_epoch(big_endian, TimeResolution::QuarterSecond, &buf)?; + ret.epoch = decode_gpst_epoch(big_endian, TimeResolution::QuarterSecond, buf)?; // decode source meta ret.meta = MonumentGeoMetadata::from(buf[5]); @@ -557,95 +557,79 @@ mod test { #[test] fn monument_geo_comments_decoding() { let mlen = 17; - let big_endian = true; - - let buf = [ - 0, 0, 1, 1, 41, 2, 0, 9, 'H' as u8, 'e' as u8, 'l' as u8, 'l' as u8, 'o' as u8, - ' ' as u8, 'G' as u8, 'E' as u8, 'O' as u8, - ]; - - let geo = MonumentGeoRecord::decode(mlen, big_endian, &buf).unwrap(); - - assert_eq!( - geo.epoch, - Epoch::from_gpst_seconds(256.0 * 60.0 + 60.0 + 10.25) - ); - - assert_eq!(geo.meta, MonumentGeoMetadata::IGS); - - assert_eq!(geo.comments.len(), 1); - - let comments = geo.comments.get(0).unwrap(); - assert_eq!(comments, "Hello GEO"); - - // ts +meta + FID_1_4 +strlen_1_4 +strlen - assert_eq!(geo.encoding_size(), 5 + 1 + 1 + 1 + 9); - - // test mirror op - let mut encoded = [0; 12]; - assert!(geo.encode(big_endian, &mut encoded).is_err()); - - let mut encoded = [0; 18]; - let size = geo.encode(big_endian, &mut encoded).unwrap(); - assert_eq!(size, 5 + 1 + 1 + 1 + 9); + for big_endian in [true, false] { + let mut geo = MonumentGeoRecord::default(); + geo.epoch = Epoch::from_gpst_seconds(256.0 * 60.0 + 60.0 + 10.25); + geo = geo.with_comment("Hello World"); + geo.meta = MonumentGeoMetadata::IGS; + + let mut encoded = [0; 64]; + geo.encode(big_endian, &mut encoded).unwrap(); + let decoded = MonumentGeoRecord::decode(mlen, big_endian, &encoded).unwrap(); + assert_eq!(decoded, geo); + } } #[test] fn monument_geo_double_comments_decoding() { - let mut geo: MonumentGeoRecord = MonumentGeoRecord::default() - .with_comment("A B C") - .with_comment("D E F"); - - geo.epoch = Epoch::from_gpst_seconds(60.0 + 0.75); - geo.meta = MonumentGeoMetadata::IGS; - - // ts + meta + 2*(FID_1_4 +STR_1_4 +STR) - assert_eq!(geo.encoding_size(), 5 + 1 + 2 * (1 + 1 + 5)); - - let mut buf = [0; 16]; - assert!(geo.encode(true, &mut buf).is_err()); - - let mut buf = [0; 22]; - let size = geo.encode(true, &mut buf).unwrap(); - - // ts + meta + 2*(FID_1_4 +strlen_1_4 +strlen) - assert_eq!(geo.encoding_size(), 5 + 1 + 2 * (1 + 1 + 5)); - assert_eq!(size, 5 + 1 + 2 * (1 + 1 + 5)); - - let mut geo = MonumentGeoRecord::default() - .with_comment("Hello") - .with_climatic_info("Clim"); - - geo.epoch = Epoch::from_gpst_seconds(60.0 + 0.75); - geo.meta = MonumentGeoMetadata::IGS; - - let mut buf = [0; 19]; - let size = geo.encode(true, &mut buf).unwrap(); - assert_eq!(size, 5 + 1 + 1 + 1 + 5 + 1 + 1 + 4); - - assert_eq!( - buf, - [ - 0, - 0, - 0, - 1, - 3, - MonumentGeoMetadata::IGS as u8, - 0, - 5, - 'H' as u8, - 'e' as u8, - 'l' as u8, - 'l' as u8, - 'o' as u8, - 14, - 4, - 'C' as u8, - 'l' as u8, - 'i' as u8, - 'm' as u8, - ] - ); + for big_endian in [true, false] { + let mut geo: MonumentGeoRecord = MonumentGeoRecord::default() + .with_comment("A B C") + .with_comment("D E F"); + + geo.epoch = Epoch::from_gpst_seconds(60.0 + 0.75); + geo.meta = MonumentGeoMetadata::IGS; + + // ts + meta + 2*(FID_1_4 +STR_1_4 +STR) + assert_eq!(geo.encoding_size(), 5 + 1 + 2 * (1 + 1 + 5)); + + let mut buf = [0; 16]; + assert!(geo.encode(big_endian, &mut buf).is_err()); + + let mut buf = [0; 22]; + let size = geo.encode(big_endian, &mut buf).unwrap(); + + // ts + meta + 2*(FID_1_4 +strlen_1_4 +strlen) + assert_eq!(geo.encoding_size(), 5 + 1 + 2 * (1 + 1 + 5)); + assert_eq!(size, 5 + 1 + 2 * (1 + 1 + 5)); + + let mut geo = MonumentGeoRecord::default() + .with_comment("Hello") + .with_climatic_info("Clim"); + + geo.epoch = Epoch::from_gpst_seconds(60.0 + 0.75); + geo.meta = MonumentGeoMetadata::IGS; + + let mut buf = [0; 19]; + let size = geo.encode(big_endian, &mut buf).unwrap(); + assert_eq!(size, 5 + 1 + 1 + 1 + 5 + 1 + 1 + 4); + + if big_endian { + assert_eq!( + buf, + [ + 0, + 0, + 0, + 1, + 3, + MonumentGeoMetadata::IGS as u8, + 0, + 5, + b'H', + b'e', + b'l', + b'l', + b'o', + 14, + 4, + b'C', + b'l', + b'i', + b'm', + ] + ); + } + } } } diff --git a/binex/src/message/record/solutions/frame/ecef.rs b/binex/src/message/record/solutions/frame/ecef.rs index bc2c3ed0..1c448612 100644 --- a/binex/src/message/record/solutions/frame/ecef.rs +++ b/binex/src/message/record/solutions/frame/ecef.rs @@ -85,7 +85,7 @@ impl PositionEcef3d { return Err(Error::NotEnoughBytes); } - let (str_len, mut ptr) = Message::decode_bnxi(&buf, big_endian); + let (str_len, mut ptr) = Message::decode_bnxi(buf, big_endian); let str_len = str_len as usize; let ellipsoid = if str_len == 0 { diff --git a/binex/src/message/record/solutions/frame/geo.rs b/binex/src/message/record/solutions/frame/geo.rs index 19ea833f..217ae6f0 100644 --- a/binex/src/message/record/solutions/frame/geo.rs +++ b/binex/src/message/record/solutions/frame/geo.rs @@ -85,7 +85,7 @@ impl PositionGeo3d { return Err(Error::NotEnoughBytes); } - let (str_len, mut ptr) = Message::decode_bnxi(&buf, big_endian); + let (str_len, mut ptr) = Message::decode_bnxi(buf, big_endian); let str_len = str_len as usize; let ellipsoid = if str_len == 0 { diff --git a/binex/src/message/record/solutions/frame/mod.rs b/binex/src/message/record/solutions/frame/mod.rs index c725686a..186115e7 100644 --- a/binex/src/message/record/solutions/frame/mod.rs +++ b/binex/src/message/record/solutions/frame/mod.rs @@ -154,7 +154,7 @@ impl SolutionsFrame { } // decode FID - let (fid, mut ptr) = Message::decode_bnxi(&buf, big_endian); + let (fid, mut ptr) = Message::decode_bnxi(buf, big_endian); let fid = FieldID::from(fid); match fid { @@ -372,10 +372,7 @@ mod test { let size = frame.encode(big_endian, &mut buf).unwrap(); assert_eq!(size, frame.encoding_size()); - assert_eq!( - buf, - [0, 5, 'H' as u8, 'e' as u8, 'l' as u8, 'l' as u8, 'o' as u8, 0, 0, 0, 0, 0, 0] - ); + assert_eq!(buf, [0, 5, b'H', b'e', b'l', b'l', b'o', 0, 0, 0, 0, 0, 0]); let decoded = SolutionsFrame::decode(big_endian, &buf).unwrap(); assert_eq!(decoded, frame); diff --git a/binex/src/message/record/solutions/frame/temporal.rs b/binex/src/message/record/solutions/frame/temporal.rs index e7e4682d..181123ff 100644 --- a/binex/src/message/record/solutions/frame/temporal.rs +++ b/binex/src/message/record/solutions/frame/temporal.rs @@ -47,7 +47,7 @@ impl TemporalSolution { if buf.len() < 8 { return Err(Error::NotEnoughBytes); } - let offset_s = Utils::decode_f64(big_endian, &buf)?; + let offset_s = Utils::decode_f64(big_endian, buf)?; Ok(Self { offset_s, drift_s_s: None, @@ -58,7 +58,7 @@ impl TemporalSolution { if buf.len() < 16 { return Err(Error::NotEnoughBytes); } - let offset_s = Utils::decode_f64(big_endian, &buf)?; + let offset_s = Utils::decode_f64(big_endian, buf)?; let drift_s_s = Utils::decode_f64(big_endian, &buf[8..])?; Ok(Self { offset_s, diff --git a/binex/src/message/record/solutions/mod.rs b/binex/src/message/record/solutions/mod.rs index b4945cb0..c2ae6245 100644 --- a/binex/src/message/record/solutions/mod.rs +++ b/binex/src/message/record/solutions/mod.rs @@ -28,7 +28,7 @@ pub struct Solutions { impl Iterator for Solutions { type Item = SolutionsFrame; fn next(&mut self) -> Option { - self.frames.iter().next().cloned() + self.frames.first().cloned() } } @@ -105,7 +105,7 @@ impl Solutions { } // decode timestamp - let epoch = decode_gpst_epoch(big_endian, TimeResolution::MilliSecond, &buf)?; + let epoch = decode_gpst_epoch(big_endian, TimeResolution::MilliSecond, buf)?; // parse inner frames (= subrecords) let mut ptr = 6; diff --git a/binex/src/stream.rs b/binex/src/stream.rs index 05e0215f..e1bac134 100644 --- a/binex/src/stream.rs +++ b/binex/src/stream.rs @@ -26,19 +26,19 @@ impl Provider { /// Identify potential closed source [Provider] /// from parsed MID (u32) pub(crate) fn match_any(mid: u32) -> Option { - if mid >= 0x80 && mid <= 0x87 { + if (0x80..=0x87).contains(&mid) { Some(Self::UCAR) - } else if mid >= 0x88 && mid <= 0xa7 { + } else if (0x88..=0xa7).contains(&mid) { Some(Self::Ashtech) - } else if mid >= 0xa8 && mid <= 0xaf { + } else if (0xa8..=0xaf).contains(&mid) { Some(Self::Topcon) - } else if mid >= 0xb0 && mid <= 0xb3 { + } else if (0xb0..=0xb3).contains(&mid) { Some(Self::GPSSolutions) - } else if mid >= 0xb4 && mid <= 0xb7 { + } else if (0xb4..=0xb7).contains(&mid) { Some(Self::NRCan) - } else if mid >= 0xb8 && mid <= 0xbf { + } else if (0xb8..=0xbf).contains(&mid) { Some(Self::JPL) - } else if mid >= 0xc0 && mid <= 0xc3 { + } else if (0xc0..=0xc3).contains(&mid) { Some(Self::ColoradoUnivBoulder) } else { None diff --git a/binex/src/utils.rs b/binex/src/utils.rs index 392c38ca..3d1c448e 100644 --- a/binex/src/utils.rs +++ b/binex/src/utils.rs @@ -8,12 +8,10 @@ impl Utils { pub fn decode_u16(big_endian: bool, buf: &[u8]) -> Result { if buf.len() < 2 { Err(Error::NotEnoughBytes) + } else if big_endian { + Ok(u16::from_be_bytes([buf[0], buf[1]])) } else { - if big_endian { - Ok(u16::from_be_bytes([buf[0], buf[1]])) - } else { - Ok(u16::from_le_bytes([buf[0], buf[1]])) - } + Ok(u16::from_le_bytes([buf[0], buf[1]])) } } /// u32 decoding attempt, as specified by @@ -21,12 +19,10 @@ impl Utils { pub fn decode_u32(big_endian: bool, buf: &[u8]) -> Result { if buf.len() < 4 { Err(Error::NotEnoughBytes) + } else if big_endian { + Ok(u32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]])) } else { - if big_endian { - Ok(u32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]])) - } else { - Ok(u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]])) - } + Ok(u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]])) } } /// i32 decoding attempt, as specified by @@ -34,12 +30,10 @@ impl Utils { pub fn decode_i32(big_endian: bool, buf: &[u8]) -> Result { if buf.len() < 4 { Err(Error::NotEnoughBytes) + } else if big_endian { + Ok(i32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]])) } else { - if big_endian { - Ok(i32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]])) - } else { - Ok(i32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]])) - } + Ok(i32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]])) } } /// f32 decoding attempt, as specified by @@ -47,12 +41,10 @@ impl Utils { pub fn decode_f32(big_endian: bool, buf: &[u8]) -> Result { if buf.len() < 4 { Err(Error::NotEnoughBytes) + } else if big_endian { + Ok(f32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]])) } else { - if big_endian { - Ok(f32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]])) - } else { - Ok(f32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]])) - } + Ok(f32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]])) } } /// f64 decoding attempt, as specified by @@ -60,16 +52,14 @@ impl Utils { pub fn decode_f64(big_endian: bool, buf: &[u8]) -> Result { if buf.len() < 8 { Err(Error::NotEnoughBytes) + } else if big_endian { + Ok(f64::from_be_bytes([ + buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], + ])) } else { - if big_endian { - Ok(f64::from_be_bytes([ - buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], - ])) - } else { - Ok(f64::from_le_bytes([ - buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], - ])) - } + Ok(f64::from_le_bytes([ + buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], + ])) } } }