code cleanup

This commit is contained in:
Jeff Baskin 2024-05-12 14:22:11 -04:00
parent bb1cdfa890
commit 734fc3ba2d
3 changed files with 14 additions and 18 deletions

View File

@ -14,7 +14,10 @@ impl Cache {
/// This should not be called directly. /// This should not be called directly.
/// It is part of the MoreThanText::new function. /// It is part of the MoreThanText::new function.
pub fn new(rx: Receiver<SendMsg>) -> Self { pub fn new(rx: Receiver<SendMsg>) -> Self {
Self { data: Vec::new(), rx: rx } Self {
data: Vec::new(),
rx: rx,
}
} }
/// Starts listening for database requests. /// Starts listening for database requests.
@ -32,17 +35,15 @@ impl Cache {
fn get_session(&mut self, id: Option<String>) -> ReceiveMsg { fn get_session(&mut self, id: Option<String>) -> ReceiveMsg {
let sid: ReceiveMsg; let sid: ReceiveMsg;
match id { match id {
Some(input) => { Some(input) => match Uuid::parse_str(&input) {
match Uuid::parse_str(&input) {
Ok(vid) => { Ok(vid) => {
if self.data.contains(&vid) { if self.data.contains(&vid) {
sid = ReceiveMsg::Session(vid); sid = ReceiveMsg::Session(vid);
} else { } else {
sid = self.new_session(); sid = self.new_session();
} }
},
Err(_) => sid = self.new_session(),
} }
Err(_) => sid = self.new_session(),
}, },
None => sid = self.new_session(), None => sid = self.new_session(),
} }
@ -103,8 +104,7 @@ mod session {
let mut cache = Cache::new(rx); let mut cache = Cache::new(rx);
let id: Uuid; let id: Uuid;
let bad_id = "A very bad id"; let bad_id = "A very bad id";
match cache.get_session(Some(bad_id.to_string())) match cache.get_session(Some(bad_id.to_string())) {
{
ReceiveMsg::Session(sid) => id = sid, ReceiveMsg::Session(sid) => id = sid,
} }
assert_ne!(id.to_string(), bad_id); assert_ne!(id.to_string(), bad_id);

View File

@ -51,10 +51,7 @@ impl MoreThanText {
/// ``` /// ```
pub fn open_session(&mut self, id: Option<String>) { pub fn open_session(&mut self, id: Option<String>) {
let (tx, rx) = channel(); let (tx, rx) = channel();
let request = SessionRequest { let request = SessionRequest { id: id, tx: tx };
id: id,
tx: tx,
};
self.tx.send(SendMsg::OpenSession(request)).unwrap(); self.tx.send(SendMsg::OpenSession(request)).unwrap();
match rx.recv().unwrap() { match rx.recv().unwrap() {
ReceiveMsg::Session(sid) => self.id = Some(sid), ReceiveMsg::Session(sid) => self.id = Some(sid),

View File

@ -12,7 +12,6 @@ pub enum ReceiveMsg {
Session(Uuid), Session(Uuid),
} }
/// Items needed for a session request. /// Items needed for a session request.
pub struct SessionRequest { pub struct SessionRequest {
/// Optional string reprosentation of the session id. /// Optional string reprosentation of the session id.