sftp-protocol 0.1.0

A pure Rust implementation of the SFTP protocol
Documentation
use camino::Utf8PathBuf;

use crate::common::FileAttributes;

use super::kind::PacketType;
use super::PayloadTrait;

#[derive(Debug, Eq, PartialEq, Nom, Serialize)]
#[nom(BigEndian)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
pub struct MkDir {
	pub id: u32,
	#[nom(Parse(crate::util::parse_path))]
	#[serde(serialize_with = "crate::util::path_with_u32_length")]
	pub path: Utf8PathBuf,
	pub attrs: FileAttributes
}

impl PayloadTrait for MkDir {
	const Type: PacketType = PacketType::MkDir;
	fn binsize(&self) -> u32 {
		4 + (4 + self.path.as_str().len() as u32) + self.attrs.binsize()
	}
}

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

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

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