This commit is contained in:
2022-09-19 07:53:06 +00:00
committed by GitHub
parent c356540463
commit a04429838c
3 changed files with 57 additions and 0 deletions

View File

@@ -5,6 +5,8 @@ pub mod context;
pub mod cors;
/// Predefined includes
pub mod preclude;
/// Base result type for JSON response
pub mod result;
/// Routes
pub mod route;
/// Services

View File

@@ -1,4 +1,5 @@
pub use super::context::ServerContext;
pub use super::result::JSONResult;
pub use super::route::ResponseForType;
pub use super::traits::{MatchRoute, ResponseFor, ResponseJsonFor};
pub use crate::error::PixivDownloaderError;

54
src/server/result.rs Normal file
View File

@@ -0,0 +1,54 @@
use json::JsonValue;
use crate::ext::json::ToJson2;
#[derive(Clone, Debug)]
/// Error information of a request
pub struct JSONError {
/// Error code
pub code: i32,
/// Error message
pub msg: String,
/// The debug information of the error
pub debug_msg: Option<JsonValue>,
}
impl From<(i32, String)> for JSONError {
fn from((code, msg): (i32, String)) -> Self {
Self {
code,
msg,
debug_msg: None,
}
}
}
impl From<(i32, String, Option<JsonValue>)> for JSONError {
fn from((code, msg, debug_msg): (i32, String, Option<JsonValue>)) -> Self {
Self {
code,
msg,
debug_msg,
}
}
}
pub type JSONResult = Result<JsonValue, JSONError>;
impl ToJson2 for JSONResult {
fn to_json2(&self) -> JsonValue {
match self {
Self::Ok(v) => json::object! {
"ok": true,
"code": 0,
"result": v.clone(),
},
Self::Err(e) => json::object! {
"ok": false,
"code": e.code,
"msg": e.msg.as_str(),
"debug_msg": e.debug_msg.clone().unwrap_or(JsonValue::Null),
},
}
}
}