emergency save.
This commit is contained in:
		
							
								
								
									
										46
									
								
								src/counter.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								src/counter.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
use std::iter::Iterator;
 | 
			
		||||
use uuid::Uuid;
 | 
			
		||||
 | 
			
		||||
struct Counter {
 | 
			
		||||
    id: Uuid,
 | 
			
		||||
    counter: u128,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Counter {
 | 
			
		||||
    fn new() -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            id: Uuid::new_v4(),
 | 
			
		||||
            counter: 0,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Iterator for Counter {
 | 
			
		||||
    type Item: Counter;
 | 
			
		||||
 | 
			
		||||
    fn next(&mut self) -> Option<Self::Item> {
 | 
			
		||||
        Counter::new()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod counters {
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn create_counter() {
 | 
			
		||||
        let count1 = Counter::new();
 | 
			
		||||
        let count2 = Counter::new();
 | 
			
		||||
        assert_ne!(count1.id, count2.id);
 | 
			
		||||
        assert_eq!(count1.counter, 0);
 | 
			
		||||
        assert_eq!(count2.counter, 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn iterate_counter() {
 | 
			
		||||
        let count = Counter::new();
 | 
			
		||||
        let first = count.next().unwrap();
 | 
			
		||||
        let second = count.next().unwrap();
 | 
			
		||||
        let third = count.next().unwrap();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										90
									
								
								src/lib.rs
									
									
									
									
									
								
							
							
						
						
									
										90
									
								
								src/lib.rs
									
									
									
									
									
								
							@@ -1,5 +1,9 @@
 | 
			
		||||
mod counter;
 | 
			
		||||
 | 
			
		||||
use rand::distributions::{Alphanumeric, DistString};
 | 
			
		||||
use std::{
 | 
			
		||||
    collections::HashMap,
 | 
			
		||||
    fmt,
 | 
			
		||||
    sync::mpsc::{channel, Receiver, Sender},
 | 
			
		||||
    thread::spawn,
 | 
			
		||||
};
 | 
			
		||||
@@ -25,7 +29,7 @@ enum SendMsg {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Cache {
 | 
			
		||||
    data: Vec<String>,
 | 
			
		||||
    data: HashMap<String, DataType>,
 | 
			
		||||
    rx: Receiver<SendMsg>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -33,7 +37,7 @@ impl Cache {
 | 
			
		||||
    fn new(recv: Receiver<SendMsg>) -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            rx: recv,
 | 
			
		||||
            data: Vec::new(),
 | 
			
		||||
            data: HashMap::new(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -53,11 +57,11 @@ impl Cache {
 | 
			
		||||
 | 
			
		||||
    fn validate_session(&mut self, sess: Option<String>) -> Session {
 | 
			
		||||
        let session: Session;
 | 
			
		||||
        if sess.is_some_and(|sess| self.data.contains(&sess)) {
 | 
			
		||||
        if sess.is_some_and(|sess| true) {// self.data.contains(&sess)) {
 | 
			
		||||
            session = Session::Ok;
 | 
			
		||||
        } else {
 | 
			
		||||
            let id = self.gen_id();
 | 
			
		||||
            self.data.push(id.clone());
 | 
			
		||||
            // `self.data.push(id.clone());
 | 
			
		||||
            session = Session::New(id);
 | 
			
		||||
        }
 | 
			
		||||
        session
 | 
			
		||||
@@ -133,3 +137,81 @@ mod client {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
enum Field {
 | 
			
		||||
    StaticString(String),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl fmt::Display for Field {
 | 
			
		||||
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 | 
			
		||||
        match self {
 | 
			
		||||
            Field::StaticString(data) => write!(f, "{}", data),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Record {
 | 
			
		||||
    data: HashMap<String, Field>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Record {
 | 
			
		||||
    fn new(data: HashMap<String, Field>) -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            data: data,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn get(&self, fieldname: &str) -> &Field {
 | 
			
		||||
        match self.data.get(fieldname) {
 | 
			
		||||
            Some(field) => field,
 | 
			
		||||
            None => unreachable!(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod records {
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn create_record() {
 | 
			
		||||
        let input = HashMap::from([
 | 
			
		||||
            ("one".to_string(), Field::StaticString("1".to_string())),
 | 
			
		||||
            ("two".to_string(), Field::StaticString("2".to_string())),
 | 
			
		||||
            ("three".to_string(), Field::StaticString("3".to_string())),
 | 
			
		||||
        ]);
 | 
			
		||||
        let rec = Record::new(input);
 | 
			
		||||
        assert_eq!(rec.get("one").to_string(), "1");
 | 
			
		||||
        assert_eq!(rec.get("two").to_string(), "2");
 | 
			
		||||
        assert_eq!(rec.get("three").to_string(), "3");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Column;
 | 
			
		||||
 | 
			
		||||
struct Table {
 | 
			
		||||
    columns: HashMap<String, Column>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Table {
 | 
			
		||||
    fn new() -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            columns: HashMap::new(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tables {
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn create_table() {
 | 
			
		||||
        let tbl = Table::new();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
enum DataType {
 | 
			
		||||
    Table(Table),
 | 
			
		||||
    Record(Record),
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user