diff options
author | alpaylan | 2021-04-16 01:02:16 +0300 |
---|---|---|
committer | alpaylan | 2021-04-16 01:02:16 +0300 |
commit | 711d987b8e060682cf2215f25392415e206b3e8d (patch) | |
tree | 80cb5d6fd52b62edd83fc136be9fca8ca061185e /src | |
parent | 7bcdcbcaa5bc8b03f8838630761bb99bb2cecef0 (diff) | |
download | gradecoin-711d987b8e060682cf2215f25392415e206b3e8d.tar.gz gradecoin-711d987b8e060682cf2215f25392415e206b3e8d.tar.bz2 gradecoin-711d987b8e060682cf2215f25392415e206b3e8d.zip |
change last block reading method.
Diffstat (limited to 'src')
-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 { |