Compare commits

...

4 Commits

Author SHA1 Message Date
MedzikUser f440f30ea8
fix(tests): add some derive to pass test
Added PartialEq, Eq, PartialOrd and Ord derive to pass test broken in 4a9f8ec2e8 commit.
2022-09-18 15:25:42 +02:00
MedzikUser 4a9f8ec2e8
chore: cosmetic changes 2022-09-18 15:14:25 +02:00
MedzikUser bf10de2ca1
chore(readme): fix typo 2022-09-18 14:59:32 +02:00
MedzikUser 6b5186b503
chore(readme): update 2022-09-18 14:57:49 +02:00
7 changed files with 55 additions and 90 deletions

View File

@ -1,36 +1,15 @@
# HomeDisk cloud server
<p align="center">
<img width="500" src="https://raw.githubusercontent.com/HomeDisk/.github/main/img/HomeDisk.svg" alt="HomeDisk Icon" />
</p>
> **Warning**
> Currently, work on the project is suspended because I'm not good at frontend and don't have time :(
[docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
[total-lines]: https://img.shields.io/tokei/lines/github/MedzikUser/HomeDisk?style=for-the-badge&logo=github&color=fede00
[code-size]: https://img.shields.io/github/languages/code-size/MedzikUser/HomeDisk?style=for-the-badge&color=c8df52&logo=github
[ci]: https://img.shields.io/github/workflow/status/MedzikUser/HomeDisk/Rust/main?style=for-the-badge
[screenshot-home]: https://raw.githubusercontent.com/HomeDisk/.github/main/screenshots/home.png
[screenshot-login]: https://raw.githubusercontent.com/HomeDisk/.github/main/screenshots/login.png
[![docs-rs]](https://homedisk-doc.vercel.app)
[![total-lines]](https://github.com/MedzikUser/HomeDisk)
[![code-size]](https://github.com/MedzikUser/HomeDisk)
[![ci]](https://github.com/MedzikUser/HomeDisk/actions/workflows/rust.yml)
![screenshot-home]
![screenshot-login]
## 👨‍💻 Building
First clone the repository: `git clone https://github.com/MedzikUser/HomeDisk.git`
### Requirements
- Rust
To build run the command: `cargo build --release`
The compiled binary can be found in `./target/release/homedisk`
## Configure
Go to [config](https://homedisk-doc.medzik.xyz/homedisk_types/config) module
<p align="center">
<a href="https://github.com/MedzikUser/HomeDisk"><img src="https://img.shields.io/badge/built_with-Rust-dca282.svg?style=flat-square"></a>
&nbsp;
<a href="https://github.com/MedzikUser/HomeDisk"><img src="https://img.shields.io/badge/license-GPL_3.0-00bfff.svg?style=flat-square"></a>
&nbsp;
<a href="https://github.com/MedzikUser/HomeDisk"><img src="https://img.shields.io/github/workflow/status/MedzikUser/HomeDisk/Rust/main?style=flat-square"></a>
&nbsp;
<a href="https://homedisk-doc.vercel.app"><img src="https://img.shields.io/badge/docs.rs-66c2a5?style=flat-square&labelColor=555555&logo=docs.rs"></a>
&nbsp;
<a href="https://documenter.getpostman.com/view/23280189/VVk9dwRk"><img src="https://img.shields.io/badge/API_Docs-887BB0?style=flat-square&labelColor=555555&logo=postman"></a>
</p>

View File

@ -1,21 +1,19 @@
use thiserror::Error;
/// Database Error
#[derive(Debug, Error)]
#[derive(Debug, Error, PartialEq, Eq, PartialOrd, Ord)]
pub enum Error {
#[error("user not found")]
#[error("User not found")]
UserNotFound,
#[error("failed to open database: {0}")]
OpenDatabase(sqlx::Error),
#[error("failed to connect to the database: {0}")]
ConnectDatabase(sqlx::Error),
#[error("failed to get row: {0}")]
GetRow(sqlx::Error),
#[error("failed to create all required tables: {0}")]
CreateTables(sqlx::Error),
#[error("failed to execute the query: {0}")]
Execute(sqlx::Error),
#[error("Failed to open database: {0}")]
OpenDatabase(String),
#[error("Failed to open connection with database: {0}")]
ConnectDatabase(String),
#[error("Failed to get row: {0}")]
GetRow(String),
#[error("Failed to create all required tables: {0}")]
CreateTables(String),
#[error("Failed to execute the query: {0}")]
Execute(String),
}
/// Custom Result alias for a [enum@Error].
pub type Result<T> = std::result::Result<T, Error>;

View File

@ -1,4 +1,4 @@
//! [SQLite database functions](sqlite::Database).
//! [SQLite database functions](sqlite::Database)
pub mod error;
mod sqlite;

View File

@ -19,14 +19,14 @@ pub struct Database {
pub pool: SqlitePool,
}
// TODO: Check UUID
impl Database {
/// Open a SQLite database
pub async fn open(path: &str) -> Result<Self> {
debug!("Opening SQLite database: {}", path);
// sqlite connection options
let mut options = SqliteConnectOptions::from_str(path).map_err(Error::OpenDatabase)?;
let mut options =
SqliteConnectOptions::from_str(path).map_err(|e| Error::OpenDatabase(e.to_string()))?;
// set log level to Debug
options.log_statements(LevelFilter::Debug);
@ -34,7 +34,7 @@ impl Database {
// create a database pool
let pool = SqlitePool::connect_with(options)
.await
.map_err(Error::ConnectDatabase)?;
.map_err(|e| Error::ConnectDatabase(e.to_string()))?;
info!("Connected to database!");
@ -46,7 +46,7 @@ impl Database {
self.pool
.execute(include_str!("../../tables.sql"))
.await
.map_err(Error::CreateTables)
.map_err(|e| Error::CreateTables(e.to_string()))
}
/// Create new user in the database.
@ -59,7 +59,10 @@ impl Database {
.bind(&user.username)
.bind(&user.password);
self.pool.execute(query).await.map_err(Error::Execute)
self.pool
.execute(query)
.await
.map_err(|e| Error::Execute(e.to_string()))
}
/// Search for a user.
@ -78,7 +81,7 @@ impl Database {
let row = stream
.try_next()
.await
.map_err(Error::Execute)?
.map_err(|e| Error::Execute(e.to_string()))?
.ok_or(Error::UserNotFound)?;
Self::find(row)
@ -98,7 +101,7 @@ impl Database {
let row = stream
.try_next()
.await
.map_err(Error::Execute)?
.map_err(|e| Error::Execute(e.to_string()))?
.ok_or(Error::UserNotFound)?;
Self::find(row)
@ -106,11 +109,17 @@ impl Database {
fn find(row: SqliteRow) -> Result<User> {
// get `id` row
let id = row.try_get("id").map_err(Error::GetRow)?;
let id = row
.try_get("id")
.map_err(|e| Error::GetRow(e.to_string()))?;
// get `username` row
let username = row.try_get("username").map_err(Error::GetRow)?;
let username = row
.try_get("username")
.map_err(|e| Error::GetRow(e.to_string()))?;
// get `password` row
let password = row.try_get("password").map_err(Error::GetRow)?;
let password = row
.try_get("password")
.map_err(|e| Error::GetRow(e.to_string()))?;
Ok(User {
id,
@ -155,7 +164,10 @@ mod tests {
async fn test_find_user() {
let db = new_user().await;
let user = db.find_user(&User::new(USERNAME, PASSWORD, false)).await.unwrap();
let user = db
.find_user(&User::new(USERNAME, PASSWORD, false))
.await
.unwrap();
assert_eq!(user.username, USERNAME)
}
@ -169,6 +181,6 @@ mod tests {
.await
.unwrap_err();
assert_eq!(err.to_string(), "user not found")
assert_eq!(err, Error::UserNotFound)
}
}

View File

@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use uuid::Uuid;
/// SQL user entry
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct User {
pub id: String,
pub username: String,

View File

@ -1,31 +1,6 @@
//! HomeDisk cloud server
//!
//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
//! [total-lines]: https://img.shields.io/tokei/lines/github/MedzikUser/HomeDisk?style=for-the-badge&logo=github&color=fede00
//! [code-size]: https://img.shields.io/github/languages/code-size/MedzikUser/HomeDisk?style=for-the-badge&color=c8df52&logo=github
//! [ci]: https://img.shields.io/github/workflow/status/MedzikUser/HomeDisk/Rust/main?style=for-the-badge
//!
//! [home-screenshot]: https://raw.githubusercontent.com/HomeDisk/.github/main/screenshots/home.png
//!
//! [![github]](https://github.com/MedzikUser/HomeDisk)
//! [![docs-rs]](https://homedisk-doc.vercel.app)
//! [![total-lines]](https://github.com/MedzikUser/HomeDisk)
//! [![code-size]](https://github.com/MedzikUser/HomeDisk)
//! [![ci]](https://github.com/MedzikUser/HomeDisk/actions/workflows/rust.yml)
//!
//! ![home-screenshot]
//!
//! ## 👨‍💻 Building
//!
//! First clone the repository: `git clone https://github.com/MedzikUser/HomeDisk.git`
//!
//! ### Requirements
//! - [Rust](https://rust-lang.org)
//!
//! To build run the command: `cargo build --release`
//!
//! The compiled binary can be found in `./target/release/homedisk`
//! [Source code available on GitHub](https://github.com/MedzikUser/HomeDisk)
//!
//! ## Configure
//!

View File

@ -1,4 +1,5 @@
//! [Configuration file types](Config).
//! [Configuration file types](Config)
use std::fs;
use serde::{Deserialize, Serialize};