Use serde to serialize/deserialize Privilege instead of manual

This commit is contained in:
Jere Vaara
2024-10-15 17:07:23 +03:00
parent 39e0e31605
commit d0ca79aeb3
5 changed files with 11 additions and 69 deletions

View File

@@ -1,6 +1,5 @@
use std::str::FromStr;
#[derive(Debug)]
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum Privilege {
Select,
Insert,
@@ -15,47 +14,3 @@ pub enum Privilege {
Temporary,
Execute,
}
impl FromStr for Privilege {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_uppercase().as_str() {
"SELECT" => Ok(Privilege::Select),
"INSERT" => Ok(Privilege::Insert),
"UPDATE" => Ok(Privilege::Update),
"DELETE" => Ok(Privilege::Delete),
"TRUNCATE" => Ok(Privilege::Truncate),
"REFERENCES" => Ok(Privilege::References),
"TRIGGER" => Ok(Privilege::Trigger),
"USAGE" => Ok(Privilege::Usage),
"CREATE" => Ok(Privilege::Create),
"CONNECT" => Ok(Privilege::Connect),
"TEMPORARY" => Ok(Privilege::Temporary),
"EXECUTE" => Ok(Privilege::Execute),
_ => Err("Invalid privilege"),
}
}
}
impl Privilege {
pub fn as_str(&self) -> &'static str {
match self {
Privilege::Select => "SELECT",
Privilege::Insert => "INSERT",
Privilege::Update => "UPDATE",
Privilege::Delete => "DELETE",
Privilege::Truncate => "TRUNCATE",
Privilege::References => "REFERENCES",
Privilege::Trigger => "TRIGGER",
Privilege::Usage => "USAGE",
Privilege::Create => "CREATE",
Privilege::Connect => "CONNECT",
Privilege::Temporary => "TEMPORARY",
Privilege::Execute => "EXECUTE",
}
}
pub fn to_string(&self) -> String {
self.as_str().to_string()
}
}

View File

@@ -1,6 +1,6 @@
//! Structs representing the JSON formats used in the compute_ctl's HTTP API.
use crate::spec::ComputeSpec;
use crate::{privilege::Privilege, spec::ComputeSpec};
use serde::Deserialize;
/// Request of the /configure API
@@ -17,6 +17,6 @@ pub struct ConfigurationRequest {
pub struct SetRoleGrantsRequest {
pub database: String,
pub schema: String,
pub privileges: Vec<String>,
pub privileges: Vec<Privilege>,
pub role: String,
}

View File

@@ -6,7 +6,10 @@ use std::fmt::Display;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize, Serializer};
use crate::spec::{ComputeSpec, Database, Role};
use crate::{
privilege::Privilege,
spec::{ComputeSpec, Database, Role},
};
#[derive(Serialize, Debug, Deserialize)]
pub struct GenericAPIError {
@@ -173,6 +176,6 @@ pub struct InstalledExtensions {
pub struct SetRoleGrantsResponse {
pub database: String,
pub schema: String,
pub privileges: Vec<String>,
pub privileges: Vec<Privilege>,
pub role: String,
}