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 Status {
	pub id: u32,
	pub status: StatusType,
	// TODO:  Do we need to do anything special for ISO-10646 UTF-8 encoding here?
	#[nom(Parse(crate::util::parse_string))]
	#[serde(serialize_with = "crate::util::str_with_u32_length")]
	pub message: String,
	#[nom(Parse(crate::util::parse_string))]
	#[serde(serialize_with = "crate::util::str_with_u32_length")]
	pub language: String
}

impl PayloadTrait for Status {
	const Type: PacketType = PacketType::Status;
	fn binsize(&self) -> u32 {
		4 + 4 + (4 + self.message.len() as u32) + (4 + self.language.len() as u32)
	}
}

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

#[derive(Clone, Copy, Debug, Eq, PartialEq, Nom, Serialize_repr)]
#[repr(u32)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
pub enum StatusType {
	OK = 0,
	EOF = 1,
	NoSuchFile = 2,
	PermissionDenied = 3,
	Failure = 4,
	BadMessage = 5,
	NoConnection = 6,
	ConnectionLost = 7,
	OpUnsupported = 8
}

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

	#[proptest]
	fn roundtrip_whole(input: Status) {
		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));
	}
}