-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod types; | ||
pub mod state; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::collections::HashMap; | ||
use std::sync::{Arc, Mutex}; | ||
use chrono::{DateTime, Utc}; | ||
|
||
pub struct Channel { | ||
pub index: Mutex<HashMap<String, usize>>, | ||
pub data: Mutex<Vec<Message>>, | ||
} | ||
|
||
pub struct Database { | ||
pub channels: Arc<Mutex<HashMap<String, Channel>>>, | ||
} | ||
|
||
#[derive(Debug)] | ||
#[derive(Clone)] | ||
pub struct Message { | ||
pub uuid: String, | ||
pub created: DateTime<Utc>, | ||
pub value: String, | ||
// pub data: String, | ||
} | ||
|
||
pub fn create_db() -> Arc<Database>{ | ||
let index = HashMap::new(); | ||
let data = Vec::new(); | ||
let foo = Channel { | ||
index: Mutex::new(index), | ||
data: Mutex::new(data), | ||
}; | ||
let mut channels = HashMap::new(); | ||
channels.insert("foo".to_string(), foo); | ||
let db = Arc::new(Database { | ||
channels: Arc::new(Mutex::new(channels)), | ||
}); | ||
db | ||
} | ||
|
||
// pub fn get_channel<'a>(db: &'a Database, channel_id: String) -> &'a Channel { | ||
// let channels = db.channels.lock().unwrap(); | ||
// let _channel = channels.get(&channel_id); | ||
// let channel = _channel.unwrap(); | ||
// channel | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use crate::lib::state::Message; | ||
|
||
pub enum Request { | ||
Push { channel_id: String, value: String }, | ||
// Retrieve { channel_id: String, value: String }, | ||
// Update { channel_id: String, value: String }, | ||
Recent { channel_id: String, }, | ||
} | ||
|
||
pub enum Response { | ||
Push { | ||
message: Message, | ||
}, | ||
Recent { | ||
channel_id: String, | ||
}, | ||
Error { | ||
msg: String, | ||
}, | ||
} | ||
|
||
impl Request { | ||
pub fn parse(input: &str) -> Result<Request, String> { | ||
let mut parts = input.splitn(3, " "); | ||
let channel_id = match parts.next() { | ||
Some(channel_id) => channel_id, | ||
None => return Err(format!("PUSH needs a channel_id")), | ||
}; | ||
println!("Incoming on {:?}", channel_id); | ||
match parts.next() { | ||
// Some("RETRIEVE") => { | ||
// let key = match parts.next() { | ||
// Some(key) => key, | ||
// None => return Err(format!("GET must be followed by a key")), | ||
// }; | ||
// if parts.next().is_some() { | ||
// return Err(format!("GET's key must not be followed by anything")); | ||
// } | ||
// Ok(Request::Retrieve { | ||
// key: key.to_string(), | ||
// }) | ||
// } | ||
Some("PUSH") => { | ||
let value = match parts.next() { | ||
Some(value) => value, | ||
None => return Err(format!("PUSH needs a value")), | ||
}; | ||
println!("DOING PUSH on {} with {}", channel_id, value); | ||
Ok(Request::Push { | ||
channel_id: channel_id.to_string(), | ||
value: value.to_string(), | ||
}) | ||
} | ||
Some("RECENT") => { | ||
println!("DOING RECENT on {}", channel_id); | ||
Ok(Request::Recent { | ||
channel_id: channel_id.to_string(), | ||
}) | ||
} | ||
Some(cmd) => Err(format!("unknown command: {}", cmd)), | ||
None => Err(format!("empty input")), | ||
} | ||
} | ||
} | ||
|
||
impl Response { | ||
pub fn serialize(&self) -> String { | ||
match *self { | ||
Response::Push { ref message } => { | ||
format!("Pushed Id {}", message.uuid) | ||
}, | ||
Response::Recent { ref channel_id } => { | ||
println!("recent {:?}", channel_id); | ||
format!("Recent on {}", channel_id) | ||
}, | ||
// Response::Value { ref key, ref value } => format!("{} = {}", key, value), | ||
// Response::Set { | ||
// ref key, | ||
// ref value, | ||
// ref previous, | ||
// } => format!("set {} = `{}`, previous: {:?}", key, value, previous), | ||
Response::Error { ref msg } => format!("error: {}", msg), | ||
} | ||
} | ||
} |