feat: add server model types and API serialization shape
This commit is contained in:
Generated
+4
@@ -468,6 +468,10 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "whoareyou-server"
|
name = "whoareyou-server"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-link"
|
name = "windows-link"
|
||||||
|
|||||||
@@ -5,5 +5,7 @@ edition.workspace = true
|
|||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
serde_json = "1"
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
// modules added as they are implemented
|
pub mod model;
|
||||||
|
|||||||
@@ -0,0 +1,87 @@
|
|||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||||
|
pub struct Entry {
|
||||||
|
pub messages: Vec<String>,
|
||||||
|
pub history: Vec<String>,
|
||||||
|
pub comments: Vec<Comment>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||||
|
pub struct Comment {
|
||||||
|
/// Unix epoch seconds, UTC.
|
||||||
|
pub timestamp: Option<i64>,
|
||||||
|
pub title: Option<String>,
|
||||||
|
pub message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Per-provider outcome as exposed in the API (and cached).
|
||||||
|
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||||
|
#[serde(tag = "status", rename_all = "snake_case")]
|
||||||
|
pub enum ProviderResult {
|
||||||
|
Ok { entry: Entry },
|
||||||
|
NoData,
|
||||||
|
FetchFailed,
|
||||||
|
ParseFailed,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A fetched HTTP response handed to a provider's `parse`.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct FetchedResponse {
|
||||||
|
pub status: u16,
|
||||||
|
pub body: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Outcome of a provider's `parse` call, before API mapping.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum ParseOutcome {
|
||||||
|
Ok(Entry),
|
||||||
|
NoData,
|
||||||
|
Failed(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub struct LookupResponse {
|
||||||
|
pub number: String,
|
||||||
|
pub results: BTreeMap<String, ProviderResult>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn provider_result_serializes_to_api_shape() {
|
||||||
|
let ok = ProviderResult::Ok {
|
||||||
|
entry: Entry {
|
||||||
|
messages: vec![],
|
||||||
|
history: vec!["42 andra".to_string()],
|
||||||
|
comments: vec![Comment {
|
||||||
|
timestamp: Some(1547746162),
|
||||||
|
title: None,
|
||||||
|
message: "Varmsälj".to_string(),
|
||||||
|
}],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let json = serde_json::to_value(&ok).unwrap();
|
||||||
|
assert_eq!(json["status"], "ok");
|
||||||
|
assert_eq!(json["entry"]["history"][0], "42 andra");
|
||||||
|
assert_eq!(json["entry"]["comments"][0]["timestamp"], 1547746162);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::to_value(&ProviderResult::NoData).unwrap()["status"],
|
||||||
|
"no_data"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::to_value(&ProviderResult::FetchFailed).unwrap()["status"],
|
||||||
|
"fetch_failed"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::to_value(&ProviderResult::ParseFailed).unwrap()["status"],
|
||||||
|
"parse_failed"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user