Skip to content

Commit

Permalink
Merge pull request #89 from h1alexbel/84-missing-docs
Browse files Browse the repository at this point in the history
feat(#84): deny on missing_docs, routes -> handlers
  • Loading branch information
h1alexbel authored Jul 21, 2024
2 parents 6932d8b + 6e382e6 commit 977aab6
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 44 deletions.
3 changes: 3 additions & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ authors = ["Aliaksei Bialíauski <[email protected]>", "Ivanchuk Ivan
[lib]
path = "src/lib.rs"

[lints.rust]
missing_docs = "deny"

[dependencies]
openapi = { path = "../github-mirror" }
anyhow = "1.0.86"
Expand Down
7 changes: 2 additions & 5 deletions server/src/routes/home.rs → server/src/handlers/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ use log::debug;
use crate::ServerConfig;
use openapi::models::MetaRoot200Response;

// @todo #79:45min Make mod/fn/struct documentation required.
// Let's configure cargo in such way, so documentation will be required at all
// levels. When such rule will be configured, please fix all warnings/errors
// and integrate changes into master branch.
/// Home handler.
pub async fn home(State(config): State<ServerConfig>) -> impl IntoResponse {
let response = Json(
MetaRoot200Response::new(
Expand Down Expand Up @@ -191,7 +188,7 @@ pub async fn home(State(config): State<ServerConfig>) -> impl IntoResponse {

#[cfg(test)]
mod tests {
use crate::routes::home::home;
use crate::handlers::home::home;
use crate::ServerConfig;
use anyhow::Result;
use axum::body::to_bytes;
Expand Down
3 changes: 2 additions & 1 deletion server/src/routes/mod.rs → server/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/// Home handler.
pub mod home;
/// Handler for internal user registration.
pub mod register_user;
pub mod rs_err;
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ use axum::Json;

use crate::objects::user::User;

/// Register user.
///
/// # Fields
///
/// * `payload`: JSON payload
///
/// # Examples
///
/// ```
/// use axum::Json;
/// use server::handlers::register_user::register_user;
/// use server::objects::user::User;
/// let registration = register_user(Json(User::new(String::from("jeff"))));
/// ```
pub async fn register_user(Json(payload): Json<User>) -> Result<StatusCode, String> {
let user = User::new(payload.username.clone());
match user.save().await {
Expand Down
35 changes: 30 additions & 5 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,71 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/*!
Fakehub server and storage.
*/
use std::io;

use anyhow::Result;
use axum::routing::{get, post};
use axum::Router;
use tokio::net::TcpListener;

use crate::routes::home;
use crate::routes::register_user::register_user;
use crate::handlers::home;
use crate::handlers::register_user::register_user;
use crate::xml::storage::Storage;

mod objects;
/// Handlers.
pub mod handlers;
/// GitHub domain objects.
pub mod objects;
/// Reports.
pub mod report;
mod routes;
mod xml;
/// XML storage.
pub mod xml;
#[allow(unused_imports)]
#[macro_use]
extern crate hamcrest;

/// Server.
#[derive(Default)]
pub struct Server {
/// Port.
port: usize,
}

impl Server {
/// Create new server with port.
///
/// * `port`: Server port
///
/// returns: Server
///
/// Examples:
///
/// ```
/// use server::Server;
/// let server = Server::new(1234);
/// ```
pub fn new(port: usize) -> Server {
Server { port }
}
}

/// Server configuration.
#[derive(Clone)]
pub struct ServerConfig {
/// Server host.
pub host: String,
/// Server port.
pub port: usize,
}

// @todo #79:30min Log 404 NOT FOUND requests too.
// Let's create a handler that would log requests failed with 404. Let's use
// info!() for this one.
impl Server {
/// Start a server.
pub async fn start(self) -> Result<()> {
tracing_subscriber::fmt::init();
Storage::new(Some("fakehub.xml"));
Expand Down
1 change: 1 addition & 0 deletions server/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/// GitHub user.
pub mod user;
17 changes: 16 additions & 1 deletion server/src/objects/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,26 @@ use log::{debug, info};
use serde::{Deserialize, Serialize};
use serde_xml_rs::to_string;

/// GitHub user.
#[derive(Debug, Serialize, Deserialize)]
pub struct User {
/// Username.
pub(crate) username: String,
}

impl User {
/// New user.
///
/// # Fields
///
/// * `username`: Username
///
/// # Examples
///
/// ```
/// use server::objects::user::User;
/// let jeff = User::new(String::from("jeff123"));
/// ```
pub fn new(username: String) -> User {
User { username }
}
Expand All @@ -45,8 +59,9 @@ impl User {
// We should prohibit to use #unwrap() function in our code. Let's configure
// clippy tool in the respective manner and get rid of all #unwrap() calls.
impl User {
/// Save user.
pub async fn save(self) -> Result<()> {
info!("registering user @{}", self.username);
info!("saving user @{}", self.username);
let xml = to_string(&self).unwrap();
debug!("XMLed user: {}", xml);
Ok(())
Expand Down
1 change: 1 addition & 0 deletions server/src/report/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
// @todo #41:45min Create similar to latex.rs functions for XML and TXT formats.
// Let's create similar to latex.rs functions for generating reports in XML and TXT.
// Don't forget to remove this puzzle.
/// LaTeX report.
pub mod latex;
29 changes: 0 additions & 29 deletions server/src/routes/rs_err.rs

This file was deleted.

1 change: 1 addition & 0 deletions server/src/xml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/// Storage.
pub mod storage;
23 changes: 20 additions & 3 deletions server/src/xml/storage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::fs::File;
use std::io::Write;

// The MIT License (MIT)
//
// Copyright (c) 2024 Aliaksei Bialiauski
Expand All @@ -24,9 +21,12 @@ use std::io::Write;
// SOFTWARE.
use anyhow::Result;
use log::info;
use std::fs::File;
use std::io::Write;

#[derive(Default)]
#[allow(dead_code)]
/// Storage.
pub struct Storage {
pub(crate) path: String,
}
Expand All @@ -37,6 +37,22 @@ const INIT_XML: &str = "<root>
";

impl Storage {
/// New storage.
///
/// # Fields
/// * `path`: Storage path
///
/// # Examples
///
/// ```
/// use server::xml::storage::Storage;
/// let storage = Storage::new(Some("test.xml"));
/// ```
/// Or use default path:
/// ```
/// use server::xml::storage::Storage;
/// let storage = Storage::new(None);
/// ```
pub fn new(path: Option<&str>) -> Storage {
let location = path.unwrap_or("fakehub.xml");
info!("Initializing XML storage: {location}");
Expand All @@ -62,6 +78,7 @@ impl Storage {
// Don't forget to create a unit tests related to #xml function.
impl Storage {
#[allow(dead_code)]
/// Return XML from the storage.
pub fn xml() -> Result<()> {
Ok(())
}
Expand Down

1 comment on commit 977aab6

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 977aab6 Jul 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 79-297c2dd6 disappeared from server/src/routes/home.rs), that's why I closed #84. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.