Added Record.
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				MoreThanText/morethantext/pipeline/head This commit looks good
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	MoreThanText/morethantext/pipeline/head This commit looks good
				
			This commit is contained in:
		
							
								
								
									
										109
									
								
								src/lib.rs
									
									
									
									
									
								
							
							
						
						
									
										109
									
								
								src/lib.rs
									
									
									
									
									
								
							@@ -9,7 +9,11 @@ use client::{Client, ClientMsg};
 | 
				
			|||||||
use message::{Message, MsgData};
 | 
					use message::{Message, MsgData};
 | 
				
			||||||
use router::Router;
 | 
					use router::Router;
 | 
				
			||||||
use session::{Session, SessionData, SessionMsg};
 | 
					use session::{Session, SessionData, SessionMsg};
 | 
				
			||||||
use std::sync::mpsc::{channel, Sender};
 | 
					use std::{
 | 
				
			||||||
 | 
					    collections::HashMap,
 | 
				
			||||||
 | 
					    ops::Deref,
 | 
				
			||||||
 | 
					    sync::mpsc::{channel, Sender},
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct Request;
 | 
					struct Request;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -25,7 +29,7 @@ impl From<String> for Field {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
impl From<&str> for Field {
 | 
					impl From<&str> for Field {
 | 
				
			||||||
    fn from(value: &str) -> Self {
 | 
					    fn from(value: &str) -> Self {
 | 
				
			||||||
        Field::Static(value.to_string())
 | 
					        Field::Static(value.into())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -54,31 +58,96 @@ mod fields {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct Record;
 | 
					struct Record {
 | 
				
			||||||
 | 
					    data: HashMap<String, Field>,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Record {
 | 
				
			||||||
 | 
					    fn new() -> Self {
 | 
				
			||||||
 | 
					        Self {
 | 
				
			||||||
 | 
					            data: HashMap::new(),
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn add<S, F>(&mut self, name: S, data: F)
 | 
				
			||||||
 | 
					    where
 | 
				
			||||||
 | 
					        S: Into<String>,
 | 
				
			||||||
 | 
					        F: Into<Field>,
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        self.data.insert(name.into(), data.into());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Deref for Record {
 | 
				
			||||||
 | 
					    type Target = HashMap<String, Field>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn deref(&self) -> &Self::Target {
 | 
				
			||||||
 | 
					        &self.data
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[cfg(test)]
 | 
				
			||||||
 | 
					mod records {
 | 
				
			||||||
 | 
					    use super::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #[test]
 | 
				
			||||||
 | 
					    fn initialize() {
 | 
				
			||||||
 | 
					        let rec = Record::new();
 | 
				
			||||||
 | 
					        assert!(rec.is_empty());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #[test]
 | 
				
			||||||
 | 
					    fn bad_get_return() {
 | 
				
			||||||
 | 
					        let rec = Record::new();
 | 
				
			||||||
 | 
					        match rec.get("empty") {
 | 
				
			||||||
 | 
					            Some(_) => unreachable!("Should_have returned a None"),
 | 
				
			||||||
 | 
					            None => {}
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #[test]
 | 
				
			||||||
 | 
					    fn add_data() {
 | 
				
			||||||
 | 
					        let name = "name1x";
 | 
				
			||||||
 | 
					        let data = "data1";
 | 
				
			||||||
 | 
					        let mut rec = Record::new();
 | 
				
			||||||
 | 
					        rec.add(name, data);
 | 
				
			||||||
 | 
					        match rec.get(name) {
 | 
				
			||||||
 | 
					            None => unreachable!("Should return data"),
 | 
				
			||||||
 | 
					            Some(result) => match result {
 | 
				
			||||||
 | 
					                Field::Static(txt) => assert_eq!(txt, data),
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #[test]
 | 
				
			||||||
 | 
					    fn add_data_strings() {
 | 
				
			||||||
 | 
					        let name = "field".to_string();
 | 
				
			||||||
 | 
					        let data = "info".to_string();
 | 
				
			||||||
 | 
					        let mut rec = Record::new();
 | 
				
			||||||
 | 
					        rec.add(name.clone(), data.clone());
 | 
				
			||||||
 | 
					        match rec.get(&name) {
 | 
				
			||||||
 | 
					            None => unreachable!("Should return data"),
 | 
				
			||||||
 | 
					            Some(result) => match result {
 | 
				
			||||||
 | 
					                Field::Static(txt) => assert_eq!(txt, &data),
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct Response {
 | 
					struct Response {
 | 
				
			||||||
    headers: Vec<String>,
 | 
					    data: HashMap<String, Vec<Field>>,
 | 
				
			||||||
    records: Vec<Record>,
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Response {
 | 
					impl Response {
 | 
				
			||||||
    fn new() -> Self {
 | 
					    fn new() -> Self {
 | 
				
			||||||
        Self {
 | 
					        Self {
 | 
				
			||||||
            headers: Vec::new(),
 | 
					            data: HashMap::new(),
 | 
				
			||||||
            records: Vec::new(),
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn count(&self) -> usize {
 | 
					    fn count(&self) -> usize {
 | 
				
			||||||
        0
 | 
					        0
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    fn add_header<S>(&mut self, name: S)
 | 
					 | 
				
			||||||
    where
 | 
					 | 
				
			||||||
        S: Into<String>,
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        self.headers.push(name.into());
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[cfg(test)]
 | 
					#[cfg(test)]
 | 
				
			||||||
@@ -88,19 +157,9 @@ mod responses {
 | 
				
			|||||||
    #[test]
 | 
					    #[test]
 | 
				
			||||||
    fn create_response() {
 | 
					    fn create_response() {
 | 
				
			||||||
        let res = Response::new();
 | 
					        let res = Response::new();
 | 
				
			||||||
        assert!(res.headers.is_empty());
 | 
					        assert!(res.data.is_empty());
 | 
				
			||||||
        assert!(res.records.is_empty());
 | 
					 | 
				
			||||||
        assert_eq!(res.count(), 0);
 | 
					        assert_eq!(res.count(), 0);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    #[test]
 | 
					 | 
				
			||||||
    fn addA_header() {
 | 
					 | 
				
			||||||
        let mut res = Response::new();
 | 
					 | 
				
			||||||
        res.add_header("one");
 | 
					 | 
				
			||||||
        assert_eq!(res.headers, ["one"]);
 | 
					 | 
				
			||||||
        res.add_header("two".to_string());
 | 
					 | 
				
			||||||
        assert_eq!(res.headers, ["one", "two"]);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Support functions for Messages.
 | 
					/// Support functions for Messages.
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user