Skip to content

Commit

Permalink
Introduce Clippy, apply automated fixes, place endpoints in their own…
Browse files Browse the repository at this point in the history
… files, and introduce conversion macros (#222)

* Create endpoints module and cargo clippy

* Add test to confirm endpoint

* Do not run E2E tests by default

* Create a module for endpoint testing

* Use conventional traits instead of implementing the functions directly.

Adds two local macros in models.rs: `from_str!` and `try_from_i16!`.

This fixes clippy warnings

* Clippy

* Run clippy by default in the test workflow

* Fix tests and remove unused Display implementations

* Improve wording

* Remove HTTP endpoint "unit" testing

* Improve matching

* Update README

* Cargo fmt

* Change the error type in the macros to `AybError`

* Fmt
  • Loading branch information
sofiaritz authored Nov 17, 2023
1 parent e3e0276 commit e9941e2
Show file tree
Hide file tree
Showing 22 changed files with 429 additions and 412 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ jobs:
- name: Build
run: cargo build --verbose
- name: Ensure fmt finds no changes
run: cargo fmt -- --check
# - name: Ensure clippy finds no issues
# run: cargo clippy
run: cargo fmt --check
- name: Ensure clippy finds no issues
run: cargo clippy
- name: Run tests
run: cargo test --verbose
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
# Emacs
*~

# IntelliJ Idea-based IDEs
.idea/

# Ignore ayb-specific files
ayb.toml
e2e.sh
Expand Down
25 changes: 11 additions & 14 deletions src/ayb_db/db_interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,13 @@ pub trait AybDb: DynClone + Send + Sync {
) -> Result<InstantiatedAuthenticationMethod, AybError>;
async fn create_database(&self, database: &Database) -> Result<InstantiatedDatabase, AybError>;
async fn get_or_create_entity(&self, entity: &Entity) -> Result<InstantiatedEntity, AybError>;
async fn get_api_token(&self, short_token: &String) -> Result<APIToken, AybError>;
async fn get_api_token(&self, short_token: &str) -> Result<APIToken, AybError>;
async fn get_database(
&self,
entity_slug: &String,
database_slug: &String,
entity_slug: &str,
database_slug: &str,
) -> Result<InstantiatedDatabase, AybError>;
async fn get_entity_by_slug(
&self,
entity_slug: &String,
) -> Result<InstantiatedEntity, AybError>;
async fn get_entity_by_slug(&self, entity_slug: &str) -> Result<InstantiatedEntity, AybError>;
async fn get_entity_by_id(&self, entity_id: i32) -> Result<InstantiatedEntity, AybError>;
async fn list_authentication_methods(
&self,
Expand All @@ -60,7 +57,7 @@ macro_rules! implement_ayb_db {
db_error: &dyn sqlx::error::DatabaseError,
) -> bool {
match db_error.code() {
Some(code) => code.to_string() == $db_type::DUPLICATE_CONSTRAINT_ERROR_CODE,
Some(code) => code == $db_type::DUPLICATE_CONSTRAINT_ERROR_CODE,
None => false,
}
}
Expand Down Expand Up @@ -136,7 +133,7 @@ RETURNING entity_id, short_token, hash, status

async fn get_api_token(
&self,
short_token: &String,
short_token: &str,
) -> Result<APIToken, AybError> {
let api_token: APIToken = sqlx::query_as(
r#"
Expand Down Expand Up @@ -164,8 +161,8 @@ WHERE short_token = $1

async fn get_database(
&self,
entity_slug: &String,
database_slug: &String,
entity_slug: &str,
database_slug: &str,
) -> Result<InstantiatedDatabase, AybError> {
let db: InstantiatedDatabase = sqlx::query_as(
r#"
Expand All @@ -191,7 +188,7 @@ WHERE

async fn get_entity_by_slug(
&self,
entity_slug: &String,
entity_slug: &str,
) -> Result<InstantiatedEntity, AybError> {
let entity: InstantiatedEntity = sqlx::query_as(
r#"
Expand Down Expand Up @@ -319,7 +316,7 @@ impl SqliteAybDb {
.run(&pool)
.await
.expect("Unable to run migrations");
return Self { pool: pool };
Self { pool }
}
}

Expand All @@ -343,7 +340,7 @@ impl PostgresAybDb {
.run(&pool)
.await
.expect("Unable to run migrations");
return Self { pool: pool };
Self { pool }
}
}

Expand Down
183 changes: 79 additions & 104 deletions src/ayb_db/models.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
use crate::error::AybError;
use clap::ValueEnum;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use sqlx::FromRow;
use std::fmt;
use std::str::FromStr;

macro_rules! try_from_i16 {
($struct:ident, { $($left:literal => $right:expr),+ }) => {
impl TryFrom<i16> for $struct {
type Error = AybError;

fn try_from(value: i16) -> Result<Self, Self::Error> {
match value {
$($left => Ok($right),)*
_ => Err(Self::Error {
message: format!("Unknown value: {}", value),
}),
}
}
}
};
}

macro_rules! from_str {
($struct:ident, { $($left:literal => $right:expr),+ }) => {
impl FromStr for $struct {
type Err = AybError;

fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
$($left => Ok($right),)*
_ => Err(Self::Err {
message: format!("Unknown value: {}", value),
}),
}
}
}
};
}

#[derive(
Serialize_repr, Deserialize_repr, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, ValueEnum,
Expand All @@ -13,29 +48,17 @@ pub enum DBType {
Duckdb = 1,
}

impl fmt::Display for DBType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}

impl DBType {
pub fn from_i16(value: i16) -> DBType {
match value {
0 => DBType::Sqlite,
1 => DBType::Duckdb,
_ => panic!("Unknown value: {}", value),
}
}
from_str!(DBType, {
"sqlite" => DBType::Sqlite,
"duckdb" => DBType::Duckdb
});

pub fn from_str(value: &str) -> DBType {
match value {
"sqlite" => DBType::Sqlite,
"duckdb" => DBType::Duckdb,
_ => panic!("Unknown value: {}", value),
}
}
try_from_i16!(DBType, {
0 => DBType::Sqlite,
1 => DBType::Duckdb
});

impl DBType {
pub fn to_str(&self) -> &str {
match self {
DBType::Sqlite => "sqlite",
Expand All @@ -53,29 +76,17 @@ pub enum EntityType {
Organization = 1,
}

impl fmt::Display for EntityType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}

impl EntityType {
pub fn from_i16(value: i16) -> EntityType {
match value {
0 => EntityType::User,
1 => EntityType::Organization,
_ => panic!("Unknown value: {}", value),
}
}
from_str!(EntityType, {
"user" => EntityType::User,
"organization" => EntityType::Organization
});

pub fn from_str(value: &str) -> EntityType {
match value {
"user" => EntityType::User,
"organization" => EntityType::Organization,
_ => panic!("Unknown value: {}", value),
}
}
try_from_i16!(EntityType, {
0 => EntityType::User,
1 => EntityType::Organization
});

impl EntityType {
pub fn to_str(&self) -> &str {
match self {
EntityType::User => "user",
Expand All @@ -92,27 +103,15 @@ pub enum AuthenticationMethodType {
Email = 0,
}

impl fmt::Display for AuthenticationMethodType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}

impl AuthenticationMethodType {
pub fn from_i16(value: i16) -> AuthenticationMethodType {
match value {
0 => AuthenticationMethodType::Email,
_ => panic!("Unknown value: {}", value),
}
}
from_str!(AuthenticationMethodType, {
"email" => AuthenticationMethodType::Email
});

pub fn from_str(value: &str) -> AuthenticationMethodType {
match value {
"email" => AuthenticationMethodType::Email,
_ => panic!("Unknown value: {}", value),
}
}
try_from_i16!(AuthenticationMethodType, {
0 => AuthenticationMethodType::Email
});

impl AuthenticationMethodType {
pub fn to_str(&self) -> &str {
match self {
AuthenticationMethodType::Email => "email",
Expand All @@ -129,29 +128,17 @@ pub enum AuthenticationMethodStatus {
Revoked = 1,
}

impl fmt::Display for AuthenticationMethodStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}

impl AuthenticationMethodStatus {
pub fn from_i16(value: i16) -> AuthenticationMethodStatus {
match value {
0 => AuthenticationMethodStatus::Verified,
1 => AuthenticationMethodStatus::Revoked,
_ => panic!("Unknown value: {}", value),
}
}
from_str!(AuthenticationMethodStatus, {
"verified" => AuthenticationMethodStatus::Verified,
"revoked" => AuthenticationMethodStatus::Revoked
});

pub fn from_str(value: &str) -> AuthenticationMethodStatus {
match value {
"verified" => AuthenticationMethodStatus::Verified,
"revoked" => AuthenticationMethodStatus::Revoked,
_ => panic!("Unknown value: {}", value),
}
}
try_from_i16!(AuthenticationMethodStatus, {
0 => AuthenticationMethodStatus::Verified,
1 => AuthenticationMethodStatus::Revoked
});

impl AuthenticationMethodStatus {
pub fn to_str(&self) -> &str {
match self {
AuthenticationMethodStatus::Verified => "verified",
Expand Down Expand Up @@ -214,29 +201,17 @@ pub enum APITokenStatus {
Revoked = 1,
}

impl fmt::Display for APITokenStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}

impl APITokenStatus {
pub fn from_i16(value: i16) -> APITokenStatus {
match value {
0 => APITokenStatus::Active,
1 => APITokenStatus::Revoked,
_ => panic!("Unknown value: {}", value),
}
}
from_str!(APITokenStatus, {
"active" => APITokenStatus::Active,
"revoked" => APITokenStatus::Revoked
});

pub fn from_str(value: &str) -> APITokenStatus {
match value {
"active" => APITokenStatus::Active,
"revoked" => APITokenStatus::Revoked,
_ => panic!("Unknown value: {}", value),
}
}
try_from_i16!(APITokenStatus, {
0 => APITokenStatus::Active,
1 => APITokenStatus::Revoked
});

impl APITokenStatus {
pub fn to_str(&self) -> &str {
match self {
APITokenStatus::Active => "active",
Expand Down
4 changes: 2 additions & 2 deletions src/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ pub async fn send_registration_email(
config: &AybConfigEmail,
e2e_testing_on: bool,
) -> Result<(), AybError> {
return send_email(
send_email(
to,
"Your login credentials",
format!("To log in, type\n\tayb client confirm {token}"),
config,
e2e_testing_on,
)
.await;
.await
}

async fn send_email(
Expand Down
10 changes: 4 additions & 6 deletions src/hosted_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl QueryResult {
.collect::<Vec<_>>();
table.add_row(Row::new(cells));
}
return table;
table
}

pub fn generate_table(&self) -> Result<(), std::io::Error> {
Expand All @@ -56,10 +56,8 @@ impl QueryResult {
pub fn run_query(path: &PathBuf, query: &str, db_type: &DBType) -> Result<QueryResult, AybError> {
match db_type {
DBType::Sqlite => Ok(run_sqlite_query(path, query)?),
_ => {
return Err(AybError {
message: "Unsupported DB type".to_string(),
})
}
_ => Err(AybError {
message: "Unsupported DB type".to_string(),
}),
}
}
Loading

0 comments on commit e9941e2

Please sign in to comment.