use crate::{Buf, BufError, BufMut, BufResult, Codec, Cursor};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Ipv4Option<'a> {
EndOfOptionList(EndOfOptionList),
NoOperation(NoOperation),
Security(Security<'a>),
LooseSourceRoute(LooseSourceRoute<'a>),
StrictSourceRoute(StrictSourceRoute<'a>),
RecordRoute(RecordRoute<'a>),
StreamId(StreamId),
InternetTimestamp(InternetTimestamp<'a>),
RouterAlert(RouterAlert),
}
impl<'a> Codec for Ipv4Option<'a> {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
match self {
Ipv4Option::EndOfOptionList(opt) => opt.encode(writer, ()),
Ipv4Option::NoOperation(opt) => opt.encode(writer, ()),
Ipv4Option::Security(opt) => opt.encode(writer, ()),
Ipv4Option::LooseSourceRoute(opt) => opt.encode(writer, ()),
Ipv4Option::StrictSourceRoute(opt) => opt.encode(writer, ()),
Ipv4Option::RecordRoute(opt) => opt.encode(writer, ()),
Ipv4Option::StreamId(opt) => opt.encode(writer, ()),
Ipv4Option::InternetTimestamp(opt) => opt.encode(writer, ()),
Ipv4Option::RouterAlert(opt) => opt.encode(writer, ()),
}
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
let typ_peek = reader.peek_u8()?;
match typ_peek {
0 => Ok(Self::EndOfOptionList(EndOfOptionList::decode(reader, ())?)),
1 => Ok(Self::NoOperation(NoOperation::decode(reader, ())?)),
7 => Ok(Self::RecordRoute(RecordRoute::decode(reader, ())?)),
68 => Ok(Self::InternetTimestamp(InternetTimestamp::decode(
reader,
(),
)?)),
130 => Ok(Self::Security(Security::decode(reader, ())?)),
131 => Ok(Self::LooseSourceRoute(LooseSourceRoute::decode(
reader,
(),
)?)),
136 => Ok(Self::StreamId(StreamId::decode(reader, ())?)),
137 => Ok(Self::StrictSourceRoute(StrictSourceRoute::decode(
reader,
(),
)?)),
148 => Ok(Self::RouterAlert(RouterAlert::decode(reader, ())?)),
_ => Err(BufError::UnexpectedValue),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum Type {
EndOfOptionList = 0,
NoOperation = 1,
RecordRoute = 7,
InternetTimestamp = 68,
Security = 130,
LooseSourceRoute = 131,
StreamId = 136,
StrictSourceRoute = 137,
RouterAlert = 148,
}
impl Codec for Type {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
(*self as u8).encode(writer, ())
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
match u8::decode(reader, ())? {
x if x == Self::EndOfOptionList as u8 => Ok(Self::EndOfOptionList),
x if x == Self::NoOperation as u8 => Ok(Self::NoOperation),
x if x == Self::RecordRoute as u8 => Ok(Self::RecordRoute),
x if x == Self::InternetTimestamp as u8 => Ok(Self::InternetTimestamp),
x if x == Self::Security as u8 => Ok(Self::Security),
x if x == Self::LooseSourceRoute as u8 => Ok(Self::LooseSourceRoute),
x if x == Self::StreamId as u8 => Ok(Self::StreamId),
x if x == Self::StrictSourceRoute as u8 => Ok(Self::StrictSourceRoute),
x if x == Self::RouterAlert as u8 => Ok(Self::RouterAlert),
_ => Err(BufError::UnexpectedValue),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EndOfOptionList;
impl EndOfOptionList {
pub const TYPE: Type = Type::EndOfOptionList;
}
impl Codec for EndOfOptionList {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
Self::TYPE.encode(writer, ())
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
if Type::decode(reader, ())? != Self::TYPE {
return Err(BufError::UnexpectedValue);
}
Ok(Self)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NoOperation;
impl NoOperation {
pub const TYPE: Type = Type::NoOperation;
}
impl Codec for NoOperation {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
Self::TYPE.encode(writer, ())
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
if Type::decode(reader, ())? != Self::TYPE {
return Err(BufError::UnexpectedValue);
}
Ok(Self)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RouterAlert {
pub value: u16,
}
impl RouterAlert {
pub const TYPE: Type = Type::RouterAlert;
}
impl Codec for RouterAlert {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
Self::TYPE.encode(writer, ())?;
4u8.encode(writer, ())?; self.value.encode(writer, ())
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
if Type::decode(reader, ())? != Self::TYPE {
return Err(BufError::UnexpectedValue);
}
let _length = u8::decode(reader, ())?;
Ok(Self {
value: u16::decode(reader, ())?,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RecordRoute<'a> {
pub pointer: u8,
pub route_data: &'a [u8],
}
impl<'a> RecordRoute<'a> {
pub const TYPE: Type = Type::RecordRoute;
fn encoded_len(&self) -> usize {
3 + self.route_data.len() }
}
impl<'a> Codec for RecordRoute<'a> {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
Self::TYPE.encode(writer, ())?;
(self.encoded_len() as u8).encode(writer, ())?;
self.pointer.encode(writer, ())?;
todo!("Encode variable length slice: self.route_data");
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
if Type::decode(reader, ())? != Self::TYPE {
return Err(BufError::UnexpectedValue);
}
let length = u8::decode(reader, ())?;
let _pointer = u8::decode(reader, ())?;
let _data_len = length.saturating_sub(3);
todo!("Decode variable length slice of length: _data_len into route_data");
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LooseSourceRoute<'a> {
pub pointer: u8,
pub route_data: &'a [u8],
}
impl<'a> LooseSourceRoute<'a> {
pub const TYPE: Type = Type::LooseSourceRoute;
fn encoded_len(&self) -> usize {
3 + self.route_data.len()
}
}
impl<'a> Codec for LooseSourceRoute<'a> {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
Self::TYPE.encode(writer, ())?;
(self.encoded_len() as u8).encode(writer, ())?;
self.pointer.encode(writer, ())?;
todo!("Encode variable length slice: self.route_data");
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
if Type::decode(reader, ())? != Self::TYPE {
return Err(BufError::UnexpectedValue);
}
let length = u8::decode(reader, ())?;
let _pointer = u8::decode(reader, ())?;
let _data_len = length.saturating_sub(3);
todo!("Decode variable length slice of length: _data_len into route_data");
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StrictSourceRoute<'a> {
pub pointer: u8,
pub route_data: &'a [u8],
}
impl<'a> StrictSourceRoute<'a> {
pub const TYPE: Type = Type::StrictSourceRoute;
fn encoded_len(&self) -> usize {
3 + self.route_data.len()
}
}
impl<'a> Codec for StrictSourceRoute<'a> {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
Self::TYPE.encode(writer, ())?;
(self.encoded_len() as u8).encode(writer, ())?;
self.pointer.encode(writer, ())?;
todo!("Encode variable length slice: self.route_data");
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
if Type::decode(reader, ())? != Self::TYPE {
return Err(BufError::UnexpectedValue);
}
let length = u8::decode(reader, ())?;
let _pointer = u8::decode(reader, ())?;
let _data_len = length.saturating_sub(3);
todo!("Decode variable length slice of length: _data_len into route_data");
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct InternetTimestamp<'a> {
pub pointer: u8,
pub overflow_and_flags: u8,
pub data: &'a [u8],
}
impl<'a> InternetTimestamp<'a> {
pub const TYPE: Type = Type::InternetTimestamp;
fn encoded_len(&self) -> usize {
4 + self.data.len() }
}
impl<'a> Codec for InternetTimestamp<'a> {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
Self::TYPE.encode(writer, ())?;
(self.encoded_len() as u8).encode(writer, ())?;
self.pointer.encode(writer, ())?;
self.overflow_and_flags.encode(writer, ())?;
todo!("Encode variable length slice: self.data");
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
if Type::decode(reader, ())? != Self::TYPE {
return Err(BufError::UnexpectedValue);
}
let length = u8::decode(reader, ())?;
let _pointer = u8::decode(reader, ())?;
let _overflow_and_flags = u8::decode(reader, ())?;
let _data_len = length.saturating_sub(4);
todo!("Decode variable length slice of length: _data_len into data");
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StreamId {
pub id: u16,
}
impl StreamId {
pub const TYPE: Type = Type::StreamId;
}
impl Codec for StreamId {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
Self::TYPE.encode(writer, ())?;
4u8.encode(writer, ())?; self.id.encode(writer, ())
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
if Type::decode(reader, ())? != Self::TYPE {
return Err(BufError::UnexpectedValue);
}
let _length = u8::decode(reader, ())?;
Ok(Self {
id: u16::decode(reader, ())?,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Security<'a> {
pub data: &'a [u8],
}
impl<'a> Security<'a> {
pub const TYPE: Type = Type::Security;
fn encoded_len(&self) -> usize {
2 + self.data.len()
}
}
impl<'a> Codec for Security<'a> {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
Self::TYPE.encode(writer, ())?;
(self.encoded_len() as u8).encode(writer, ())?;
todo!("Encode variable length slice: self.data");
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
if Type::decode(reader, ())? != Self::TYPE {
return Err(BufError::UnexpectedValue);
}
let length = u8::decode(reader, ())?;
let _data_len = length.saturating_sub(2);
todo!("Decode variable length slice of length: _data_len into data");
}
}
#[cfg(test)]
mod tests {
#[test]
fn test() {}
}