Skip to content

Commit

Permalink
Add lib to source control
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Mar 4, 2019
1 parent c903889 commit 033e0b1
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod types;
pub mod state;
43 changes: 43 additions & 0 deletions src/lib/state.rs
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
// }
85 changes: 85 additions & 0 deletions src/lib/types.rs
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),
}
}
}

0 comments on commit 033e0b1

Please sign in to comment.