Skip to content

Commit

Permalink
added more lints and lots of missing documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
worldofjoni committed Sep 28, 2023
1 parent ec7474a commit 7d1f20e
Show file tree
Hide file tree
Showing 51 changed files with 164 additions and 53 deletions.
1 change: 0 additions & 1 deletion backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ colored = "2.0.4"
image = "0.24.0"

[dev-dependencies]
serde_json = "1.0"
serial_test = "2.0.0"
rand = "0.8.0"
tracing-test = "0.2.4"
Expand Down
1 change: 1 addition & 0 deletions backend/src/interface/api_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::util::{ReportReason, Uuid};

use super::persistent_data::DataError;

/// Result returned from commands, potentially containing a [`CommandError`].
pub type Result<T> = std::result::Result<T, CommandError>;

/// Interface for accessing commands which can be triggered by an API.
Expand Down
1 change: 1 addition & 0 deletions backend/src/interface/file_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use thiserror::Error;

use crate::util::{ImageResource, Uuid};

/// Result returned from file operations, potentially containing a [`FileError`].
pub type Result<T> = std::result::Result<T, FileError>;

/// This interface allows to store images as file.
Expand Down
2 changes: 2 additions & 0 deletions backend/src/interface/image_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::util::ImageResource;
use async_trait::async_trait;
use thiserror::Error;

/// Result returned from image validation operations, potentially containing a [`ImageError`].
pub type Result<T> = std::result::Result<T, ImageError>;

/// This interface allows to validate an image by checking whether it does contain inappropriate content.
Expand All @@ -17,6 +18,7 @@ pub trait ImageValidation: Send + Sync {
/// Enum describing possible ways an image validation can go wrong
#[derive(Debug, Error)]
pub enum ImageError {
/// Error returned when an image contains invalid content.
#[error("This image contains content that is not permitted: {0}")]
InvalidContent(String),
// TODO
Expand Down
1 change: 1 addition & 0 deletions backend/src/interface/mealplan_management.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! This interface allows starting the operations for updating the menu from the the canteen's website.
use async_trait::async_trait;

/// Interface allowing to start the operations for updating the menu from the the canteen's website.
#[async_trait]
pub trait MensaParseScheduling: Send + Sync {
/// Initiate the parsing procedure of the canteen-website.
Expand Down
12 changes: 9 additions & 3 deletions backend/src/interface/mensa_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ use crate::{interface::mensa_parser::model::ParseCanteen, util::Date};
use async_trait::async_trait;
use thiserror::Error;

/// Result returned from parse operations, potentially containing a [`ParseError`].
pub type Result<T> = std::result::Result<T, ParseError>;

/// Error indicating that something went wrong while parsing.
#[derive(Debug, Error)]
pub enum ParseError {
/// A html node was expected but not found in the document.
#[error("the node was not found: {0}")]
InvalidHtmlDocument(String),
#[error("no connection could be established")]
/// No connection to the meal plan webpage could be established
#[error("no connection could be established: {0}")]
NoConnectionEstablished(String),
#[error("some html code couldn't be decoded")]
/// Html document could not be decoded.
#[error("some html code couldn't be decoded: {0}")]
DecodeFailed(String),
#[error("the html reqwest client creation failed")]
/// Could not build client for making web requests.
#[error("the html reqwest client creation failed: {0}")]
ClientBuilderFailed(String),
}

Expand Down
1 change: 1 addition & 0 deletions backend/src/interface/persistent_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use sqlx::migrate::MigrateError;
use std::num::TryFromIntError;
use thiserror::Error;

/// Result returned from data access operations, potentially containing a [`DataError`].
pub type Result<T> = std::result::Result<T, DataError>;

/// Enumerations for possible data request faults
Expand Down
2 changes: 2 additions & 0 deletions backend/src/layer/data/database/command.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Module responsible for handling database requests for commands.
use async_trait::async_trait;
use sqlx::{Pool, Postgres};

Expand All @@ -11,6 +12,7 @@ use crate::{
};

/// Class implementing all database requests arising from graphql manipulations.
#[derive(Debug)]
pub struct PersistentCommandData {
pub(super) pool: Pool<Postgres>,
}
Expand Down
4 changes: 4 additions & 0 deletions backend/src/layer/data/database/factory.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Module containing a factory pattern to construct instances to access the database for all components needing it.
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
use tracing::info;

Expand All @@ -9,6 +10,7 @@ use super::{
};

/// Structure containing all information necessary to connect to a database.
#[derive(Debug)]
pub struct DatabaseInfo {
/// Connection string to database of format `postgres://<username>:<password>@<host>:<port>/<database>`.
pub connection: String,
Expand All @@ -17,8 +19,10 @@ pub struct DatabaseInfo {
}

/// This class is responsible for instantiating the database access implementations classes.
#[derive(Debug)]
pub struct DataAccessFactory {
pool: Pool<Postgres>,
/// Number of weeks meal plan data will be available in the database.
pub max_weeks_data: u32,
}

Expand Down
3 changes: 3 additions & 0 deletions backend/src/layer/data/database/mealplan_management.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Module responsible for handling database requests for meal plan management operations.
use async_trait::async_trait;
use sqlx::{Pool, Postgres};

Expand All @@ -6,6 +7,8 @@ use crate::{
util::{Additive, Allergen, Date, MealType, Price, Uuid},
};

/// Class for performing database operations necessary for meal plan management.
#[derive(Debug)]
pub struct PersistentMealplanManagementData {
pub(super) pool: Pool<Postgres>,
}
Expand Down
1 change: 1 addition & 0 deletions backend/src/layer/data/database/request.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Module responsible for handling database requests for api requests.
use async_trait::async_trait;
use chrono::{Duration, Local};
use sqlx::{Pool, Postgres};
Expand Down
2 changes: 2 additions & 0 deletions backend/src/layer/data/database/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use heck::AsShoutySnakeCase;
use crate::util::{Additive, Allergen};

impl Allergen {
/// Converts this instance into its database string representation.
#[must_use]
pub fn to_db_string(self) -> String {
format!("{}", AsShoutySnakeCase(format!("{self:?}")))
}
}

impl Additive {
/// Converts this instance into its database string representation.
#[must_use]
pub fn to_db_string(self) -> String {
format!("{}", AsShoutySnakeCase(format!("{self:?}")))
Expand Down
4 changes: 4 additions & 0 deletions backend/src/layer/data/mail/mail_info.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//! See [`MailInfo`].

/// Information necessary to send email notifications to an administrator.
#[derive(Debug)]
pub struct MailInfo {
/// Name of the domain of the connection to the mail server over SMTP
pub smtp_server: String,
Expand Down
10 changes: 8 additions & 2 deletions backend/src/layer/data/mail/mail_sender.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! Module responsible for sending email notifications to administrators.
use std::fmt::Debug;

use async_trait::async_trait;
use thiserror::Error;

Expand All @@ -14,6 +17,7 @@ use crate::{
use string_template::Template;
use tracing::{error, info};

/// Result returned when sending emails, potentially containing a [`MailError`].
pub type MailResult<T> = std::result::Result<T, MailError>;

const REPORT_TEMPLATE: &str = include_str!("./template.txt");
Expand All @@ -24,16 +28,18 @@ const MAIL_SUBJECT: &str = "An image was reported and requires your review";
/// Enum describing the possible ways, the mail notification can fail.
#[derive(Debug, Error)]
pub enum MailError {
/// Error occurring when an email address could not be parsed.
#[error("an error occurred while parsing the addresses: {0}")]
AddressError(#[from] AddressError),
#[error("an error occurred while reading the template: {0}")]
TemplateError(#[from] std::io::Error),
/// Error occurring when an email could not be constructed.
#[error("an error occurred while parsing the mail: {0}")]
MailParseError(#[from] lettre::error::Error),
/// Error occurring when mail sender instance could bot be build.
#[error("an error occurred while sending the mail: {0}")]
MailSendError(#[from] lettre::transport::smtp::Error),
}

/// Class for sending emails.
pub struct MailSender {
config: MailInfo,
mailer: SmtpTransport,
Expand Down
1 change: 1 addition & 0 deletions backend/src/layer/data/swka_parser/html_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const INVALID_ROOT_NODE_MESSAGE: &str =
"could not find mensa root node. this could mean a wrong webpage got loaded";

/// A static class, that transforms html files into datatypes, that can be used for further processing using the `HTMLParser::transform` function.
#[derive(Debug)]
pub struct HTMLParser;

impl HTMLParser {
Expand Down
4 changes: 4 additions & 0 deletions backend/src/layer/data/swka_parser/parsing_helper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! This module contains some helper functions for parsing parts of the meal plan.
use crate::util::{Additive, Allergen, MealType};

impl Allergen {
/// Parses an allergen from its representation in the meal plan.
#[must_use]
pub fn parse(s: &str) -> Option<Self> {
match s {
Expand Down Expand Up @@ -38,6 +40,7 @@ impl Allergen {
}

impl Additive {
/// Parses an additive from its representation in the meal plan.
#[must_use]
pub fn parse(s: &str) -> Option<Self> {
match s {
Expand All @@ -62,6 +65,7 @@ impl Additive {
}

impl MealType {
/// Parses a meal's type from its representation in the meal plan.
#[must_use]
pub fn parse(s: &str) -> Self {
match s {
Expand Down
2 changes: 2 additions & 0 deletions backend/src/layer/data/swka_parser/swka_html_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use reqwest::Client;
use std::time::Duration;
use tracing::trace;

/// Class for requesting the meal plan's webpage.
#[derive(Debug)]
pub struct SwKaHtmlRequest {
client: Client,
}
Expand Down
2 changes: 2 additions & 0 deletions backend/src/layer/data/swka_parser/swka_link_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use crate::util::Date;
const URL_SEPARATOR: char = '/';
const WEEK_SELECTOR: &str = "?kw=";

/// Class for creating the URLs necessary to request the meal plans.
#[derive(Debug)]
pub struct SwKaLinkCreator {
base_url: String,
valid_canteens: Vec<String>,
Expand Down
8 changes: 8 additions & 0 deletions backend/src/layer/data/swka_parser/swka_parse_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@ use crate::util::Date;
use async_trait::async_trait;
use std::collections::HashMap;

/// Struct containing all information necessary to create a [`SwKaParseManager`].
#[derive(Debug, Clone)]
pub struct SwKaInfo {
/// Base url under which the canteens' meal plans are available.
pub base_url: String,
/// Canteen slugs to be appended to the base url.
pub valid_canteens: Vec<String>,
/// Timeout when making web requests.
pub client_timeout: std::time::Duration,
/// User agent string when making web request.
pub client_user_agent: String,
/// Number of weeks in the future to request data for on full parsing.
pub number_of_weeks_to_poll: u32,
}

/// Class for managing the request and parsing of meal plans.
#[derive(Debug)]
pub struct SwKaParseManager {
link_creator: SwKaLinkCreator,
request: SwKaHtmlRequest,
Expand Down
14 changes: 7 additions & 7 deletions backend/src/layer/data/swka_parser/test/const_test_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::layer::data::swka_parser::swka_parse_manager::SwKaInfo;
use std::time::Duration;

#[must_use]
pub fn get_mensa_names() -> Vec<String> {
pub(in super::super) fn get_mensa_names() -> Vec<String> {
vec![
String::from("mensa_adenauerring"),
String::from("mensa_gottesaue"),
Expand All @@ -19,22 +19,22 @@ pub fn get_mensa_names() -> Vec<String> {
}

#[must_use]
pub fn get_base_url() -> String {
pub(in super::super) fn get_base_url() -> String {
String::from("https://www.sw-ka.de/de/hochschulgastronomie/speiseplan/")
}

#[must_use]
pub const fn get_client_timeout() -> Duration {
pub(in super::super) const fn get_client_timeout() -> Duration {
Duration::from_millis(6000)
}

#[must_use]
pub fn get_client_user_agent() -> String {
pub(in super::super) fn get_client_user_agent() -> String {
String::from("User-Agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36")
}

#[must_use]
pub fn get_parse_info() -> SwKaInfo {
pub(in super::super) fn get_parse_info() -> SwKaInfo {
SwKaInfo {
base_url: get_base_url(),
valid_canteens: get_mensa_names(),
Expand All @@ -45,11 +45,11 @@ pub fn get_parse_info() -> SwKaInfo {
}

#[must_use]
pub fn get_creator() -> SwKaLinkCreator {
pub(in super::super) fn get_creator() -> SwKaLinkCreator {
SwKaLinkCreator::new(get_base_url(), get_mensa_names(), 5)
}

#[must_use]
pub fn get_request() -> SwKaHtmlRequest {
pub(in super::super) fn get_request() -> SwKaHtmlRequest {
SwKaHtmlRequest::new(get_client_timeout(), get_client_user_agent()).unwrap()
}
2 changes: 1 addition & 1 deletion backend/src/layer/data/swka_parser/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#[cfg(test)]
pub mod const_test_data;
pub(super) mod const_test_data;
2 changes: 2 additions & 0 deletions backend/src/layer/logic/api_command/auth/authenticator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Module responsible for authenticating api commands by verifying hashes over the api key and data.
use base64::{engine::general_purpose::STANDARD, Engine};

use sha2::{Digest, Sha512};
Expand All @@ -7,6 +8,7 @@ use crate::interface::api_command::{CommandError, InnerAuthInfo, Result};
use super::command_type::CommandType;

/// Class for authenticating commands.
#[derive(Debug)]
pub struct Authenticator {
api_keys: Vec<String>,
}
Expand Down
Loading

0 comments on commit 7d1f20e

Please sign in to comment.