Skip to content

Commit

Permalink
Added Default for AWDLTLV.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frostie314159 committed Jan 6, 2024
1 parent d957ca7 commit 70e0c01
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "awdl-frame-parser"
version = "0.3.2"
version = "0.3.3"
edition = "2021"
description = "A parser for AWDL data and action frames."
authors = ["Frostie314159"]
Expand Down
4 changes: 2 additions & 2 deletions src/action_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::fmt::Debug;
use core::{fmt::Debug, iter::repeat};

use crate::{
common::LabelIterator,
common::ReadLabelIterator,
tlvs::{dns_sd::ReadValueIterator, sync_elect::ReadMACIterator, AWDLTLV},
};

Expand Down Expand Up @@ -49,7 +49,7 @@ impl<'a> AWDLActionFrame<'a> {
pub fn get_named_tlvs(
&'a self,
) -> impl Iterator<
Item = AWDLTLV<'a, ReadMACIterator<'a>, LabelIterator<'a>, ReadValueIterator<'a>>,
Item = AWDLTLV<'a, ReadMACIterator<'a>, ReadLabelIterator<'a>, ReadValueIterator<'a>>,
> + Clone {
repeat(()).scan(0, |offset, _| self.tagged_data.gread(offset).ok())
}
Expand Down
16 changes: 8 additions & 8 deletions src/common/awdl_dns_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ use crate::tlvs::RawAWDLTLV;
use super::{awdl_dns_compression::AWDLDnsCompression, awdl_str::AWDLStr};

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct LabelIterator<'a> {
pub struct ReadLabelIterator<'a> {
bytes: &'a [u8],
offset: usize,
}
impl<'a> LabelIterator<'a> {
impl<'a> ReadLabelIterator<'a> {
pub const fn new(bytes: &'a [u8]) -> Self {
Self { bytes, offset: 0 }
}
}
impl MeasureWith<()> for LabelIterator<'_> {
impl MeasureWith<()> for ReadLabelIterator<'_> {
fn measure_with(&self, _ctx: &()) -> usize {
self.bytes.len()
}
}
impl<'a> Iterator for LabelIterator<'a> {
impl<'a> Iterator for ReadLabelIterator<'a> {
type Item = AWDLStr<'a>;
fn next(&mut self) -> Option<Self::Item> {
self.bytes.gread(&mut self.offset).ok()
}
}
impl ExactSizeIterator for LabelIterator<'_> {
impl ExactSizeIterator for ReadLabelIterator<'_> {
fn len(&self) -> usize {
repeat(())
.scan(0usize, |offset, _| {
Expand Down Expand Up @@ -88,7 +88,7 @@ where
+ 2
}
}
impl<'a> TryFromCtx<'a> for AWDLDnsName<LabelIterator<'a>> {
impl<'a> TryFromCtx<'a> for AWDLDnsName<ReadLabelIterator<'a>> {
type Error = scroll::Error;
fn try_from_ctx(from: &'a [u8], _ctx: ()) -> Result<(Self, usize), Self::Error> {
let mut offset = 0;
Expand All @@ -97,7 +97,7 @@ impl<'a> TryFromCtx<'a> for AWDLDnsName<LabelIterator<'a>> {
AWDLDnsCompression::from_representation(from.gread_with(&mut offset, NETWORK)?);
Ok((
Self {
labels: LabelIterator::new(label_bytes),
labels: ReadLabelIterator::new(label_bytes),
domain,
},
offset,
Expand Down Expand Up @@ -137,7 +137,7 @@ fn test_dns_name() {
0x04, b'a', b'w', b'd', b'l', 0x04, b'a', b'w', b'd', b'l', 0xc0, 0x0c,
]
.as_slice();
let dns_name = bytes.pread::<AWDLDnsName<LabelIterator>>(0).unwrap();
let dns_name = bytes.pread::<AWDLDnsName<ReadLabelIterator>>(0).unwrap();
assert_eq!(
dns_name,
AWDLDnsName {
Expand Down
2 changes: 1 addition & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ mod awdl_str;
mod awdl_version;

pub use awdl_dns_compression::AWDLDnsCompression;
pub use awdl_dns_name::{AWDLDnsName, LabelIterator};
pub use awdl_dns_name::{AWDLDnsName, ReadLabelIterator};
pub use awdl_str::AWDLStr;
pub use awdl_version::AWDLVersion;
8 changes: 4 additions & 4 deletions src/tlvs/dns_sd/arpa_tlv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use scroll::{
Pread, Pwrite,
};

use crate::common::{AWDLDnsName, AWDLStr, LabelIterator};
use crate::common::{AWDLDnsName, AWDLStr, ReadLabelIterator};

#[derive(Clone, Debug, Default, PartialEq, Eq)]
/// A TLV containing the hostname of the peer. Used for reverse DNS.
Expand All @@ -24,7 +24,7 @@ where
self.arpa.measure_with(ctx) + 1
}
}
impl<'a> TryFromCtx<'a> for ArpaTLV<'a, LabelIterator<'a>> {
impl<'a> TryFromCtx<'a> for ArpaTLV<'a, ReadLabelIterator<'a>> {
type Error = scroll::Error;
fn try_from_ctx(from: &'a [u8], _ctx: ()) -> Result<(Self, usize), Self::Error> {
let mut offset = 0;
Expand Down Expand Up @@ -57,15 +57,15 @@ fn test_arpa_tlv() {

let bytes = &include_bytes!("../../../test_bins/arpa_tlv.bin")[3..];

let arpa_tlv = bytes.pread::<ArpaTLV<LabelIterator>>(0).unwrap();
let arpa_tlv = bytes.pread::<ArpaTLV<ReadLabelIterator>>(0).unwrap();
assert_eq!(arpa_tlv.arpa.domain, AWDLDnsCompression::Local);
assert_eq!(
arpa_tlv.arpa.labels.collect::<Vec<_>>(),
vec!["simon-framework".into()]
);
let mut buf = vec![0x00; arpa_tlv.measure_with(&())];
buf.as_mut_slice()
.pwrite::<ArpaTLV<LabelIterator>>(arpa_tlv, 0)
.pwrite::<ArpaTLV<ReadLabelIterator>>(arpa_tlv, 0)
.unwrap();
assert_eq!(buf, bytes);
}
6 changes: 3 additions & 3 deletions src/tlvs/dns_sd/service_response_tlv/dns_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use scroll::{
Pread, Pwrite, NETWORK,
};

use crate::common::{AWDLDnsName, AWDLStr, LabelIterator};
use crate::common::{AWDLDnsName, AWDLStr, ReadLabelIterator};

serializable_enum! {
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -136,7 +136,7 @@ where
}) + 1
}
}
impl<'a> TryFromCtx<'a> for AWDLDnsRecord<'a, LabelIterator<'a>> {
impl<'a> TryFromCtx<'a> for AWDLDnsRecord<'a, ReadLabelIterator<'a>> {
type Error = scroll::Error;
fn try_from_ctx(from: &'a [u8], _ctx: ()) -> Result<(Self, usize), Self::Error> {
let mut offset = 0;
Expand All @@ -148,7 +148,7 @@ impl<'a> TryFromCtx<'a> for AWDLDnsRecord<'a, LabelIterator<'a>> {
domain_name: from.gread(&mut offset)?,
},
AWDLDnsRecordType::TXT => Self::TXT {
txt_record: LabelIterator::new(&from[offset..]),
txt_record: ReadLabelIterator::new(&from[offset..]),
},
AWDLDnsRecordType::SRV => Self::SRV {
priority: from.gread_with(&mut offset, NETWORK)?,
Expand Down
12 changes: 6 additions & 6 deletions src/tlvs/dns_sd/service_response_tlv/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod dns_record;

use crate::common::{AWDLDnsName, AWDLStr, LabelIterator};
use crate::common::{AWDLDnsName, AWDLStr, ReadLabelIterator};

use dns_record::AWDLDnsRecord;

Expand Down Expand Up @@ -43,7 +43,7 @@ where
6 + self.name.measure_with(ctx) + self.record.measure_with(ctx)
}
}
impl<'a> TryFromCtx<'a> for ServiceResponseTLV<'a, LabelIterator<'a>> {
impl<'a> TryFromCtx<'a> for ServiceResponseTLV<'a, ReadLabelIterator<'a>> {
type Error = scroll::Error;
fn try_from_ctx(from: &'a [u8], _ctx: ()) -> Result<(Self, usize), Self::Error> {
let mut offset = 0;
Expand Down Expand Up @@ -81,15 +81,15 @@ mod service_response_tests {
use scroll::{ctx::MeasureWith, Pread, Pwrite};

use crate::{
common::{AWDLDnsCompression, AWDLDnsName, LabelIterator},
common::{AWDLDnsCompression, AWDLDnsName, ReadLabelIterator},
tlvs::dns_sd::{dns_record::AWDLDnsRecord, ServiceResponseTLV},
};

#[test]
fn test_service_response_tlv_ptr() {
let bytes = &include_bytes!("../../../../test_bins/service_response_tlv_ptr.bin")[3..];

let service_response_tlv = bytes.pread::<ServiceResponseTLV<LabelIterator>>(0).unwrap();
let service_response_tlv = bytes.pread::<ServiceResponseTLV<ReadLabelIterator>>(0).unwrap();

assert_eq!(
service_response_tlv,
Expand All @@ -116,7 +116,7 @@ mod service_response_tests {
fn test_service_response_tlv_srv() {
let bytes = &include_bytes!("../../../../test_bins/service_response_tlv_srv.bin")[3..];

let service_response_tlv = bytes.pread::<ServiceResponseTLV<LabelIterator>>(0).unwrap();
let service_response_tlv = bytes.pread::<ServiceResponseTLV<ReadLabelIterator>>(0).unwrap();

assert_eq!(
service_response_tlv,
Expand Down Expand Up @@ -146,7 +146,7 @@ mod service_response_tests {
fn test_service_response_tlv_txt() {
let bytes = &include_bytes!("../../../../test_bins/service_response_tlv_txt.bin")[3..];

let service_response_tlv = bytes.pread::<ServiceResponseTLV<LabelIterator>>(0).unwrap();
let service_response_tlv = bytes.pread::<ServiceResponseTLV<ReadLabelIterator>>(0).unwrap();

assert_eq!(
service_response_tlv,
Expand Down
7 changes: 5 additions & 2 deletions src/tlvs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use scroll::{
};
use tlv_rs::{raw_tlv::RawTLV, TLV};

use crate::common::{AWDLStr, LabelIterator};
use crate::common::{AWDLStr, ReadLabelIterator};

use self::{
data_path::{DataPathStateTLV, HTCapabilitiesTLV, IEEE80211ContainerTLV},
Expand Down Expand Up @@ -125,7 +125,7 @@ where
}
}
impl<'a> TryFromCtx<'a>
for AWDLTLV<'a, ReadMACIterator<'a>, LabelIterator<'a>, ReadValueIterator<'a>>
for AWDLTLV<'a, ReadMACIterator<'a>, ReadLabelIterator<'a>, ReadValueIterator<'a>>
{
type Error = scroll::Error;
fn try_from_ctx(from: &'a [u8], _ctx: ()) -> Result<(Self, usize), Self::Error> {
Expand Down Expand Up @@ -294,3 +294,6 @@ where
}
}
}

/// Default [AWDLTLV] returned by reading.
pub type DefaultAWDLTLV<'a> = AWDLTLV<'a, ReadMACIterator<'a>, ReadLabelIterator<'a>, ReadValueIterator<'a>>;

0 comments on commit 70e0c01

Please sign in to comment.