From dc4ca79834a56b1e79a85d202a6778ecf7929018 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 18 Jan 2023 09:06:16 +0800 Subject: [PATCH] services: pddb: use an enum for file types Rather than hardcoding numbers, use an enum with the constant values. This will make it easier to reason about what the constants mean. Signed-off-by: Sean Cross --- services/pddb/src/libstd/mod.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/services/pddb/src/libstd/mod.rs b/services/pddb/src/libstd/mod.rs index 0cb709c3d..db4c3db9a 100644 --- a/services/pddb/src/libstd/mod.rs +++ b/services/pddb/src/libstd/mod.rs @@ -7,6 +7,16 @@ use crate::FileHandle; use senres::{Senres, SenresMut}; +#[repr(u8)] +enum FileType { + Basis = 0, + Dict = 1, + Key = 2, + DictKey = 3, + None = 4, + // Unknown = 5, // Currently unused +} + fn get_fd( fds: &mut Vec>, fd: usize, @@ -74,7 +84,7 @@ pub(crate) fn stat_path( for s in &basis_list { if s == basis { // Kind - writer.append(0u8); + writer.append(FileType::Basis as u8); // Length writer.append(0u64); return Ok(()); @@ -83,7 +93,7 @@ pub(crate) fn stat_path( // Otherwise, indicate it's nothing log::error!("remainder is empty on {} and basis doesn't exist", path); - writer.append(4u8); + writer.append(FileType::None as u8); return Ok(()); } } @@ -92,7 +102,7 @@ pub(crate) fn stat_path( // The root is a dict if stripped_path == "" { - writer.append(1u8); // Dict + writer.append(FileType::Dict as u8); // Dict return Ok(()); } @@ -120,15 +130,12 @@ pub(crate) fn stat_path( // Add the count of entries let val = match (is_dict, is_key) { - (true, false) => 1u8, - (false, true) => 2u8, - (true, true) => 3u8, - (false, false) => 4u8, + (true, false) => FileType::Dict, + (false, true) => FileType::Key, + (true, true) => FileType::DictKey, + (false, false) => FileType::None, }; - if val == 4 { - log::error!("path {} couldn't be found", path); - } - writer.append(val); + writer.append(val as u8); // Placeholder for file length writer.append(0u64);