cat_dev/fsemul/pcfs/sata/proto/
rewind_folder.rs

1//! Definitions for the `RewindDirectory` packet type, and it's response types.
2//!
3//! This will return the directory iterator to the very beginning of the
4//! directory regardless of where it is.
5
6use crate::errors::NetworkParseError;
7use bytes::{Buf, BufMut, Bytes, BytesMut};
8use valuable::{Fields, NamedField, NamedValues, StructDef, Structable, Valuable, Value, Visit};
9
10/// A packet to rewind to the beginning of a directory.
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct SataRewindFolderPacketBody {
13	file_descriptor: i32,
14}
15
16impl SataRewindFolderPacketBody {
17	/// Create a new rewind directory packet.
18	#[must_use]
19	pub const fn new(file_descriptor: i32) -> Self {
20		Self { file_descriptor }
21	}
22
23	#[must_use]
24	pub const fn file_descriptor(&self) -> i32 {
25		self.file_descriptor
26	}
27
28	pub const fn set_file_descriptor(&mut self, new_fd: i32) {
29		self.file_descriptor = new_fd;
30	}
31}
32
33impl From<&SataRewindFolderPacketBody> for Bytes {
34	fn from(value: &SataRewindFolderPacketBody) -> Self {
35		let mut buff = BytesMut::with_capacity(4);
36		buff.put_i32(value.file_descriptor);
37		buff.freeze()
38	}
39}
40
41impl From<SataRewindFolderPacketBody> for Bytes {
42	fn from(value: SataRewindFolderPacketBody) -> Self {
43		Self::from(&value)
44	}
45}
46
47impl TryFrom<Bytes> for SataRewindFolderPacketBody {
48	type Error = NetworkParseError;
49
50	fn try_from(mut value: Bytes) -> Result<Self, Self::Error> {
51		if value.len() < 0x4 {
52			return Err(NetworkParseError::FieldNotLongEnough(
53				"SataRewindDir",
54				"Body",
55				0x4,
56				value.len(),
57				value,
58			));
59		}
60		if value.len() > 0x4 {
61			return Err(NetworkParseError::UnexpectedTrailer(
62				"SataRewindDir",
63				value.slice(0x4..),
64			));
65		}
66
67		let fd = value.get_i32();
68
69		Ok(Self {
70			file_descriptor: fd,
71		})
72	}
73}
74
75const SATA_REWIND_FOLDER_PACKET_BODY_FIELDS: &[NamedField<'static>] = &[NamedField::new("fd")];
76
77impl Structable for SataRewindFolderPacketBody {
78	fn definition(&self) -> StructDef<'_> {
79		StructDef::new_static(
80			"SataRewindDirPacketBody",
81			Fields::Named(SATA_REWIND_FOLDER_PACKET_BODY_FIELDS),
82		)
83	}
84}
85
86impl Valuable for SataRewindFolderPacketBody {
87	fn as_value(&self) -> Value<'_> {
88		Value::Structable(self)
89	}
90
91	fn visit(&self, visitor: &mut dyn Visit) {
92		visitor.visit_named_fields(&NamedValues::new(
93			SATA_REWIND_FOLDER_PACKET_BODY_FIELDS,
94			&[Valuable::as_value(&self.file_descriptor)],
95		));
96	}
97}