Change Actions enum to bitflags

This commit is contained in:
F. Levi 2024-08-31 13:18:59 +03:00
parent 9a756cf2c5
commit 07e7ddbc5b
4 changed files with 211 additions and 73 deletions

1
Cargo.lock generated
View File

@ -3486,6 +3486,7 @@ version = "1.10.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"anyhow", "anyhow",
"bitflags 2.6.0",
"convert_case 0.6.0", "convert_case 0.6.0",
"csv", "csv",
"deserr", "deserr",

View File

@ -38,6 +38,7 @@ time = { version = "0.3.36", features = [
] } ] }
tokio = "1.38" tokio = "1.38"
uuid = { version = "1.10.0", features = ["serde", "v4"] } uuid = { version = "1.10.0", features = ["serde", "v4"] }
bitflags = {version = "2.6.0", features = ["serde"] }
[dev-dependencies] [dev-dependencies]
insta = "1.39.0" insta = "1.39.0"

View File

@ -2,7 +2,8 @@ use std::convert::Infallible;
use std::hash::Hash; use std::hash::Hash;
use std::str::FromStr; use std::str::FromStr;
use deserr::{DeserializeError, Deserr, MergeWithError, ValuePointerRef}; use bitflags::bitflags;
use deserr::{take_cf_content, DeserializeError, Deserr, MergeWithError, ValuePointerRef};
use enum_iterator::Sequence; use enum_iterator::Sequence;
use milli::update::Setting; use milli::update::Setting;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -179,113 +180,248 @@ fn parse_expiration_date(
} }
} }
#[derive(Copy, Clone, Serialize, Deserialize, Debug, Eq, PartialEq, Hash, Sequence, Deserr)] bitflags! {
#[repr(u8)] #[derive(Copy, Clone, Serialize, Deserialize, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub enum Action { #[repr(transparent)]
pub struct Action: u8 {
#[serde(rename = "*")] #[serde(rename = "*")]
#[deserr(rename = "*")] const All = 0;
All = 0,
#[serde(rename = "search")] #[serde(rename = "search")]
#[deserr(rename = "search")] const Search = 1;
Search,
#[serde(rename = "documents.*")] #[serde(rename = "documents.*")]
#[deserr(rename = "documents.*")] const DocumentsAll = 2;
DocumentsAll,
#[serde(rename = "documents.add")] #[serde(rename = "documents.add")]
#[deserr(rename = "documents.add")] const DocumentsAdd = 3;
DocumentsAdd,
#[serde(rename = "documents.get")] #[serde(rename = "documents.get")]
#[deserr(rename = "documents.get")] const DocumentsGet = 4;
DocumentsGet,
#[serde(rename = "documents.delete")] #[serde(rename = "documents.delete")]
#[deserr(rename = "documents.delete")] const DocumentsDelete = 5;
DocumentsDelete,
#[serde(rename = "indexes.*")] #[serde(rename = "indexes.*")]
#[deserr(rename = "indexes.*")] const IndexesAll = 6;
IndexesAll,
#[serde(rename = "indexes.create")] #[serde(rename = "indexes.create")]
#[deserr(rename = "indexes.create")] const IndexesAdd = 7;
IndexesAdd,
#[serde(rename = "indexes.get")] #[serde(rename = "indexes.get")]
#[deserr(rename = "indexes.get")] const IndexesGet = 8;
IndexesGet,
#[serde(rename = "indexes.update")] #[serde(rename = "indexes.update")]
#[deserr(rename = "indexes.update")] const IndexesUpdate = 9;
IndexesUpdate,
#[serde(rename = "indexes.delete")] #[serde(rename = "indexes.delete")]
#[deserr(rename = "indexes.delete")] const IndexesDelete = 10;
IndexesDelete,
#[serde(rename = "indexes.swap")] #[serde(rename = "indexes.swap")]
#[deserr(rename = "indexes.swap")] const IndexesSwap = 11;
IndexesSwap,
#[serde(rename = "tasks.*")] #[serde(rename = "tasks.*")]
#[deserr(rename = "tasks.*")] const TasksAll = 12;
TasksAll,
#[serde(rename = "tasks.cancel")] #[serde(rename = "tasks.cancel")]
#[deserr(rename = "tasks.cancel")] const TasksCancel = 13;
TasksCancel,
#[serde(rename = "tasks.delete")] #[serde(rename = "tasks.delete")]
#[deserr(rename = "tasks.delete")] const TasksDelete = 14;
TasksDelete,
#[serde(rename = "tasks.get")] #[serde(rename = "tasks.get")]
#[deserr(rename = "tasks.get")] const TasksGet = 15;
TasksGet,
#[serde(rename = "settings.*")] #[serde(rename = "settings.*")]
#[deserr(rename = "settings.*")] const SettingsAll = 16;
SettingsAll,
#[serde(rename = "settings.get")] #[serde(rename = "settings.get")]
#[deserr(rename = "settings.get")] const SettingsGet = 17;
SettingsGet,
#[serde(rename = "settings.update")] #[serde(rename = "settings.update")]
#[deserr(rename = "settings.update")] const SettingsUpdate = 18;
SettingsUpdate,
#[serde(rename = "stats.*")] #[serde(rename = "stats.*")]
#[deserr(rename = "stats.*")] const StatsAll = 19;
StatsAll,
#[serde(rename = "stats.get")] #[serde(rename = "stats.get")]
#[deserr(rename = "stats.get")] const StatsGet = 20;
StatsGet,
#[serde(rename = "metrics.*")] #[serde(rename = "metrics.*")]
#[deserr(rename = "metrics.*")] const MetricsAll = 21;
MetricsAll,
#[serde(rename = "metrics.get")] #[serde(rename = "metrics.get")]
#[deserr(rename = "metrics.get")] const MetricsGet = 22;
MetricsGet,
#[serde(rename = "dumps.*")] #[serde(rename = "dumps.*")]
#[deserr(rename = "dumps.*")] const DumpsAll = 23;
DumpsAll,
#[serde(rename = "dumps.create")] #[serde(rename = "dumps.create")]
#[deserr(rename = "dumps.create")] const DumpsCreate = 24;
DumpsCreate,
#[serde(rename = "snapshots.*")] #[serde(rename = "snapshots.*")]
#[deserr(rename = "snapshots.*")] const SnapshotsAll = 25;
SnapshotsAll,
#[serde(rename = "snapshots.create")] #[serde(rename = "snapshots.create")]
#[deserr(rename = "snapshots.create")] const SnapshotsCreate = 26;
SnapshotsCreate,
#[serde(rename = "version")] #[serde(rename = "version")]
#[deserr(rename = "version")] const Version = 27;
Version,
#[serde(rename = "keys.create")] #[serde(rename = "keys.create")]
#[deserr(rename = "keys.create")] const KeysAdd = 28;
KeysAdd,
#[serde(rename = "keys.get")] #[serde(rename = "keys.get")]
#[deserr(rename = "keys.get")] const KeysGet = 29;
KeysGet,
#[serde(rename = "keys.update")] #[serde(rename = "keys.update")]
#[deserr(rename = "keys.update")] const KeysUpdate = 30;
KeysUpdate,
#[serde(rename = "keys.delete")] #[serde(rename = "keys.delete")]
#[deserr(rename = "keys.delete")] const KeysDelete = 31;
KeysDelete,
#[serde(rename = "experimental.get")] #[serde(rename = "experimental.get")]
#[deserr(rename = "experimental.get")] const ExperimentalFeaturesGet = 32;
ExperimentalFeaturesGet,
#[serde(rename = "experimental.update")] #[serde(rename = "experimental.update")]
#[deserr(rename = "experimental.update")] const ExperimentalFeaturesUpdate = 33;
ExperimentalFeaturesUpdate, }
} }
impl<E: DeserializeError> Deserr<E> for Action {
fn deserialize_from_value<V: deserr::IntoValue>(
value: deserr::Value<V>,
location: deserr::ValuePointerRef<'_>,
) -> Result<Self, E> {
match value {
deserr::Value::String(s) => match s.as_str() {
"*" => Ok(Action::All),
"search" => Ok(Action::Search),
"documents.*" => Ok(Action::DocumentsAll),
"documents.add" => Ok(Action::DocumentsAdd),
"documents.get" => Ok(Action::DocumentsGet),
"documents.delete" => Ok(Action::DocumentsDelete),
"indexes.*" => Ok(Action::IndexesAll),
"indexes.create" => Ok(Action::IndexesAdd),
"indexes.get" => Ok(Action::IndexesGet),
"indexes.update" => Ok(Action::IndexesUpdate),
"indexes.delete" => Ok(Action::IndexesDelete),
"indexes.swap" => Ok(Action::IndexesSwap),
"tasks.*" => Ok(Action::TasksAll),
"tasks.cancel" => Ok(Action::TasksCancel),
"tasks.delete" => Ok(Action::TasksDelete),
"tasks.get" => Ok(Action::TasksGet),
"settings.*" => Ok(Action::SettingsAll),
"settings.get" => Ok(Action::SettingsGet),
"settings.update" => Ok(Action::SettingsUpdate),
"stats.*" => Ok(Action::StatsAll),
"stats.get" => Ok(Action::StatsGet),
"metrics.*" => Ok(Action::MetricsAll),
"metrics.get" => Ok(Action::MetricsGet),
"dumps.*" => Ok(Action::DumpsAll),
"dumps.create" => Ok(Action::DumpsCreate),
"snapshots.*" => Ok(Action::SnapshotsAll),
"snapshots.create" => Ok(Action::SnapshotsCreate),
"version" => Ok(Action::Version),
"keys.create" => Ok(Action::KeysAdd),
"keys.get" => Ok(Action::KeysGet),
"keys.update" => Ok(Action::KeysUpdate),
"keys.delete" => Ok(Action::KeysDelete),
"experimental.get" => Ok(Action::ExperimentalFeaturesGet),
"experimental.update" => Ok(Action::ExperimentalFeaturesUpdate),
_ => Err(deserr::take_cf_content(E::error::<std::convert::Infallible>(
None,
deserr::ErrorKind::Unexpected { msg: format!("TODO {}", s) },
location,
))),
},
value => Err(take_cf_content(E::error(
None,
deserr::ErrorKind::IncorrectValueKind {
actual: value,
accepted: &[deserr::ValueKind::String],
},
location,
))),
}
}
}
// #[derive(Copy, Clone, Serialize, Deserialize, Debug, Eq, PartialEq, Hash, Sequence, Deserr)]
// #[repr(u8)]
// pub enum Action {
// #[serde(rename = "*")]
// #[deserr(rename = "*")]
// All = 0,
// #[serde(rename = "search")]
// #[deserr(rename = "search")]
// Search,
// #[serde(rename = "documents.*")]
// #[deserr(rename = "documents.*")]
// DocumentsAll,
// #[serde(rename = "documents.add")]
// #[deserr(rename = "documents.add")]
// DocumentsAdd,
// #[serde(rename = "documents.get")]
// #[deserr(rename = "documents.get")]
// DocumentsGet,
// #[serde(rename = "documents.delete")]
// #[deserr(rename = "documents.delete")]
// DocumentsDelete,
// #[serde(rename = "indexes.*")]
// #[deserr(rename = "indexes.*")]
// IndexesAll,
// #[serde(rename = "indexes.create")]
// #[deserr(rename = "indexes.create")]
// IndexesAdd,
// #[serde(rename = "indexes.get")]
// #[deserr(rename = "indexes.get")]
// IndexesGet,
// #[serde(rename = "indexes.update")]
// #[deserr(rename = "indexes.update")]
// IndexesUpdate,
// #[serde(rename = "indexes.delete")]
// #[deserr(rename = "indexes.delete")]
// IndexesDelete,
// #[serde(rename = "indexes.swap")]
// #[deserr(rename = "indexes.swap")]
// IndexesSwap,
// #[serde(rename = "tasks.*")]
// #[deserr(rename = "tasks.*")]
// TasksAll,
// #[serde(rename = "tasks.cancel")]
// #[deserr(rename = "tasks.cancel")]
// TasksCancel,
// #[serde(rename = "tasks.delete")]
// #[deserr(rename = "tasks.delete")]
// TasksDelete,
// #[serde(rename = "tasks.get")]
// #[deserr(rename = "tasks.get")]
// TasksGet,
// #[serde(rename = "settings.*")]
// #[deserr(rename = "settings.*")]
// SettingsAll,
// #[serde(rename = "settings.get")]
// #[deserr(rename = "settings.get")]
// SettingsGet,
// #[serde(rename = "settings.update")]
// #[deserr(rename = "settings.update")]
// SettingsUpdate,
// #[serde(rename = "stats.*")]
// #[deserr(rename = "stats.*")]
// StatsAll,
// #[serde(rename = "stats.get")]
// #[deserr(rename = "stats.get")]
// StatsGet,
// #[serde(rename = "metrics.*")]
// #[deserr(rename = "metrics.*")]
// MetricsAll,
// #[serde(rename = "metrics.get")]
// #[deserr(rename = "metrics.get")]
// MetricsGet,
// #[serde(rename = "dumps.*")]
// #[deserr(rename = "dumps.*")]
// DumpsAll,
// #[serde(rename = "dumps.create")]
// #[deserr(rename = "dumps.create")]
// DumpsCreate,
// #[serde(rename = "snapshots.*")]
// #[deserr(rename = "snapshots.*")]
// SnapshotsAll,
// #[serde(rename = "snapshots.create")]
// #[deserr(rename = "snapshots.create")]
// SnapshotsCreate,
// #[serde(rename = "version")]
// #[deserr(rename = "version")]
// Version,
// #[serde(rename = "keys.create")]
// #[deserr(rename = "keys.create")]
// KeysAdd,
// #[serde(rename = "keys.get")]
// #[deserr(rename = "keys.get")]
// KeysGet,
// #[serde(rename = "keys.update")]
// #[deserr(rename = "keys.update")]
// KeysUpdate,
// #[serde(rename = "keys.delete")]
// #[deserr(rename = "keys.delete")]
// KeysDelete,
// #[serde(rename = "experimental.get")]
// #[deserr(rename = "experimental.get")]
// ExperimentalFeaturesGet,
// #[serde(rename = "experimental.update")]
// #[deserr(rename = "experimental.update")]
// ExperimentalFeaturesUpdate,
// }
impl Action { impl Action {
pub const fn from_repr(repr: u8) -> Option<Self> { pub const fn from_repr(repr: u8) -> Option<Self> {
use actions::*; use actions::*;

View File

@ -352,7 +352,7 @@ where
None, None,
deserr::ErrorKind::Unexpected { deserr::ErrorKind::Unexpected {
msg: format!( msg: format!(
"the distribution sigma must be in the range ]0, 1], got {}", "the distribution sigma must be in the range [0, 1], got {}",
value.sigma value.sigma
), ),
}, },