sftp-protocol 0.1.0

A pure Rust implementation of the SFTP protocol
Documentation
use super::kind::PacketType;
use super::PayloadTrait;

#[derive(Debug, Eq, PartialEq, Nom, Serialize)]
#[nom(BigEndian)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
pub struct Read {
	pub id: u32,
	#[nom(Parse(crate::util::parse_uuid))]
	#[serde(serialize_with = "crate::util::serialize_uuid")]
	#[cfg_attr(test, strategy(crate::util::arbitrary_uuid()))]
	pub handle: uuid::Uuid,
	pub offset: u64,
	pub len: u32,
}

impl PayloadTrait for Read {
	const Type: PacketType = PacketType::Read;
	fn binsize(&self) -> u32 {
		4 + (4 + 36) + 8 + 4
	}
}

impl From<Read> for super::Payload {
	fn from(p: Read) -> Self {
		Self::Read(p)
	}
}

#[cfg(test)]
mod tests {
	use test_strategy::proptest;
	use crate::parser::encode;
	use crate::parser::Parser;
	use super::*;

	#[proptest]
	fn roundtrip_whole(input: Read) {
		let mut stream = Parser::default();
		let packet = input.into_packet();
		stream.write(&encode(&packet)).unwrap();
		assert_eq!(stream.get_packet(), Ok(Some(packet)));
		assert_eq!(stream.get_packet(), Ok(None));
	}
}