Added a record.
This commit is contained in:
		@@ -1,32 +0,0 @@
 | 
				
			|||||||
use std::fmt;
 | 
					 | 
				
			||||||
use uuid::Uuid;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
struct ID {
 | 
					 | 
				
			||||||
    data: Uuid,
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl ID {
 | 
					 | 
				
			||||||
    fn new(id: Uuid) -> Self {
 | 
					 | 
				
			||||||
        Self {
 | 
					 | 
				
			||||||
            data: id,
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl fmt::Display for ID {
 | 
					 | 
				
			||||||
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 | 
					 | 
				
			||||||
        write!(f, "{}", self.data)
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#[cfg(test)]
 | 
					 | 
				
			||||||
mod id {
 | 
					 | 
				
			||||||
    use super::*;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    #[test]
 | 
					 | 
				
			||||||
    fn id_new() {
 | 
					 | 
				
			||||||
        let data = Uuid::new_v4();
 | 
					 | 
				
			||||||
        let id = ID::new(data.clone());
 | 
					 | 
				
			||||||
        assert_eq!(id.to_string(), data.to_string());
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,12 +1,9 @@
 | 
				
			|||||||
mod client;
 | 
					mod client;
 | 
				
			||||||
mod fields;
 | 
					mod record;
 | 
				
			||||||
mod message;
 | 
					mod message;
 | 
				
			||||||
mod router;
 | 
					mod router;
 | 
				
			||||||
mod session;
 | 
					mod session;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//use client::ClientMsg;
 | 
					 | 
				
			||||||
//use router::Router;
 | 
					 | 
				
			||||||
//use session::{Session, SessionFilter, SessionMsg};
 | 
					 | 
				
			||||||
use client::{Client, ClientMsg};
 | 
					use client::{Client, ClientMsg};
 | 
				
			||||||
use message::{Message, MsgData};
 | 
					use message::{Message, MsgData};
 | 
				
			||||||
use router::Router;
 | 
					use router::Router;
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										37
									
								
								src/record/id.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								src/record/id.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,37 @@
 | 
				
			|||||||
 | 
					use std::fmt;
 | 
				
			||||||
 | 
					use uuid::Uuid;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Clone)]
 | 
				
			||||||
 | 
					pub struct ID {
 | 
				
			||||||
 | 
					    data: Uuid,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl ID {
 | 
				
			||||||
 | 
					    pub fn new() -> Self {
 | 
				
			||||||
 | 
					        Self {
 | 
				
			||||||
 | 
					            data: Uuid::new_v4(),
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl fmt::Display for ID {
 | 
				
			||||||
 | 
					    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 | 
				
			||||||
 | 
					        write!(f, "{}", self.data)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[cfg(test)]
 | 
				
			||||||
 | 
					mod id {
 | 
				
			||||||
 | 
					    use super::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #[test]
 | 
				
			||||||
 | 
					    fn id_are_unique() {
 | 
				
			||||||
 | 
					        let mut ids: Vec<String> = Vec::new();
 | 
				
			||||||
 | 
					        for _ in 1..10 {
 | 
				
			||||||
 | 
					            let id = ID::new();
 | 
				
			||||||
 | 
					            let id_string = id.to_string();
 | 
				
			||||||
 | 
					            assert!(!ids.contains(&id_string), "'{}' repeated", id_string);
 | 
				
			||||||
 | 
					            ids.push(id_string);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										71
									
								
								src/record/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								src/record/mod.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,71 @@
 | 
				
			|||||||
 | 
					mod id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use id::ID;
 | 
				
			||||||
 | 
					use std::{
 | 
				
			||||||
 | 
					    collections::HashMap,
 | 
				
			||||||
 | 
					    fmt,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Clone)]
 | 
				
			||||||
 | 
					enum Field {
 | 
				
			||||||
 | 
					    ID(ID),
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct Record {
 | 
				
			||||||
 | 
					    data: HashMap<String, Field>,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Record {
 | 
				
			||||||
 | 
					    fn new(data: HashMap<String, Field>) -> Self {
 | 
				
			||||||
 | 
					        Self {
 | 
				
			||||||
 | 
					            data: data,
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn len(&self) -> usize {
 | 
				
			||||||
 | 
					        self.data.len()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn get(&self, name: &str) -> Field {
 | 
				
			||||||
 | 
					        match self.data.get(name) {
 | 
				
			||||||
 | 
					            Some(data) => data.clone(),
 | 
				
			||||||
 | 
					            None => unreachable!(),
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl fmt::Display for Field {
 | 
				
			||||||
 | 
					    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 | 
				
			||||||
 | 
					        match self {
 | 
				
			||||||
 | 
					            Field::ID(id) => write!(f, "{}", id),
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[cfg(test)]
 | 
				
			||||||
 | 
					mod records {
 | 
				
			||||||
 | 
					    use super::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #[test]
 | 
				
			||||||
 | 
					    fn new_record() {
 | 
				
			||||||
 | 
					        let data: HashMap<String, Field> = HashMap::new();
 | 
				
			||||||
 | 
					        let rec = Record::new(data);
 | 
				
			||||||
 | 
					        assert_eq!(rec.len(), 0);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #[test]
 | 
				
			||||||
 | 
					    fn new_record_more_data() {
 | 
				
			||||||
 | 
					        let a = ID::new();
 | 
				
			||||||
 | 
					        let b = ID::new();
 | 
				
			||||||
 | 
					        let c = ID::new();
 | 
				
			||||||
 | 
					        let mut data: HashMap<String, Field> = HashMap::new();
 | 
				
			||||||
 | 
					        data.insert("a".to_string(), Field::ID(a.clone()));
 | 
				
			||||||
 | 
					        data.insert("b".to_string(), Field::ID(b.clone()));
 | 
				
			||||||
 | 
					        data.insert("c".to_string(), Field::ID(c.clone()));
 | 
				
			||||||
 | 
					        let rec = Record::new(data);
 | 
				
			||||||
 | 
					        assert_eq!(rec.len(), 3);
 | 
				
			||||||
 | 
					        assert_eq!(rec.get("a").to_string(), a.to_string(), "record a");
 | 
				
			||||||
 | 
					        assert_eq!(rec.get("b").to_string(), b.to_string(), "record b");
 | 
				
			||||||
 | 
					        assert_eq!(rec.get("c").to_string(), c.to_string(), "record c");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user