diff options
Diffstat (limited to 'src/schema.rs')
-rw-r--r-- | src/schema.rs | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/src/schema.rs b/src/schema.rs index af10b4d..264d2bd 100644 --- a/src/schema.rs +++ b/src/schema.rs | |||
@@ -25,15 +25,34 @@ pub type Fingerprint = String; | |||
25 | 25 | ||
26 | // TODO: start by reading users from file too <14-04-21, yigit> // | 26 | // TODO: start by reading users from file too <14-04-21, yigit> // |
27 | 27 | ||
28 | fn last_block_exists() -> (bool, String) { | 28 | fn block_parser(path: String) -> u64 { |
29 | let end_pos = path.find(".block").unwrap(); | ||
30 | let block_str = path[9..end_pos].to_string(); | ||
31 | let block_u64 : u64 = block_str.parse().unwrap(); | ||
32 | block_u64 | ||
33 | } | ||
34 | |||
35 | fn last_block_content() -> (bool, String) { | ||
29 | let blocks = read_block_name().unwrap(); | 36 | let blocks = read_block_name().unwrap(); |
30 | for block in blocks { | 37 | |
38 | if blocks.len() == 0 { | ||
39 | return (false, "".to_string()); | ||
40 | } | ||
41 | |||
42 | let last_block = blocks[0].to_str().unwrap(); | ||
43 | let mut last_block = block_parser(last_block.to_string()); | ||
44 | let mut last_block_index = 0; | ||
45 | |||
46 | for (index, block) in blocks.iter().enumerate() { | ||
31 | let block = block.to_str().unwrap(); | 47 | let block = block.to_str().unwrap(); |
32 | if block.contains("last.block") { | 48 | let block = block_parser(block.to_string()); |
33 | return (true, block.to_string()); | 49 | if block > last_block { |
50 | last_block = block; | ||
51 | last_block_index = index; | ||
34 | } | 52 | } |
35 | } | 53 | } |
36 | (false, "".to_string()) | 54 | return (true, blocks[last_block_index].to_str().unwrap().parse().unwrap()); |
55 | |||
37 | } | 56 | } |
38 | 57 | ||
39 | fn read_block_name() -> io::Result<Vec<PathBuf>> { | 58 | fn read_block_name() -> io::Result<Vec<PathBuf>> { |
@@ -57,7 +76,7 @@ fn create_db_with_last_block(path: String) -> Db { | |||
57 | pub fn create_database() -> Db { | 76 | pub fn create_database() -> Db { |
58 | fs::create_dir_all("blocks").unwrap(); | 77 | fs::create_dir_all("blocks").unwrap(); |
59 | fs::create_dir_all("users").unwrap(); | 78 | fs::create_dir_all("users").unwrap(); |
60 | let (res, path) = last_block_exists(); | 79 | let (res, path) = last_block_content(); |
61 | if res { | 80 | if res { |
62 | return create_db_with_last_block(path); | 81 | return create_db_with_last_block(path); |
63 | } else { | 82 | } else { |