Using serde_json
This commit is contained in:
@ -1,35 +1,36 @@
|
||||
use crate::database;
|
||||
use json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
|
||||
static ENDPOINTS: [(&str, fn(json::JsonValue) -> json::JsonValue); 1] = [("echo", echo)];
|
||||
|
||||
pub fn is_valid_endpoint(endpoint: &str) -> bool {
|
||||
for (name, _func) in &ENDPOINTS {
|
||||
if endpoint.eq(*name) {
|
||||
return true;
|
||||
}
|
||||
pub fn endpoint(endpoint: &str, input: serde_json::Value) -> serde_json::Result<serde_json::Value> {
|
||||
match endpoint {
|
||||
"echo" => Ok(serde_json::to_value(echo(serde_json::from_value(input)?))?),
|
||||
_ => Ok("{}".into()),
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn endpoint(endpoint: &str, input: json::JsonValue) -> json::JsonValue {
|
||||
for (name, func) in &ENDPOINTS {
|
||||
if endpoint.eq(*name) {
|
||||
return func(input);
|
||||
}
|
||||
}
|
||||
panic!("invalid endpoint, check with is_valid_endpoint")
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct EchoInput {
|
||||
message: String,
|
||||
}
|
||||
|
||||
pub fn echo(input: json::JsonValue) -> json::JsonValue {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct EchoOutput {
|
||||
message: String,
|
||||
database: Option<i32>,
|
||||
}
|
||||
|
||||
pub fn echo(input: EchoInput) -> EchoOutput {
|
||||
let backend = database::client();
|
||||
let mut output = input.clone();
|
||||
let mut output = EchoOutput {
|
||||
message: input.message,
|
||||
database: None,
|
||||
};
|
||||
match backend {
|
||||
database::Client::Postgres(mut client) => {
|
||||
let res = client.query("SELECT * FROM test", &[]).unwrap();
|
||||
for row in res {
|
||||
let val: i32 = row.get(0);
|
||||
output["database"] = val.into();
|
||||
output.database = Some(row.get(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user