diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/schema.rs | 39 | 
1 files changed, 29 insertions, 10 deletions
| diff --git a/src/schema.rs b/src/schema.rs index 80cf73f..cb353e4 100644 --- a/src/schema.rs +++ b/src/schema.rs | |||
| @@ -24,15 +24,34 @@ use std::vec::Vec; | |||
| 24 | 24 | ||
| 25 | pub type Fingerprint = String; | 25 | pub type Fingerprint = String; | 
| 26 | 26 | ||
| 27 | fn last_block_exists() -> Option<String> { | 27 | fn block_parser(path: String) -> u64 { | 
| 28 | let end_pos = path.find(".block").unwrap(); | ||
| 29 | let block_str = path[9..end_pos].to_string(); | ||
| 30 | let block_u64 : u64 = block_str.parse().unwrap(); | ||
| 31 | block_u64 | ||
| 32 | } | ||
| 33 | |||
| 34 | fn last_block_content() -> (bool, String) { | ||
| 28 | let blocks = read_block_name().unwrap(); | 35 | let blocks = read_block_name().unwrap(); | 
| 29 | for block in blocks { | 36 | |
| 37 | if blocks.len() == 0 { | ||
| 38 | return (false, "".to_string()); | ||
| 39 | } | ||
| 40 | |||
| 41 | let last_block = blocks[0].to_str().unwrap(); | ||
| 42 | let mut last_block = block_parser(last_block.to_string()); | ||
| 43 | let mut last_block_index = 0; | ||
| 44 | |||
| 45 | for (index, block) in blocks.iter().enumerate() { | ||
| 30 | let block = block.to_str().unwrap(); | 46 | let block = block.to_str().unwrap(); | 
| 31 | if block.contains("last.block") { | 47 | let block = block_parser(block.to_string()); | 
| 32 | return Some(block.to_string()); | 48 | if block > last_block { | 
| 49 | last_block = block; | ||
| 50 | last_block_index = index; | ||
| 33 | } | 51 | } | 
| 34 | } | 52 | } | 
| 35 | None | 53 | return (true, blocks[last_block_index].to_str().unwrap().parse().unwrap()); | 
| 54 | |||
| 36 | } | 55 | } | 
| 37 | 56 | ||
| 38 | fn read_block_name() -> io::Result<Vec<PathBuf>> { | 57 | fn read_block_name() -> io::Result<Vec<PathBuf>> { | 
| @@ -89,11 +108,11 @@ fn populate_db_with_users(db: &mut Db, files: Vec<PathBuf>) -> &mut Db { | |||
| 89 | pub fn create_database() -> Db { | 108 | pub fn create_database() -> Db { | 
| 90 | fs::create_dir_all("blocks").unwrap(); | 109 | fs::create_dir_all("blocks").unwrap(); | 
| 91 | fs::create_dir_all("users").unwrap(); | 110 | fs::create_dir_all("users").unwrap(); | 
| 92 | 111 | let (res, path) = last_block_content(); | |
| 93 | let mut db = Db::new(); | 112 | if res { | 
| 94 | 113 | return create_db_with_last_block(path); | |
| 95 | if let Some(blocks_path) = last_block_exists() { | 114 | } else { | 
| 96 | populate_db_with_last_block(&mut db, blocks_path); | 115 | return Db::new(); | 
| 97 | } | 116 | } | 
| 98 | 117 | ||
| 99 | if let Ok(users_path) = read_users() { | 118 | if let Ok(users_path) = read_users() { | 
