Private
Public Access
1
0
This commit is contained in:
2024-04-21 15:14:16 -07:00
parent 0b2811dc9f
commit 3e878ced4e
8 changed files with 379 additions and 19 deletions

View File

@@ -6,3 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-trait = "0.1.80"
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.91"
time = { version = "0.3.17", features = ["parsing", "serde"] }
uuid = { version = "1.6.1", features = ["v4", "fast-rng", "macro-diagnostics"] }

View File

@@ -1,12 +1,14 @@
use async_trait::async_trait;
pub use crate::model::Conversation;
#[async_trait]
pub trait APIInterface {
fn version(&self) -> String;
type Error;
// (GET) /version
async fn get_version(&self) -> Result<String, Self::Error>;
// (GET) /conversations
async fn get_conversations(&self) -> Result<Vec<Conversation>, Self::Error>;
}
pub struct TestClient {}
impl APIInterface for TestClient {
fn version(&self) -> String {
return "KordophoneTest-1.0".to_string()
}
}

View File

@@ -1,16 +1,7 @@
mod api;
pub use self::api::TestClient;
pub mod model;
pub use self::api::APIInterface;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_version() {
let client = TestClient{};
let version = client.version();
assert_eq!(version, "KordophoneTest-1.0");
}
}
pub mod tests;

View File

@@ -0,0 +1,86 @@
use serde::Deserialize;
use time::OffsetDateTime;
use uuid::Uuid;
#[derive(Debug, Clone, Deserialize)]
pub struct Conversation {
pub guid: String,
#[serde(with = "time::serde::iso8601")]
pub date: OffsetDateTime,
#[serde(rename = "unreadCount")]
pub unread_count: i32,
#[serde(rename = "lastMessagePreview")]
pub last_message_preview: Option<String>,
#[serde(rename = "participantDisplayNames")]
pub participant_display_names: Vec<String>,
#[serde(rename = "displayName")]
pub display_name: Option<String>,
}
impl Conversation {
pub fn builder() -> ConversationBuilder {
ConversationBuilder::new()
}
}
#[derive(Default)]
pub struct ConversationBuilder {
guid: Option<String>,
date: Option<OffsetDateTime>,
unread_count: Option<i32>,
last_message_preview: Option<String>,
participant_display_names: Option<Vec<String>>,
display_name: Option<String>,
}
impl ConversationBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn guid(mut self, guid: String) -> Self {
self.guid = Some(guid);
self
}
pub fn date(mut self, date: OffsetDateTime) -> Self {
self.date = Some(date);
self
}
pub fn unread_count(mut self, unread_count: i32) -> Self {
self.unread_count = Some(unread_count);
self
}
pub fn last_message_preview(mut self, last_message_preview: String) -> Self {
self.last_message_preview = Some(last_message_preview);
self
}
pub fn participant_display_names(mut self, participant_display_names: Vec<String>) -> Self {
self.participant_display_names = Some(participant_display_names);
self
}
pub fn display_name<T>(mut self, display_name: T) -> Self where T: Into<String> {
self.display_name = Some(display_name.into());
self
}
pub fn build(self) -> Conversation {
Conversation {
guid: self.guid.unwrap_or(Uuid::new_v4().to_string()),
date: self.date.unwrap_or(OffsetDateTime::now_utc()),
unread_count: self.unread_count.unwrap_or(0),
last_message_preview: self.last_message_preview,
participant_display_names: self.participant_display_names.unwrap_or(vec![]),
display_name: self.display_name,
}
}
}

View File

@@ -0,0 +1,3 @@
pub mod conversation;
pub use conversation::Conversation;

View File

@@ -0,0 +1,22 @@
mod test_client;
use self::test_client::TestClient;
use crate::APIInterface;
pub mod api_interface {
use super::*;
#[test]
fn test_version() {
let client = TestClient{};
let version = client.get_version();
assert_eq!(version, "KordophoneTest-1.0");
}
#[test]
fn test_conversations() {
let client = TestClient{};
let conversations = client.get_conversations();
assert_eq!(conversations.len(), 1);
assert_eq!(conversations[0].display_name, Some("Test Conversation".to_string()));
}
}

View File

@@ -0,0 +1,16 @@
pub use crate::APIInterface;
use crate::model::Conversation;
pub struct TestClient {}
impl APIInterface for TestClient {
fn get_version(&self) -> String {
return "KordophoneTest-1.0".to_string()
}
fn get_conversations(&self) -> Vec<Conversation> {
let mut conversations = Vec::new();
conversations.push(Conversation::builder().display_name("Test Conversation").build());
conversations
}
}