refactor(storage-server): add SectorId, FileName, PackedPath newtypes, eliminate raw primitives and DRY violations
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use server_framework::Server;
|
||||
|
||||
use crate::filesystem::{FileHandle, FileSystem};
|
||||
use crate::filesystem::{FileSystem, DirEntry};
|
||||
use crate::protocol::{StorageRequest, StorageResponse};
|
||||
|
||||
pub struct StorageServer<F: FileSystem> {
|
||||
@@ -15,19 +15,6 @@ impl<F: FileSystem> StorageServer<F> {
|
||||
pub fn filesystem(&self) -> &F {
|
||||
&self.fs
|
||||
}
|
||||
|
||||
fn path_from_words(w0: usize, w1: usize) -> [u8; 16] {
|
||||
let mut path = [0u8; 16];
|
||||
let b0 = w0.to_le_bytes();
|
||||
let b1 = w1.to_le_bytes();
|
||||
path[..8].copy_from_slice(&b0);
|
||||
path[8..16].copy_from_slice(&b1);
|
||||
path
|
||||
}
|
||||
|
||||
fn path_len(path: &[u8]) -> usize {
|
||||
path.iter().position(|&b| b == 0).unwrap_or(path.len())
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: FileSystem> Server for StorageServer<F> {
|
||||
@@ -36,10 +23,10 @@ impl<F: FileSystem> Server for StorageServer<F> {
|
||||
|
||||
fn handle(&mut self, request: StorageRequest) -> StorageResponse {
|
||||
match request {
|
||||
StorageRequest::Open { path_word0, path_word1 } => {
|
||||
let path = Self::path_from_words(path_word0, path_word1);
|
||||
let len = Self::path_len(&path);
|
||||
match self.fs.open(&path[..len]) {
|
||||
StorageRequest::Open { path } => {
|
||||
let buf = path.to_bytes();
|
||||
let len = buf.iter().position(|&b| b == 0).unwrap_or(16);
|
||||
match self.fs.open(&buf[..len]) {
|
||||
Some(h) => StorageResponse::Handle(h),
|
||||
None => StorageResponse::NotFound,
|
||||
}
|
||||
@@ -63,27 +50,27 @@ impl<F: FileSystem> Server for StorageServer<F> {
|
||||
self.fs.close(handle);
|
||||
StorageResponse::Ok
|
||||
}
|
||||
StorageRequest::ListDir { path_word0, path_word1 } => {
|
||||
let path = Self::path_from_words(path_word0, path_word1);
|
||||
let len = Self::path_len(&path);
|
||||
let mut entries = [crate::filesystem::DirEntry::empty(); 16];
|
||||
match self.fs.list_dir(&path[..len], &mut entries) {
|
||||
StorageRequest::ListDir { path } => {
|
||||
let buf = path.to_bytes();
|
||||
let len = buf.iter().position(|&b| b == 0).unwrap_or(16);
|
||||
let mut entries = [DirEntry::empty(); 16];
|
||||
match self.fs.list_dir(&buf[..len], &mut entries) {
|
||||
Some(count) => StorageResponse::DirEntries { count },
|
||||
None => StorageResponse::NotFound,
|
||||
}
|
||||
}
|
||||
StorageRequest::Create { path_word0, path_word1 } => {
|
||||
let path = Self::path_from_words(path_word0, path_word1);
|
||||
let len = Self::path_len(&path);
|
||||
match self.fs.create(&path[..len]) {
|
||||
StorageRequest::Create { path } => {
|
||||
let buf = path.to_bytes();
|
||||
let len = buf.iter().position(|&b| b == 0).unwrap_or(16);
|
||||
match self.fs.create(&buf[..len]) {
|
||||
Some(h) => StorageResponse::Handle(h),
|
||||
None => StorageResponse::Error,
|
||||
}
|
||||
}
|
||||
StorageRequest::Delete { path_word0, path_word1 } => {
|
||||
let path = Self::path_from_words(path_word0, path_word1);
|
||||
let len = Self::path_len(&path);
|
||||
if self.fs.delete(&path[..len]) {
|
||||
StorageRequest::Delete { path } => {
|
||||
let buf = path.to_bytes();
|
||||
let len = buf.iter().position(|&b| b == 0).unwrap_or(16);
|
||||
if self.fs.delete(&buf[..len]) {
|
||||
StorageResponse::Ok
|
||||
} else {
|
||||
StorageResponse::NotFound
|
||||
|
||||
Reference in New Issue
Block a user