Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/rust_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Rust Lint & Format

on:
push:
branches: [ "main" ]
branches: [ "main", "class-implementations" ] # Added new branch
pull_request:
branches: [ "main" ]
branches: [ "main", "class-implementations" ] # Added new branch

env:
CARGO_TERM_COLOR: always
Expand Down
6 changes: 3 additions & 3 deletions Block 1/Challenge 5/rev_eng/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ Add gitignore to top level of repository for general things and a new one within
- **`read_string()`** – ***NEW*** function which helps to read full lines of text, sanitizing for empty inputs and fixing the behavior presented by `cin >> str` within `Block 1/Challenge 5/buggy_src/Buggy.cpp` that caused only the first word of the title to be read

## Step 8: Implementation of the Data Models for Book, User, and BookReadingSession
- **`Book`**: A public struct containing the author, isbn title, and page contents.
- **`Book`**: A public struct containing the author, isbn title, and page contents. The class implementation in `Block 1/Challenge 5/rev_eng/src/models.rs` is just a constructor. I Will implement a manager within another file

- **`User`**: A public struct containing the admin status, email, name, password, and username.
- **`User`**: A public struct containing the admin status, email, name, password, and username. This constructs a new user, defaulting admin status to false and starting a reading session by being empty. Including the ability to add a session.


- **`BookReadingSession`**: A public struct containing the book isbn, current page, and last access date.
- **`BookReadingSession`**: A public struct containing the book isbn, current page, and last access date. This receives a critical safety update, instead of owning the book pointer it instead now just borrows the book.

Empty file.
4 changes: 3 additions & 1 deletion Block 1/Challenge 5/rev_eng/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod books_manager;
mod models;
mod utils;
mod users_manager;

fn main() {
println!("Hello, world!");
}
}
109 changes: 107 additions & 2 deletions Block 1/Challenge 5/rev_eng/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,116 @@ pub struct User {
pub name: String,
pub email: String,
pub is_admin: bool,

// store the actual reading session rather than a pointer to prevent memory leaks.
pub reading_sessions: Vec<BookReadingSession>,
}

// This replaces the C++ BookReadingSession class.
=pub struct BookReadingSession {
pub struct BookReadingSession {
// store the book's ACTUAL ISBN rather than a pointer to prevent memory leaks.
pub book_isbn: String,
pub current_page: usize,
pub last_access_date: String,
}
}

impl Book {
// Constructor for a Book
pub fn new(isbn: String, title: String, author: String, pages: Vec<String>) -> Self {
Self {
isbn,
title,
author,
pages,
}
}

// replaces the C++ Book::toString() method
pub fn to_string(&self) -> &str {
&self.title
}
}

impl User {
// Constructor for a user
pub fn new(
username: String,
password: String,
name: String,
email: String,
is_admin: bool,
) -> Self {
Self {
username,
password,
name,
email,
is_admin: false, // Default to not an admin
reading_sessions: Vec::new(), // Empty to start
}
}

// replaces the C++ User::toString() method
pub fn to_string(&self) -> String {
let admin_str = if self.is_admin { " | Admin" } else { "" };
format!(
"Name: {}\nEmail: {}\nUsername: {}{}",
self.name, self.email, self.username, admin_str
)
}
pub fn add_reading_session(&mut self, book: &Book) -> &mut BookReadingSession {
let session = BookReadingSession::new(book.isbn.clone());
self.reading_sessions.push(session);
// Return a mutable reference to the session that was added
self.reading_sessions.last_mut().unwrap()
}
}

impl BookReadingSession {
// Constructor for a new reading session
pub fn new(book_isbn: String) -> Self {
Self {
book_isbn,
current_page: 0,
last_access_date: utils::get_current_time_and_date(),
}
}

// Replicates C++ PageIdxStr() Method
pub fn page_idx_str(&self, book: &Book) -> String {
format!("{}/{}", self.current_page + 1, book.pages.len())
}

// Getter for content of page, rather than owning a pointer it just borrows the book
pub fn get_page_content<'a>(&self, book: &'a Book) -> &'a str {
&book.pages[self.current_page]
}

// Next page function replication:
pub fn next_page(&mut self, book: &Book) {
if self.current_page < book.pages.len() - 1 {
self.current_page += 1;
}
}

// replication of previous page funtion
pub fn previous_page(&mut self) {
if self.current_page > 0 {
self.current_page -= 1;
}
}

// Method to reset last access date to current date
pub fn reset_last_access_date(&mut self) {
self.last_access_date = utils::get_current_time_and_date();
}

pub fn to_string(&self, book: &Book) -> String {
format!(
"{} Page: {} - {}",
book.to_string(),
self.page_idx_str(book),
self.last_access_date
)
}
}
Empty file.
2 changes: 1 addition & 1 deletion Block 1/Challenge 5/rev_eng/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use chrono::prelude::*;
pub fn read_int(low: i32, high: i32) -> i32 {
if low > high {
// changed to a panic, since this is a programming error, not a user input error.
= panic!(
panic!(
"Invalid range: low ({}) is greater than high ({})",
low, high
);
Expand Down
Loading