code cleanup
This commit is contained in:
		
							
								
								
									
										26
									
								
								src/cache.rs
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								src/cache.rs
									
									
									
									
									
								
							@@ -14,7 +14,10 @@ impl Cache {
 | 
			
		||||
    /// This should not be called directly.
 | 
			
		||||
    /// It is part of the MoreThanText::new function.
 | 
			
		||||
    pub fn new(rx: Receiver<SendMsg>) -> Self {
 | 
			
		||||
        Self { data: Vec::new(), rx: rx }
 | 
			
		||||
        Self {
 | 
			
		||||
            data: Vec::new(),
 | 
			
		||||
            rx: rx,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Starts listening for database requests.
 | 
			
		||||
@@ -32,17 +35,15 @@ impl Cache {
 | 
			
		||||
    fn get_session(&mut self, id: Option<String>) -> ReceiveMsg {
 | 
			
		||||
        let sid: ReceiveMsg;
 | 
			
		||||
        match id {
 | 
			
		||||
            Some(input) => {
 | 
			
		||||
                match Uuid::parse_str(&input) {
 | 
			
		||||
                    Ok(vid) => {
 | 
			
		||||
                        if self.data.contains(&vid) {
 | 
			
		||||
                            sid = ReceiveMsg::Session(vid);
 | 
			
		||||
                        } else {
 | 
			
		||||
                            sid = self.new_session();
 | 
			
		||||
                        }
 | 
			
		||||
                    },
 | 
			
		||||
                    Err(_) => sid = self.new_session(),
 | 
			
		||||
            Some(input) => match Uuid::parse_str(&input) {
 | 
			
		||||
                Ok(vid) => {
 | 
			
		||||
                    if self.data.contains(&vid) {
 | 
			
		||||
                        sid = ReceiveMsg::Session(vid);
 | 
			
		||||
                    } else {
 | 
			
		||||
                        sid = self.new_session();
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                Err(_) => sid = self.new_session(),
 | 
			
		||||
            },
 | 
			
		||||
            None => sid = self.new_session(),
 | 
			
		||||
        }
 | 
			
		||||
@@ -103,8 +104,7 @@ mod session {
 | 
			
		||||
        let mut cache = Cache::new(rx);
 | 
			
		||||
        let id: Uuid;
 | 
			
		||||
        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,
 | 
			
		||||
        }
 | 
			
		||||
        assert_ne!(id.to_string(), bad_id);
 | 
			
		||||
 
 | 
			
		||||
@@ -51,10 +51,7 @@ impl MoreThanText {
 | 
			
		||||
    ///  ```
 | 
			
		||||
    pub fn open_session(&mut self, id: Option<String>) {
 | 
			
		||||
        let (tx, rx) = channel();
 | 
			
		||||
        let request = SessionRequest {
 | 
			
		||||
            id: id,
 | 
			
		||||
            tx: tx,
 | 
			
		||||
        };
 | 
			
		||||
        let request = SessionRequest { id: id, tx: tx };
 | 
			
		||||
        self.tx.send(SendMsg::OpenSession(request)).unwrap();
 | 
			
		||||
        match rx.recv().unwrap() {
 | 
			
		||||
            ReceiveMsg::Session(sid) => self.id = Some(sid),
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,6 @@ pub enum ReceiveMsg {
 | 
			
		||||
    Session(Uuid),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/// Items needed for a session request.
 | 
			
		||||
pub struct SessionRequest {
 | 
			
		||||
    /// Optional string reprosentation of the session id.
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user