Deprecate body_to_json: Use Axum Json Extractor instead

This commit is contained in:
Laurenz 2024-05-08 15:53:38 +02:00
parent 3447a9f4ff
commit 7e361cf940
2 changed files with 12 additions and 0 deletions

View file

@ -61,6 +61,7 @@ async fn get_data(
Ok(v) => { Ok(v) => {
let version: i64 = v.get("version_number"); let version: i64 = v.get("version_number");
let secret_content: HashMap<String, String> = HashMap::from([ let secret_content: HashMap<String, String> = HashMap::from([
// TODO: use sqlx to parse the row to a struct, do not do it manually
("secret_data".to_string(), v.get("secret_data")), ("secret_data".to_string(), v.get("secret_data")),
("created_time".to_string(), v.get("created_time")), ("created_time".to_string(), v.get("created_time")),
("deletion_time".to_string(), v.get("deletion_time")), ("deletion_time".to_string(), v.get("deletion_time")),

View file

@ -4,26 +4,33 @@ use super::structs::*;
// TODO create default function // TODO create default function
#[deprecated(note = "Use Axum functionality with structs instead, also, this should be inlined if it is actually needed")]
/// serialize secret to JSON String /// serialize secret to JSON String
pub fn serialize_secret_json(secret: &KvSecretReq) -> Result<String, serde_json::Error> { pub fn serialize_secret_json(secret: &KvSecretReq) -> Result<String, serde_json::Error> {
serde_json::to_string(&secret) serde_json::to_string(&secret)
} }
#[deprecated(note = "Use Axum functionality with structs instead, also, this should be inlined if it is actually needed")]
/// deserialize JSON String to secret /// deserialize JSON String to secret
pub fn deserialize_secret_struct(raw: &String) -> Result<KvSecretReq, serde_json::Error> { pub fn deserialize_secret_struct(raw: &String) -> Result<KvSecretReq, serde_json::Error> {
serde_json::from_str(raw) serde_json::from_str(raw)
} }
#[deprecated(note = "Use Axum functionality with structs instead, also, this should be inlined if it is actually needed")]
/// serialize metadata to JSON String /// serialize metadata to JSON String
pub fn serialize_metadata_json(secret: &SecretMeta) -> Result<String, serde_json::Error> { pub fn serialize_metadata_json(secret: &SecretMeta) -> Result<String, serde_json::Error> {
serde_json::to_string(&secret) serde_json::to_string(&secret)
} }
#[deprecated(note = "Use Axum functionality with structs instead, also, this should be inlined if it is actually needed")]
/// deserialize JSON String to metadata /// deserialize JSON String to metadata
pub fn deserialize_metadata_struct(raw: &String) -> Result<SecretMeta, serde_json::Error> { pub fn deserialize_metadata_struct(raw: &String) -> Result<SecretMeta, serde_json::Error> {
serde_json::from_str(raw) serde_json::from_str(raw)
} }
#[deprecated(note = "Propably not needed (remove deprecation if actually needed)")]
/// Consider:
/// Instead of patching JSON, we should apply the modified fields directly to the database
pub fn patch_metadata( pub fn patch_metadata(
old: &mut SecretMeta, old: &mut SecretMeta,
new: &SecretMeta, new: &SecretMeta,
@ -34,7 +41,11 @@ pub fn patch_metadata(
serde_json::from_value(patch) serde_json::from_value(patch)
} }
#[deprecated(note = "DO NOT USE: Use Axum extractors to structs instead")]
#[allow(unreachable_code, unused_variables)]
/// See [JSON extractor documentation](https://docs.rs/axum/latest/axum/struct.Json.html#extractor-example)
pub fn body_to_json(body: String) -> Value { pub fn body_to_json(body: String) -> Value {
todo!("REMOVE: Use Axum extractors to structs instead");
match serde_json::from_str::<serde_json::Value>(body.as_str()) { match serde_json::from_str::<serde_json::Value>(body.as_str()) {
Ok(val) => val, Ok(val) => val,
Err(e) => { Err(e) => {