Runs rustfmt across realworld

This commit is contained in:
Samani G. Gikandi 2020-04-15 11:00:46 -07:00 committed by Ryan Leckey
parent 8138a26b61
commit 9fb523d4e8
8 changed files with 64 additions and 53 deletions

View file

@ -280,7 +280,8 @@ pub async fn delete_article(
let mut tx = state
.conn()
.and_then(Connection::begin)
.await.server_err()?;
.await
.server_err()?;
let article = tx.get_article_by_slug(&slug).await?;

View file

@ -1,7 +1,7 @@
use std::collections::HashSet;
use serde::Serialize;
use chrono::{DateTime, Utc};
use serde::Serialize;
use crate::db::model::*;
@ -25,7 +25,10 @@ pub(in crate::api) struct Article {
impl Article {
/// Create an article with the author.following field populated
pub fn with_following(entities: (ArticleEntity, ProfileEntity), leader_ids: &HashSet<EntityId>) -> Self {
pub fn with_following(
entities: (ArticleEntity, ProfileEntity),
leader_ids: &HashSet<EntityId>,
) -> Self {
let is_following = leader_ids.contains(&entities.1.user_id);
let mut article = Article::from(entities);
article.author.following = is_following;
@ -72,7 +75,7 @@ impl From<(ArticleEntity, Profile)> for Article {
updated_at,
tag_list: vec![],
favorited: false,
favorites_count: 0
favorites_count: 0,
}
}
}
@ -90,24 +93,24 @@ pub(in crate::api) struct Profile {
impl Profile {
pub fn following(self, following: bool) -> Self {
Profile {
following,
..self
}
Profile { following, ..self }
}
}
impl From<ProfileEntity> for Profile {
fn from(ent: ProfileEntity) -> Self {
let ProfileEntity {
username, bio, image, ..
username,
bio,
image,
..
} = ent;
Profile {
username,
bio,
image,
following: false
following: false,
}
}
}

View file

@ -1,14 +1,14 @@
use futures::TryFutureExt;
use log::*;
use serde::Serialize;
use sqlx::pool::PoolConnection;
use sqlx::{Connect, Connection};
use tide::{Error, IntoResponse, Request, Response, ResultExt};
use futures::TryFutureExt;
use crate::api::model::*;
use crate::api::util::*;
use crate::db::model::*;
use crate::db::Db;
use crate::api::model::*;
#[derive(Serialize)]
struct ProfileResponseBody {

View file

@ -4,7 +4,7 @@ use chrono::{Duration, Utc};
use futures::TryFutureExt;
use log::*;
use rand::{thread_rng, RngCore};
use serde::{Deserialize, Serialize, Deserializer};
use serde::{Deserialize, Deserializer, Serialize};
use sqlx::pool::PoolConnection;
use sqlx::{Connect, Connection};
use tide::{Error, IntoResponse, Request, Response, ResultExt};
@ -70,10 +70,12 @@ impl<T> From<Option<T>> for Nullable<T> {
}
impl<'de, T> Deserialize<'de> for Nullable<T>
where T: Deserialize<'de>
where
T: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Option::deserialize(deserializer).map(Nullable::from)
}
@ -85,7 +87,6 @@ impl<T> Default for Nullable<T> {
}
}
/// The response body for User API requests
#[derive(Serialize)]
struct UserResponseBody {
@ -192,9 +193,11 @@ pub async fn get_current_user(
let mut db = state.conn().await.server_err()?;
// n.b - the app doesn't support deleting users
let user_ent= db.get_user_by_id(user_id).await?;
let user_ent = db.get_user_by_id(user_id).await?;
let resp = to_json_response(&UserResponseBody::from(User::from(user_ent).token(Some(token))))?;
let resp = to_json_response(&UserResponseBody::from(
User::from(user_ent).token(Some(token)),
))?;
Ok::<_, Error>(resp)
}
@ -346,7 +349,7 @@ fn generate_random_salt() -> [u8; 16] {
fn generate_token(user_id: i32) -> jsonwebtoken::errors::Result<String> {
use jsonwebtoken::Header;
let exp = Utc::now() + Duration::hours(24); // n.b. (bad for sec, good for testing)
let exp = Utc::now() + Duration::hours(24); // n.b. (bad for sec, good for testing)
let token = jsonwebtoken::encode(
&Header::default(),
&TokenClaims {

View file

@ -191,16 +191,17 @@ impl From<SqlxError> for ProvideError {
{
if let Some(pg_err) = db_err.try_downcast_ref::<sqlx::postgres::PgError>() {
if let Ok(provide_err) = ProvideError::try_from(pg_err) {
return provide_err
return provide_err;
}
}
}
#[cfg(feature = "sqlite")]
{
if let Some(sqlite_err) = db_err.try_downcast_ref::<sqlx::sqlite::SqliteError>() {
if let Some(sqlite_err) = db_err.try_downcast_ref::<sqlx::sqlite::SqliteError>()
{
if let Ok(provide_err) = ProvideError::try_from(sqlite_err) {
return provide_err
return provide_err;
}
}
}
@ -211,4 +212,3 @@ impl From<SqlxError> for ProvideError {
}
}
}

View file

@ -1,10 +1,10 @@
use std::convert::TryFrom;
use async_trait::async_trait;
use sqlx::pool::PoolConnection;
use sqlx::{PgConnection, PgPool};
use sqlx::error::DatabaseError;
use sqlx::pool::PoolConnection;
use sqlx::postgres::PgError;
use sqlx::{PgConnection, PgPool};
use crate::db::model::*;
use crate::db::Db;
@ -402,8 +402,8 @@ WHERE comment_id = $2
article_slug,
comment_id,
)
.fetch_one(self)
.await?;
.fetch_one(self)
.await?;
Ok(rec)
}

View file

@ -3,16 +3,15 @@ use std::convert::TryFrom;
use anyhow::{Error, Result};
use async_trait::async_trait;
use chrono::{TimeZone, Utc};
use sqlx::error::DatabaseError;
use sqlx::pool::PoolConnection;
use sqlx::sqlite::{SqliteQueryAs, SqliteError};
use sqlx::sqlite::{SqliteError, SqliteQueryAs};
use sqlx::Error as SqlxError;
use sqlx::{Connection, Cursor, Executor, FromRow, SqliteConnection, SqlitePool};
use sqlx::error::DatabaseError;
use crate::db::model::*;
use crate::db::Db;
impl TryFrom<&SqliteError> for ProvideError {
type Error = ();
@ -31,7 +30,6 @@ impl TryFrom<&SqliteError> for ProvideError {
}
}
#[derive(sqlx::FromRow)]
struct SqliteArticleEntity {
article_id: EntityId,
@ -478,13 +476,12 @@ WHERE comment_id = $2
article_slug,
comment_id,
)
.fetch_one(self)
.await?;
.fetch_one(self)
.await?;
Ok(rec.into())
}
async fn get_comments_on_article(
&mut self,
article_slug: &str,

View file

@ -1,10 +1,10 @@
use async_std::net::ToSocketAddrs;
use sqlx_example_realworld::api::{articles, profiles, users};
use sqlx_example_realworld::db::Db;
use sqlx_example_realworld::db::model::{ProvideAuthn, ProvideData};
use sqlx_example_realworld::db;
use sqlx::pool::PoolConnection;
use sqlx_example_realworld::api::{articles, profiles, users};
use sqlx_example_realworld::db;
use sqlx_example_realworld::db::model::{ProvideAuthn, ProvideData};
use sqlx_example_realworld::db::Db;
use tide::middleware::RequestLogger;
#[derive(structopt::StructOpt)]
@ -21,8 +21,8 @@ struct Args {
async fn run_server<S, C>(addr: impl ToSocketAddrs, state: S) -> anyhow::Result<()>
where
S: Send + Sync + Db<Conn=PoolConnection<C>> + 'static,
C: sqlx::Connect + ProvideAuthn + ProvideData
S: Send + Sync + Db<Conn = PoolConnection<C>> + 'static,
C: sqlx::Connect + ProvideAuthn + ProvideData,
{
let mut server = tide::with_state(state);
@ -31,43 +31,50 @@ where
// users
server.at("/api/users").post(users::register);
server.at("/api/users/login").post(users::login);
server.at("/api/user")
server
.at("/api/user")
.get(users::get_current_user)
.put(users::update_user);
// profiles
server.at("/api/profiles/:username").get(profiles::get_profile);
server.at("/api/profiles/:username/follow")
server
.at("/api/profiles/:username")
.get(profiles::get_profile);
server
.at("/api/profiles/:username/follow")
.post(profiles::follow_user)
.delete(profiles::unfollow_user);
// articles
server.at("/api/articles")
server
.at("/api/articles")
.get(articles::list_articles)
.post(articles::create_article);
server.at("/api/articles/:slug")
server
.at("/api/articles/:slug")
.get(articles::get_article)
.put(articles::update_article)
.delete(articles::delete_article);
server.at("/api/articles/feed")
.get(articles::get_feed);
server.at("/api/articles/feed").get(articles::get_feed);
// favorites
server.at("/api/articles/:slug/favorite")
server
.at("/api/articles/:slug/favorite")
.post(articles::favorite_article)
.delete(articles::unfavorite_article);
// comments
server.at("/api/articles/:slug/comments")
server
.at("/api/articles/:slug/comments")
.post(articles::add_comment)
.get(articles::get_comments);
server.at("/api/articles/:slug/comments/:comment_id")
server
.at("/api/articles/:slug/comments/:comment_id")
.delete(articles::delete_comment);
// tags
server.at("/api/tags")
.get(articles::get_tags);
server.at("/api/tags").get(articles::get_tags);
server.listen(addr).await?;