refactor(storage-server): add SectorId, FileName, PackedPath newtypes, eliminate raw primitives and DRY violations

This commit is contained in:
2026-10-18 03:41:14 +02:00
parent 7a4600b3a2
commit 2dc503e07e
8 changed files with 162 additions and 111 deletions

View File

@@ -1,15 +1,50 @@
use server_framework::{Decode, Encode};
use crate::filesystem::FileHandle;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PackedPath {
word0: usize,
word1: usize,
}
impl PackedPath {
pub fn from_bytes(src: &[u8]) -> Self {
let mut w0 = [0u8; 8];
let mut w1 = [0u8; 8];
for (i, &b) in src.iter().enumerate() {
if i < 8 { w0[i] = b; }
else if i < 16 { w1[i - 8] = b; }
}
Self {
word0: usize::from_le_bytes(w0),
word1: usize::from_le_bytes(w1),
}
}
pub fn from_words(word0: usize, word1: usize) -> Self {
Self { word0, word1 }
}
pub fn to_bytes(&self) -> [u8; 16] {
let mut buf = [0u8; 16];
buf[..8].copy_from_slice(&self.word0.to_le_bytes());
buf[8..16].copy_from_slice(&self.word1.to_le_bytes());
buf
}
pub fn word0(&self) -> usize { self.word0 }
pub fn word1(&self) -> usize { self.word1 }
}
#[derive(Debug, PartialEq)]
pub enum StorageRequest {
Open { path_word0: usize, path_word1: usize },
Open { path: PackedPath },
Read { handle: FileHandle, max_bytes: usize },
Write { handle: FileHandle, data_word: usize },
Close { handle: FileHandle },
ListDir { path_word0: usize, path_word1: usize },
Create { path_word0: usize, path_word1: usize },
Delete { path_word0: usize, path_word1: usize },
ListDir { path: PackedPath },
Create { path: PackedPath },
Delete { path: PackedPath },
FileSize { handle: FileHandle },
}
@@ -28,13 +63,13 @@ pub enum StorageResponse {
impl Encode for StorageRequest {
fn encode(&self) -> [usize; 4] {
match self {
StorageRequest::Open { path_word0, path_word1 } => [0, *path_word0, *path_word1, 0],
StorageRequest::Open { path } => [0, path.word0(), path.word1(), 0],
StorageRequest::Read { handle, max_bytes } => [1, handle.raw() as usize, *max_bytes, 0],
StorageRequest::Write { handle, data_word } => [2, handle.raw() as usize, *data_word, 0],
StorageRequest::Close { handle } => [3, handle.raw() as usize, 0, 0],
StorageRequest::ListDir { path_word0, path_word1 } => [4, *path_word0, *path_word1, 0],
StorageRequest::Create { path_word0, path_word1 } => [5, *path_word0, *path_word1, 0],
StorageRequest::Delete { path_word0, path_word1 } => [6, *path_word0, *path_word1, 0],
StorageRequest::ListDir { path } => [4, path.word0(), path.word1(), 0],
StorageRequest::Create { path } => [5, path.word0(), path.word1(), 0],
StorageRequest::Delete { path } => [6, path.word0(), path.word1(), 0],
StorageRequest::FileSize { handle } => [7, handle.raw() as usize, 0, 0],
}
}
@@ -43,13 +78,13 @@ impl Encode for StorageRequest {
impl Decode for StorageRequest {
fn decode(words: [usize; 4]) -> Option<Self> {
match words[0] {
0 => Some(StorageRequest::Open { path_word0: words[1], path_word1: words[2] }),
0 => Some(StorageRequest::Open { path: PackedPath::from_words(words[1], words[2]) }),
1 => Some(StorageRequest::Read { handle: FileHandle::new(words[1] as u16), max_bytes: words[2] }),
2 => Some(StorageRequest::Write { handle: FileHandle::new(words[1] as u16), data_word: words[2] }),
3 => Some(StorageRequest::Close { handle: FileHandle::new(words[1] as u16) }),
4 => Some(StorageRequest::ListDir { path_word0: words[1], path_word1: words[2] }),
5 => Some(StorageRequest::Create { path_word0: words[1], path_word1: words[2] }),
6 => Some(StorageRequest::Delete { path_word0: words[1], path_word1: words[2] }),
4 => Some(StorageRequest::ListDir { path: PackedPath::from_words(words[1], words[2]) }),
5 => Some(StorageRequest::Create { path: PackedPath::from_words(words[1], words[2]) }),
6 => Some(StorageRequest::Delete { path: PackedPath::from_words(words[1], words[2]) }),
7 => Some(StorageRequest::FileSize { handle: FileHandle::new(words[1] as u16) }),
_ => None,
}