|
|
@ -4,7 +4,7 @@ use std::hash::{Hash, Hasher}; |
|
|
|
use std::fs; |
|
|
|
use std::fs; |
|
|
|
use std::io::{self, Write, BufRead}; |
|
|
|
use std::io::{self, Write, BufRead}; |
|
|
|
use std::fmt; |
|
|
|
use std::fmt; |
|
|
|
use std::cmp::PartialEq; |
|
|
|
use std::cmp::{PartialEq, Ordering}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Item { |
|
|
|
pub struct Item { |
|
|
@ -12,7 +12,6 @@ pub struct Item { |
|
|
|
pub content: String |
|
|
|
pub content: String |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Item { |
|
|
|
impl Item { |
|
|
|
pub fn from(s: String) -> Item { |
|
|
|
pub fn from(s: String) -> Item { |
|
|
|
Item { id: s, content: String::from("") } |
|
|
|
Item { id: s, content: String::from("") } |
|
|
@ -41,40 +40,34 @@ impl fmt::Display for Item { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn test_items() { |
|
|
|
impl PartialOrd for Item { |
|
|
|
let mut item1 = Item::from("item1".to_string()); |
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
|
|
|
item1.content = "some content".to_string(); |
|
|
|
self.id.partial_cmp(&other.id) |
|
|
|
let mut item2 = Item::from("item1".to_string()); |
|
|
|
|
|
|
|
item2.content = "other content".to_string(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut hs = HashSet::<Item>::new(); |
|
|
|
|
|
|
|
hs.insert(item1); |
|
|
|
|
|
|
|
hs.insert(item2); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for item in hs.iter() { |
|
|
|
|
|
|
|
println!("item: {}", item) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Ord for Item { |
|
|
|
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering { |
|
|
|
|
|
|
|
self.id.cmp(&other.id) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub struct DB { |
|
|
|
pub struct DB { |
|
|
|
pub items: HashSet::<String> |
|
|
|
pub items: HashSet::<Item> |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl DB { |
|
|
|
impl DB { |
|
|
|
pub fn new(path: &str) -> io::Result<DB> { |
|
|
|
pub fn new(path: &str) -> io::Result<DB> { |
|
|
|
let file = fs::File::open(path)?; |
|
|
|
let file = fs::File::open(path)?; |
|
|
|
let reader = io::BufReader::new(file); |
|
|
|
let reader = io::BufReader::new(file); |
|
|
|
let mut items = HashSet::<String>::new(); |
|
|
|
let mut items = HashSet::<Item>::new(); |
|
|
|
for line in reader.lines() { |
|
|
|
for line in reader.lines() { |
|
|
|
match line { |
|
|
|
match line { |
|
|
|
Ok(content) => { |
|
|
|
Ok(id) => { |
|
|
|
items.insert(content); |
|
|
|
items.insert(Item::from(id)); |
|
|
|
}, |
|
|
|
}, |
|
|
|
Err(e) => { |
|
|
|
Err(e) => { |
|
|
|
eprintln!("Error reading line , {}", e); |
|
|
|
eprintln!("Error reading line, {}", e); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -84,14 +77,19 @@ impl DB { |
|
|
|
Ok(result) |
|
|
|
Ok(result) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn contains(&self, id: &String) -> bool { |
|
|
|
|
|
|
|
let item = Item::from(id.clone()); |
|
|
|
|
|
|
|
self.items.contains(&item) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn dump(&self, path: &str) -> io::Result<()> { |
|
|
|
pub fn dump(&self, path: &str) -> io::Result<()> { |
|
|
|
let mut file = fs::OpenOptions::new() |
|
|
|
let mut file = fs::OpenOptions::new() |
|
|
|
.write(true) |
|
|
|
.write(true) |
|
|
|
.append(false) |
|
|
|
.append(false) |
|
|
|
.open(path)?; |
|
|
|
.open(path)?; |
|
|
|
|
|
|
|
|
|
|
|
for id in self.items.iter() { |
|
|
|
for item in self.items.iter() { |
|
|
|
writeln!(file, "{}", id)?; |
|
|
|
writeln!(file, "{}", item.id)?; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|