diff --git a/Cargo.lock b/Cargo.lock index baa8409..8e6c9cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,7 +16,7 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] name = "booklibrs" -version = "1.0.15" +version = "1.1.0" dependencies = [ "chrono", "fltk 0.15.4 (git+https://github.com/MoAlyousef/fltk-rs)", diff --git a/Cargo.toml b/Cargo.toml index c477e59..4533bd0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,12 @@ [package] name = "booklibrs" -version = "1.0.15" +version = "1.1.0" authors = ["Paranid5 "] edition = "2018" license = "MIT/Apache-2.0" description = "Book libarary project including giveaway operations and gui interface." homepage = "https://github.com/dinaraparanid/Library_rs" -documentation = "https://docs.rs/binartree/1.0.15/booklibrs/" +documentation = "https://docs.rs/binartree/1.1.0/booklibrs/" [dependencies] chrono = "0.4.19" diff --git a/src/actions/book/utils.rs b/src/actions/book/utils.rs index 55d1e99..a9fb755 100644 --- a/src/actions/book/utils.rs +++ b/src/actions/book/utils.rs @@ -1,8 +1,7 @@ extern crate fltk; -use fltk::dialog::alert; - use crate::{books::book_sys::BookSystem, Lang}; +use fltk::dialog::alert; /// Function that checks /// if input of book was empty diff --git a/src/actions/genres.rs b/src/actions/genres.rs index 82278d5..e3ff7b4 100644 --- a/src/actions/genres.rs +++ b/src/actions/genres.rs @@ -214,7 +214,7 @@ pub fn customize_book_genre( 500, 100, 300, - 50 * genres.genres.len() as i32, + 50 * genres.len() as i32, match lang { Lang::English => "Select Genres", Lang::Russian => "Выбрать жанры", @@ -222,9 +222,9 @@ pub fn customize_book_genre( ); let mut genre_choice = - CheckBrowser::new(0, 0, 300, 50 * genres.genres.len() as i32, ""); + CheckBrowser::new(0, 0, 300, 50 * genres.len() as i32, ""); - genres.genres.iter().for_each(|g| unsafe { + genres.iter().for_each(|g| unsafe { genre_choice.add( g.as_str(), if let Some(gen) = &(**book_system.books.get_unchecked(index)) @@ -246,7 +246,7 @@ pub fn customize_book_genre( wind.show(); while app.wait() { - (0..genres.genres.len()).for_each(|i| { + (0..genres.len()).for_each(|i| { if genre_choice.checked(i as i32 + 1) { unsafe { if (**book_system.books.get_unchecked(index)) @@ -260,13 +260,7 @@ pub fn customize_book_genre( .as_mut() .unwrap() .insert( - genres - .genres - .iter() - .skip(i) - .next() - .unwrap() - .clone(), + genres.iter().skip(i).next().unwrap().clone(), ); } else { (**book_system.books.get_unchecked(index)) @@ -279,13 +273,7 @@ pub fn customize_book_genre( .as_mut() .unwrap() .insert( - genres - .genres - .iter() - .skip(i) - .next() - .unwrap() - .clone(), + genres.iter().skip(i).next().unwrap().clone(), ); } } @@ -303,9 +291,7 @@ pub fn customize_book_genre( .genres .as_mut() .unwrap() - .remove( - genres.genres.iter().skip(i).next().unwrap(), - ); + .remove(genres.iter().skip(i).next().unwrap()); if (**book_system.books.get_unchecked(index)) .borrow() @@ -350,7 +336,7 @@ pub fn find_by_genre_simple( ) -> Vec<(String, String, u16)> { let mut find = vec![]; - book_system.books.iter().for_each(|x| { + book_system.iter().for_each(|x| { if (**x).borrow().genres.is_some() && (**x) .borrow() @@ -417,7 +403,7 @@ fn find_by_genre(book_system: &BookSystem, app: &App, lang: Lang) { let mut book_table = Table::new(0, 0, 300, 400, ""); let mut find = vec![]; - book_system.books.iter().for_each(|x| { + book_system.iter().for_each(|x| { if (**x).borrow().genres.is_some() && (**x) .borrow() diff --git a/src/actions/giveaway/full.rs b/src/actions/giveaway/full.rs index cbb9d4d..aa99179 100644 --- a/src/actions/giveaway/full.rs +++ b/src/actions/giveaway/full.rs @@ -64,7 +64,6 @@ pub fn change_return_date( reader_base, genres, caretaker, - app, lang, ); } @@ -230,7 +229,7 @@ pub fn get_book( } None => { - caretaker.pop(); + caretaker.pop().unwrap(); return; } } @@ -238,7 +237,7 @@ pub fn get_book( } break; } else if !inp.shown() { - caretaker.pop(); + caretaker.pop().unwrap(); break; } } diff --git a/src/actions/giveaway/simple.rs b/src/actions/giveaway/simple.rs index d3ffa0c..ec0ed74 100644 --- a/src/actions/giveaway/simple.rs +++ b/src/actions/giveaway/simple.rs @@ -19,7 +19,7 @@ use fltk::{ prelude::*, }; -use chrono::Local; +use chrono::{Datelike, Local}; use fltk_calendar::calendar::Calendar; use std::{cell::RefCell, rc::Weak}; @@ -103,8 +103,7 @@ pub(crate) fn give_book_known_reader( if msg { win.hide(); - let cal = Calendar::default(); - let date = cal.get_date(); + let date = Calendar::default().get_date(); return match date { Some(date) => { @@ -611,7 +610,6 @@ pub(crate) fn get_book_known_reader( }; let bind = book_system - .books .iter() .position(|b| b.as_ptr() == book.as_ptr()) .unwrap(); @@ -775,7 +773,7 @@ fn get_book_known_reader_input( } }, ); - caretaker.pop(); + caretaker.pop().unwrap(); false } @@ -859,7 +857,7 @@ fn get_book_known_reader_input( } break; } else if !inp2.shown() { - caretaker.pop(); + caretaker.pop().unwrap(); return false; } } @@ -872,6 +870,122 @@ fn get_book_known_reader_input( #[inline] pub fn change_return_date_simple( + book_op: &Option>>, + book_system: &mut BookSystem, + reader_base: &ReaderBase, + genres: &Genres, + caretaker: &mut Caretaker, + lang: Lang, +) -> bool { + caretaker.add_memento(reader_base, book_system, genres); + + return match book_op { + None => { + alert( + 500, + 500, + match lang { + Lang::English => "This reader isn't reading anything", + Lang::Russian => "Этот читатель ни читает книгу", + }, + ); + + caretaker.pop().unwrap(); + false + } + + Some(book) => { + return match Calendar::default().get_date() { + None => { + alert( + 500, + 500, + match lang { + Lang::English => "Date wasn't selected", + Lang::Russian => "Дата не была выбрана", + }, + ); + false + } + + Some(date) => { + let new_date = Date { + day: date.day() as u8, + month: date.month() as u8, + year: date.year() as u16, + }; + + let start = ((*(*book.upgrade().unwrap()) + .borrow_mut() + .readers + .last_mut() + .unwrap()) + .1) + .0; + + if new_date >= start && new_date >= Date::from(Local::now()) { + ((*(*book.upgrade().unwrap()) + .borrow_mut() + .readers + .last_mut() + .unwrap()) + .1) + .1 = new_date; + + fltk::dialog::message( + 500, + 500, + match lang { + Lang::English => "Date is successfully changed", + Lang::Russian => "Дата успешно изменена", + }, + ); + + book_system.save(); + true + } else { + alert( + 500, + 500, + match lang { + Lang::English => { + concat!( + "Date can only be not earlier than deadline", + " and not earlier than today" + ) + } + + Lang::Russian => { + concat!( + "Дата обязана быть не позже дедлайна", + " и не позже сегоднешней даты" + ) + } + }, + ); + + caretaker.pop().unwrap(); + false + } + } + } + } + }; +} + +/// **DEPRECATED** +/// +/// Used before. Requires user input. +/// Consider using **change_return_date_simple() instead** +/// +/// Function that changes +/// return date for already known book + +#[allow(dead_code)] +#[deprecated( + note = "Used before. Requires user input. Consider using change_return_date_simple() instead" +)] +fn change_return_date_simple_input( book_op: &Option>>, book_system: &mut BookSystem, reader_base: &ReaderBase, @@ -974,15 +1088,15 @@ pub fn change_return_date_simple( match lang { Lang::English => { concat!( - "Date can only be not earlier than deadline", - " and not earlier than today" + "Date can only be not earlier than deadline", + " and not earlier than today" ) } Lang::Russian => { concat!( - "Дата обязана быть не позже дедлайна", - " и не позже сегоднешней даты" + "Дата обязана быть не позже дедлайна", + " и не позже сегоднешней даты" ) } }, diff --git a/src/actions/read/add_rem/full.rs b/src/actions/read/add_rem/full.rs index 9c08a64..80dd4fa 100644 --- a/src/actions/read/add_rem/full.rs +++ b/src/actions/read/add_rem/full.rs @@ -99,10 +99,7 @@ pub fn add_reader( if msg { win.hide(); - let cal = Calendar::default(); - let date = cal.get_date(); - - match date { + match Calendar::default().get_date() { Some(date) => { match reader_base.add_reader( unsafe { reader.get_unchecked(0).clone() }, diff --git a/src/actions/read/change/simple.rs b/src/actions/read/change/simple.rs index 35b767f..238e2fc 100644 --- a/src/actions/read/change/simple.rs +++ b/src/actions/read/change/simple.rs @@ -10,7 +10,6 @@ use crate::{ }; use fltk::{app, app::App, dialog::alert, input::Input, prelude::*}; - use fltk_calendar::calendar::Calendar; /// Function that changes @@ -384,10 +383,7 @@ pub(crate) fn change_age_simple( if msg { win.hide(); - let cal = Calendar::default(); - let date = cal.get_date(); - - return match date { + return match Calendar::default().get_date() { Some(date) => match reader_base.change_age(ind, Date::from(date)) { Ok(_) => { fltk::dialog::message( diff --git a/src/actions/read/info/full.rs b/src/actions/read/info/full.rs index 2c48384..d9f2dd9 100644 --- a/src/actions/read/info/full.rs +++ b/src/actions/read/info/full.rs @@ -60,8 +60,8 @@ pub fn reader_info( if let Some(ind) = check { reader_info_simple( ind, - &mut *(*reader_base).borrow_mut(), - &mut *(*book_system).borrow_mut(), + reader_base.clone(), + book_system.clone(), genres, caretaker, app, diff --git a/src/actions/read/info/simple.rs b/src/actions/read/info/simple.rs index a575f8a..47a9648 100644 --- a/src/actions/read/info/simple.rs +++ b/src/actions/read/info/simple.rs @@ -25,7 +25,7 @@ use fltk::{ window::SingleWindow, }; -use std::cmp::max; +use std::{cell::RefCell, cmp::max, rc::Rc}; /// Messages for info menu /// for reader_info @@ -46,8 +46,8 @@ enum MessageReader { pub fn reader_info_simple( ind: usize, - reader_base: &mut ReaderBase, - book_system: &mut BookSystem, + reader_base: Rc>, + book_system: Rc>, genres: &Genres, caretaker: &mut Caretaker, app: &App, @@ -60,9 +60,21 @@ pub fn reader_info_simple( 600, format!( "{} {} {}", - *unsafe { &(*reader_base.readers.get_unchecked(ind)).borrow().name }, - *unsafe { &(*reader_base.readers.get_unchecked(ind)).borrow().family }, - *unsafe { &(*reader_base.readers.get_unchecked(ind)).borrow().father }, + *unsafe { + &(*(*reader_base).borrow().readers.get_unchecked(ind)) + .borrow() + .name + }, + *unsafe { + &(*(*reader_base).borrow().readers.get_unchecked(ind)) + .borrow() + .family + }, + *unsafe { + &(*(*reader_base).borrow().readers.get_unchecked(ind)) + .borrow() + .father + }, ) .as_str(), ) @@ -82,7 +94,11 @@ pub fn reader_info_simple( Lang::English => "First Name", Lang::Russian => "\t\tИмя", }, - *unsafe { &(*reader_base.readers.get_unchecked(ind)).borrow().name } + *unsafe { + &(*(*reader_base).borrow().readers.get_unchecked(ind)) + .borrow() + .name + } ) .as_str(), ); @@ -98,7 +114,11 @@ pub fn reader_info_simple( Lang::English => "Second Name", Lang::Russian => "\t\tФамилия", }, - *unsafe { &(*reader_base.readers.get_unchecked(ind)).borrow().family } + *unsafe { + &(*(*reader_base).borrow().readers.get_unchecked(ind)) + .borrow() + .family + } ) .as_str(), ); @@ -114,7 +134,11 @@ pub fn reader_info_simple( Lang::English => "Middle Name", Lang::Russian => "\t\tОтчество", }, - *unsafe { &(*reader_base.readers.get_unchecked(ind)).borrow().father } + *unsafe { + &(*(*reader_base).borrow().readers.get_unchecked(ind)) + .borrow() + .father + } ) .as_str(), ); @@ -130,7 +154,11 @@ pub fn reader_info_simple( Lang::English => "Age", Lang::Russian => "\t\tВозраст", }, - *unsafe { &(*reader_base.readers.get_unchecked(ind)).borrow().age() } + *unsafe { + &(*(*reader_base).borrow().readers.get_unchecked(ind)) + .borrow() + .age() + } ) .as_str(), ); @@ -147,13 +175,13 @@ pub fn reader_info_simple( Lang::Russian => "Читается сейчас", }, if unsafe { - (**reader_base.readers.get_unchecked(ind)) + (**(*reader_base).borrow().readers.get_unchecked(ind)) .borrow() .reading .is_some() } { unsafe { - (*(**reader_base.readers.get_unchecked(ind)) + (*(**(*reader_base).borrow().readers.get_unchecked(ind)) .borrow() .reading .as_ref() @@ -161,7 +189,7 @@ pub fn reader_info_simple( .upgrade() .unwrap()) .borrow() - .to_string(book_system) + .to_string(&*(*book_system).borrow()) } } else { match lang { @@ -199,7 +227,7 @@ pub fn reader_info_simple( let mut table2 = Table::new(0, 127, 570, 600, ""); table2.set_rows(max(30, unsafe { - (**reader_base.readers.get_unchecked(ind)) + (**(*reader_base).borrow().readers.get_unchecked(ind)) .borrow() .books .len() as u32 @@ -305,8 +333,8 @@ pub fn reader_info_simple( wind.show(); - let base_ptr = reader_base as *mut ReaderBase; - let sys_ptr = book_system as *mut BookSystem; + let rb = reader_base.clone(); + let bs = book_system.clone(); table2.draw_cell2(move |t, ctx, row, col, x, y, w, h| match ctx { fltk::table::TableContext::StartPage => draw::set_font(Font::Helvetica, 14), @@ -347,13 +375,7 @@ pub fn reader_info_simple( fltk::table::TableContext::Cell => draw_data( &format!( "{}", - cell_book2( - col, - row, - ind, - unsafe { base_ptr.as_ref().unwrap() }, - unsafe { sys_ptr.as_ref().unwrap() } - ) + cell_book2(col, row, ind, &*(*rb).borrow(), &*(*bs).borrow()) ), x, y, @@ -372,8 +394,8 @@ pub fn reader_info_simple( MessageReader::ChangeName => { if let Some(new_name) = change_name_simple( ind, - reader_base, - book_system, + &mut *(*reader_base).borrow_mut(), + &mut *(*book_system).borrow_mut(), genres, caretaker, app, @@ -397,8 +419,8 @@ pub fn reader_info_simple( MessageReader::ChangeFamily => { if let Some(new_family) = change_family_simple( ind, - reader_base, - book_system, + &mut *(*reader_base).borrow_mut(), + &mut *(*book_system).borrow_mut(), genres, caretaker, app, @@ -422,8 +444,8 @@ pub fn reader_info_simple( MessageReader::ChangeFather => { if let Some(new_father) = change_father_simple( ind, - reader_base, - book_system, + &mut *(*reader_base).borrow_mut(), + &mut *(*book_system).borrow_mut(), genres, caretaker, app, @@ -447,8 +469,8 @@ pub fn reader_info_simple( MessageReader::ChangeAge => { if let Some(new_age) = change_age_simple( ind, - reader_base, - book_system, + &mut *(*reader_base).borrow_mut(), + &mut *(*book_system).borrow_mut(), genres, caretaker, app, @@ -472,8 +494,8 @@ pub fn reader_info_simple( MessageReader::GiveBook => { if let Some(book) = give_book_known_reader( ind, - reader_base, - book_system, + &mut *(*reader_base).borrow_mut(), + &mut *(*book_system).borrow_mut(), genres, caretaker, app, @@ -496,8 +518,14 @@ pub fn reader_info_simple( } MessageReader::GetBook => { - if get_book_known_reader(ind, reader_base, book_system, genres, caretaker, lang) - { + if get_book_known_reader( + ind, + &mut *(*reader_base).borrow_mut(), + &mut *(*book_system).borrow_mut(), + genres, + caretaker, + lang, + ) { reading_frame.set_label( format!( "{}: {}", @@ -518,7 +546,14 @@ pub fn reader_info_simple( } MessageReader::RemoveThis => { - remove_reader_simple(ind, reader_base, book_system, genres, caretaker, lang); + remove_reader_simple( + ind, + &mut *(*reader_base).borrow_mut(), + &mut *(*book_system).borrow_mut(), + genres, + caretaker, + lang, + ); return; } } @@ -527,7 +562,7 @@ pub fn reader_info_simple( } let len = unsafe { - (**reader_base.readers.get_unchecked(ind)) + (**(*reader_base).borrow().readers.get_unchecked(ind)) .borrow() .books .len() @@ -542,13 +577,13 @@ pub fn reader_info_simple( unsafe { book_info_simple( Some( - (**reader_base.readers.get_unchecked(ind)) + (**(*reader_base).borrow().readers.get_unchecked(ind)) .borrow() .books .get_unchecked(i) .clone(), ), - book_system, + &*(*book_system).borrow(), lang, ); } diff --git a/src/actions/read/utils.rs b/src/actions/read/utils.rs index 3e45f01..1e271b6 100644 --- a/src/actions/read/utils.rs +++ b/src/actions/read/utils.rs @@ -101,10 +101,7 @@ pub(crate) fn check_reader( if msg { win.hide(); - let cal = Calendar::default(); - let date = cal.get_date(); - - match date { + match Calendar::default().get_date() { Some(date) => reader_base.find_reader( unsafe { &reader.get_unchecked(0) }, unsafe { &reader.get_unchecked(1) }, diff --git a/src/actions/tables.rs b/src/actions/tables.rs index 8564237..704f6a8 100644 --- a/src/actions/tables.rs +++ b/src/actions/tables.rs @@ -78,7 +78,7 @@ pub fn cell_reader( book_system: &BookSystem, lang: Lang, ) -> (String, Option) { - return if y < reader_base.readers.len() as i32 { + return if y < reader_base.len() as i32 { let reader_date; match RefCell::borrow(unsafe { &(**reader_base.readers.get_unchecked(y as usize)) }).reading @@ -393,7 +393,7 @@ pub fn cell_reader2(x: i32, y: i32, book: Weak>) -> String { pub fn cell_book(x: i32, y: i32, book_system: &BookSystem) -> String { return format!( "{}", - if y < book_system.books.len() as i32 { + if y < book_system.len() as i32 { match x { 0 => RefCell::borrow(unsafe { &(**book_system.books.get_unchecked(y as usize)) }) .title @@ -572,7 +572,7 @@ pub(crate) fn cell_genre(x: i32, book: &Rc>, lang: Lang) -> Str fn cell_genre2(y: i32, genres: &Genres) -> String { return format!( "{}", - if y < genres.genres.len() as i32 { + if y < genres.len() as i32 { genres .genres .iter() diff --git a/src/books/book_sys.rs b/src/books/book_sys.rs index c852606..4560508 100644 --- a/src/books/book_sys.rs +++ b/src/books/book_sys.rs @@ -28,6 +28,7 @@ use yaml_rust::{ /// Reader Base structure, /// which contains only Book interfaces +#[derive(Default)] pub struct BookSystem { pub(crate) books: Vec>>, } @@ -42,7 +43,6 @@ impl Debug for BookSystem { .field( "books", &self - .books .iter() .map(|x| format!("{:?}", *(**x).borrow())) .collect::>(), @@ -89,12 +89,19 @@ impl BookSystem { self.books.iter() } + /// Amount of books + + #[inline] + pub(crate) fn len(&self) -> usize { + self.books.len() + } + /// Finds The Book. /// If book is not found, it' ll return TheBooks amount #[inline] pub(crate) fn find_book(&self, title: &String, author: &String, pages: u16) -> Option { - self.books.iter().position(|x| { + self.iter().position(|x| { (**x).borrow().title == *title && (**x).borrow().author == *author && (**x).borrow().pages == pages @@ -132,7 +139,7 @@ impl BookSystem { app: &App, lang: Lang, ) -> ResultSelf { - return if ind >= self.books.len() { + return if ind >= self.len() { Err(1) // out of range } else { unsafe { @@ -207,7 +214,7 @@ impl BookSystem { #[inline] pub(crate) fn remove_one_book(&mut self, ind: usize, rind: usize) -> ResultSelf { - return if ind >= self.books.len() { + return if ind >= self.len() { Err(0) // search ind (TheBook) out of range } else { unsafe { @@ -241,7 +248,7 @@ impl BookSystem { #[inline] pub(crate) fn remove_book(&mut self, ind: usize) -> ResultSelf { - return if ind >= self.books.len() { + return if ind >= self.len() { Err(0) // out of range } else { Ok(unsafe { self.remove_book_unchecked(ind) }) @@ -342,30 +349,28 @@ impl BookSystem { #[inline] pub(crate) fn change_pages(&mut self, ind: usize, new_pages: String) -> ResultSelf { - let new_pages_num; - - match new_pages.trim().parse::() { - Ok(x) => new_pages_num = x, - Err(_) => return Err(0), // parse error - } - - return if ind == self.books.len() { - Err(1) // not found - } else { - unsafe { - if self - .find_book( - &RefCell::borrow(&(**self.books.get_unchecked(ind))).title, - &RefCell::borrow(&(**self.books.get_unchecked(ind))).author, - new_pages_num, - ) - .is_some() - { - Err(2) // already exists + return match new_pages.trim().parse::() { + Ok(new_pages_num) => { + if ind == self.len() { + Err(1) // not found } else { - Ok(self.change_pages_unchecked(ind, new_pages_num)) + unsafe { + if self + .find_book( + &RefCell::borrow(&(**self.books.get_unchecked(ind))).title, + &RefCell::borrow(&(**self.books.get_unchecked(ind))).author, + new_pages_num, + ) + .is_some() + { + Err(2) // already exists + } else { + Ok(self.change_pages_unchecked(ind, new_pages_num)) + } + } } } + Err(_) => Err(0), // parse error }; } diff --git a/src/books/genres.rs b/src/books/genres.rs index be13457..f6ca278 100644 --- a/src/books/genres.rs +++ b/src/books/genres.rs @@ -109,6 +109,13 @@ impl Genres { self.genres.iter() } + /// Amount of genres + + #[inline] + pub fn len(&self) -> usize { + self.genres.len() + } + /// Adds new genre to set. /// If this genre is already exists, /// it will return false diff --git a/src/main.rs b/src/main.rs index f30fd58..cc11fd1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -143,55 +143,42 @@ fn main() -> Result<(), Box> { while app.wait() { if let Some(msg) = r.recv() { - match msg { - true => { - password.hide(); - - if let Ok(data) = password.set_input() { - if !data.first().unwrap().is_ascii() - || !data.last().unwrap().is_ascii() - { - alert( - 500, - 500, - "Incorrect Password. You must use only English letters. Try again", - ); - - success = 2; - } else { - let hash1 = - get_hash(&data.first().unwrap(), 97, 1e9 as u128 + 7); - let hash2 = - get_hash(&data.last().unwrap(), 101, 1e9 as u128 + 7); - - File::create("src/utils/admin.bin").unwrap().write( - format!( - "{}", - hash1 + if msg { + password.hide(); + + if let Ok(data) = password.set_input() { + if !data.first().unwrap().is_ascii() || !data.last().unwrap().is_ascii() + { + alert( + 500, + 500, + "Incorrect Password. You must use only English letters. Try again", + ); + + success = 2; + } else { + let hash1 = get_hash(&data.first().unwrap(), 97, 1e9 as u128 + 7); + let hash2 = get_hash(&data.last().unwrap(), 101, 1e9 as u128 + 7); + + File::create("src/utils/admin.bin").unwrap().write( + format!( + "{}", + hash1.iter().map(|x| *x as u8 as char).collect::() + + "\0" + + hash2 .iter() .map(|x| *x as u8 as char) .collect::() - + "\0" - + hash2 - .iter() - .map(|x| *x as u8 as char) - .collect::() - .as_str() - ) - .as_bytes(), - )?; - - fltk::dialog::message( - 500, - 500, - "New login and password are saved", - ); - success = 1; - break; - } + .as_str() + ) + .as_bytes(), + )?; + + fltk::dialog::message(500, 500, "New login and password are saved"); + success = 1; + break; } } - false => (), } } } @@ -227,49 +214,45 @@ fn main() -> Result<(), Box> { while app.wait() { if let Some(msg) = r.recv() { - match msg { - true => { - let input = password.set_input(); - password.hide(); - - if let Ok(data) = input { - let hash1 = get_hash(&data.first().unwrap(), 97, 1e9 as u128 + 7); - let hash2 = get_hash(&data.last().unwrap(), 101, 1e9 as u128 + 7); - - let rehash1 = - hash1.iter().map(|x| *x as u8 as char).collect::(); - let rehash2 = - hash2.iter().map(|x| *x as u8 as char).collect::(); - - if format!("{}", rehash1) - == format!("{}", admin_data.first().unwrap()) - && format!("{}", rehash2) - == format!("{}", *admin_data.last().unwrap()) - { - fltk::dialog::message( - 500, - 500, - match lang { - Lang::English => "Authorization is complete", - Lang::Russian => "Авторизация пройдена", - }, - ); - success = 1; - break; - } else { - success = 2; - alert( - 500, - 500, - match lang { - Lang::English => "Wrong login or password. Try again", - Lang::Russian => "Неправильный логин или пароль", - }, - ); - } + if msg { + let input = password.set_input(); + password.hide(); + + if let Ok(data) = input { + let hash1 = get_hash(&data.first().unwrap(), 97, 1e9 as u128 + 7); + let hash2 = get_hash(&data.last().unwrap(), 101, 1e9 as u128 + 7); + + let rehash1 = + hash1.iter().map(|x| *x as u8 as char).collect::(); + let rehash2 = + hash2.iter().map(|x| *x as u8 as char).collect::(); + + if format!("{}", rehash1) == format!("{}", admin_data.first().unwrap()) + && format!("{}", rehash2) + == format!("{}", *admin_data.last().unwrap()) + { + fltk::dialog::message( + 500, + 500, + match lang { + Lang::English => "Authorization is complete", + Lang::Russian => "Авторизация пройдена", + }, + ); + success = 1; + break; + } else { + success = 2; + alert( + 500, + 500, + match lang { + Lang::English => "Wrong login or password. Try again", + Lang::Russian => "Неправильный логин или пароль", + }, + ); } } - false => (), } } } @@ -1078,8 +1061,8 @@ fn main() -> Result<(), Box> { if table.is_selected(i as i32, 0) { reader_info_simple( i, - &mut (*reader_base).borrow_mut(), - &mut (*book_system).borrow_mut(), + reader_base.clone(), + book_system.clone(), &(*genres).borrow(), &mut (*caretaker).borrow_mut(), &app, @@ -1104,7 +1087,6 @@ fn main() -> Result<(), Box> { &(*reader_base).borrow(), &(*genres).borrow(), &mut (*caretaker).borrow_mut(), - &app, lang, ); diff --git a/src/reading/read_base.rs b/src/reading/read_base.rs index dbf61a1..6581b02 100644 --- a/src/reading/read_base.rs +++ b/src/reading/read_base.rs @@ -33,7 +33,6 @@ impl Debug for ReaderBase { .field( "readers", &self - .readers .iter() .map(|x| format!("{:?}", *(**x).borrow())) .collect::>(), @@ -50,7 +49,6 @@ impl Clone for ReaderBase { fn clone(&self) -> Self { ReaderBase { readers: self - .readers .iter() .map(|x| Rc::new(RefCell::new((**x).borrow().clone()))) .collect(), @@ -92,11 +90,17 @@ impl ReaderBase { /// Iterate on Book System with smart pointers of The Book #[inline] - #[allow(dead_code)] pub(crate) fn iter(&self) -> std::slice::Iter>> { self.readers.iter() } + /// Returns amount of readers + + #[inline] + pub fn len(&self) -> usize { + self.readers.len() + } + /// Searches reader by his params. /// If ok returns index, /// else returns amount of readers @@ -109,7 +113,7 @@ impl ReaderBase { father: &String, birth: Date, ) -> Option { - self.readers.iter().position(|x| { + self.iter().position(|x| { (**x).borrow().name == *name && (**x).borrow().family == *family && (**x).borrow().father == *father @@ -173,7 +177,7 @@ impl ReaderBase { #[inline] pub(crate) fn remove_reader(&mut self, ind: usize) -> ResultSelf { - return if ind >= self.readers.len() { + return if ind >= self.len() { Err(0) // out of range } else { Ok(unsafe { self.remove_reader_unchecked(ind) }) @@ -202,7 +206,7 @@ impl ReaderBase { #[inline] pub(crate) fn change_name(&mut self, ind: usize, new_name: String) -> ResultSelf { - return if ind >= self.readers.len() { + return if ind >= self.len() { Err(0) // out of range } else { unsafe { @@ -245,7 +249,7 @@ impl ReaderBase { #[inline] pub(crate) fn change_family(&mut self, ind: usize, new_family: String) -> ResultSelf { - return if ind >= self.readers.len() { + return if ind >= self.len() { Err(0) // out of range } else { unsafe { @@ -288,7 +292,7 @@ impl ReaderBase { #[inline] pub(crate) fn change_father(&mut self, ind: usize, new_father: String) -> ResultSelf { - return if ind >= self.readers.len() { + return if ind >= self.len() { Err(0) // out of range } else { unsafe { @@ -326,7 +330,7 @@ impl ReaderBase { #[inline] pub(crate) fn change_age(&mut self, ind: usize, new_birth: Date) -> ResultSelf { - return if ind >= self.readers.len() { + return if ind >= self.len() { Err(0) // out of range } else { unsafe { @@ -347,13 +351,6 @@ impl ReaderBase { }; } - /// Returns amount of readers - - #[inline] - pub fn len(&self) -> usize { - self.readers.len() - } - /// Returns book (or None) which is read by reader now #[inline] @@ -376,7 +373,7 @@ impl ReaderBase { pub(crate) fn save(&self) { let mut array = yaml_rust::yaml::Array::new(); - (0..self.readers.len()).for_each(|guy| { + (0..self.len()).for_each(|guy| { let mut data = Hash::new(); unsafe { @@ -524,13 +521,7 @@ impl ReaderBase { if d["Reading"].as_str().unwrap() == "None" { None } else { - Some(Rc::downgrade(&Rc::new(RefCell::new(Book { - the_book: None, - is_using: false, - cabinet: 0, - shelf: 0, - readers: vec![], - })))) + Some(Rc::downgrade(&Rc::new(RefCell::new(Book::default())))) } }); } diff --git a/src/reading/reader.rs b/src/reading/reader.rs index 02b637d..447a4e1 100644 --- a/src/reading/reader.rs +++ b/src/reading/reader.rs @@ -1,5 +1,3 @@ -extern crate chrono; - use crate::books::{book::Book, date::Date, ResultSelf}; use std::{