fix clippy warnings of backend

This commit is contained in:
appflowy 2021-12-02 21:54:21 +08:00
parent 24ea68c605
commit 739333c29c
19 changed files with 80 additions and 77 deletions

View File

@ -134,10 +134,9 @@ pub async fn init_app_context(configuration: &Settings) -> AppContext {
let _ = crate::service::log::Builder::new("flowy-server")
.env_filter("Trace")
.build();
let pg_pool = get_connection_pool(&configuration.database).await.expect(&format!(
"Failed to connect to Postgres at {:?}.",
configuration.database
));
let pg_pool = get_connection_pool(&configuration.database)
.await
.unwrap_or_else(|_| panic!("Failed to connect to Postgres at {:?}.", configuration.database));
let ws_server = WsServer::new().start();
AppContext::new(ws_server, pg_pool)

View File

@ -9,12 +9,12 @@ pub struct DocTable {
pub(crate) rev_id: i64,
}
impl std::convert::Into<Doc> for DocTable {
fn into(self) -> Doc {
impl std::convert::From<DocTable> for Doc {
fn from(table: DocTable) -> Self {
let mut doc = Doc::new();
doc.set_id(self.id.to_string());
doc.set_data(self.data);
doc.set_rev_id(self.rev_id);
doc.set_id(table.id.to_string());
doc.set_data(table.data);
doc.set_rev_id(table.rev_id);
doc
}
}

View File

@ -16,15 +16,14 @@ pub struct WorkspaceTable {
pub(crate) create_time: chrono::DateTime<Utc>,
pub(crate) user_id: String,
}
impl std::convert::Into<Workspace> for WorkspaceTable {
fn into(self) -> Workspace {
impl std::convert::From<WorkspaceTable> for Workspace {
fn from(table: WorkspaceTable) -> Self {
let mut workspace = Workspace::default();
workspace.set_id(self.id.to_string());
workspace.set_name(self.name.clone());
workspace.set_desc(self.description.clone());
workspace.set_modified_time(self.modified_time.timestamp());
workspace.set_create_time(self.create_time.timestamp());
workspace.set_id(table.id.to_string());
workspace.set_name(table.name.clone());
workspace.set_desc(table.description.clone());
workspace.set_modified_time(table.modified_time.timestamp());
workspace.set_create_time(table.create_time.timestamp());
workspace
}
}
@ -41,29 +40,28 @@ pub struct AppTable {
pub(crate) create_time: chrono::DateTime<Utc>,
pub(crate) user_id: String,
}
impl std::convert::Into<App> for AppTable {
fn into(self) -> App {
impl std::convert::From<AppTable> for App {
fn from(table: AppTable) -> Self {
let mut app = App::default();
app.set_id(self.id.to_string());
app.set_workspace_id(self.workspace_id.to_string());
app.set_name(self.name.clone());
app.set_desc(self.description.clone());
app.set_id(table.id.to_string());
app.set_workspace_id(table.workspace_id.to_string());
app.set_name(table.name.clone());
app.set_desc(table.description.clone());
app.set_belongings(RepeatedView::default());
app.set_modified_time(self.modified_time.timestamp());
app.set_create_time(self.create_time.timestamp());
app.set_modified_time(table.modified_time.timestamp());
app.set_create_time(table.create_time.timestamp());
app
}
}
impl std::convert::Into<Trash> for AppTable {
fn into(self) -> Trash {
impl std::convert::From<AppTable> for Trash {
fn from(table: AppTable) -> Self {
Trash {
id: self.id.to_string(),
name: self.name,
modified_time: self.modified_time.timestamp(),
create_time: self.create_time.timestamp(),
id: table.id.to_string(),
name: table.name,
modified_time: table.modified_time.timestamp(),
create_time: table.create_time.timestamp(),
ty: TrashType::App,
unknown_fields: Default::default(),
cached_size: Default::default(),
@ -82,32 +80,31 @@ pub struct ViewTable {
pub(crate) thumbnail: String,
pub(crate) view_type: i32,
}
impl std::convert::Into<View> for ViewTable {
fn into(self) -> View {
let view_type = ViewType::from_i32(self.view_type).unwrap_or(ViewType::Doc);
impl std::convert::From<ViewTable> for View {
fn from(table: ViewTable) -> Self {
let view_type = ViewType::from_i32(table.view_type).unwrap_or(ViewType::Doc);
let mut view = View::default();
view.set_id(self.id.to_string());
view.set_belong_to_id(self.belong_to_id);
view.set_name(self.name);
view.set_desc(self.description);
view.set_id(table.id.to_string());
view.set_belong_to_id(table.belong_to_id);
view.set_name(table.name);
view.set_desc(table.description);
view.set_view_type(view_type);
view.set_belongings(RepeatedView::default());
view.set_create_time(self.create_time.timestamp());
view.set_modified_time(self.modified_time.timestamp());
view.set_create_time(table.create_time.timestamp());
view.set_modified_time(table.modified_time.timestamp());
view
}
}
impl std::convert::Into<Trash> for ViewTable {
fn into(self) -> Trash {
impl std::convert::From<ViewTable> for Trash {
fn from(table: ViewTable) -> Self {
Trash {
id: self.id.to_string(),
name: self.name,
modified_time: self.modified_time.timestamp(),
create_time: self.create_time.timestamp(),
id: table.id.to_string(),
name: table.name,
modified_time: table.modified_time.timestamp(),
create_time: table.create_time.timestamp(),
ty: TrashType::View,
unknown_fields: Default::default(),
cached_size: Default::default(),

View File

@ -85,10 +85,10 @@ where
if authenticate_pass {
let fut = self.service.call(req);
return Box::pin(async move {
Box::pin(async move {
let res = fut.await?;
Ok(res.map_body(|_, body| AnyBody::from_message(body)))
});
})
} else {
Box::pin(async move { Ok(req.into_response(unauthorized_response())) })
}

View File

@ -73,7 +73,7 @@ impl NewAppSqlBuilder {
}
pub fn color_style(mut self, color_style: ColorStyle) -> Self {
self.table.color_style = color_style.write_to_bytes().unwrap_or(default_color_style());
self.table.color_style = color_style.write_to_bytes().unwrap_or_else(|_| default_color_style());
self
}

View File

@ -61,12 +61,16 @@ pub struct DocManager {
docs_map: DashMap<String, Arc<DocHandle>>,
}
impl DocManager {
pub fn new() -> Self {
impl std::default::Default for DocManager {
fn default() -> Self {
Self {
docs_map: DashMap::new(),
}
}
}
impl DocManager {
pub fn new() -> Self { DocManager::default() }
pub async fn get(&self, doc_id: &str, pg_pool: Data<PgPool>) -> Result<Option<Arc<DocHandle>>, ServerError> {
match self.docs_map.get(doc_id) {

View File

@ -146,7 +146,7 @@ impl ServerEditDoc {
fn mk_revision(&self, base_rev_id: i64, delta: Delta) -> Revision {
let delta_data = delta.to_bytes().to_vec();
let md5 = md5(&delta_data);
let revision = Revision {
Revision {
base_rev_id,
rev_id: self.rev_id.load(SeqCst),
delta_data,
@ -154,8 +154,7 @@ impl ServerEditDoc {
doc_id: self.doc_id.to_string(),
ty: RevType::Remote,
..Default::default()
};
revision
}
}
#[tracing::instrument(

View File

@ -35,7 +35,7 @@ impl Builder {
.finish()
.with(env_filter);
let formatting_layer = BunyanFormattingLayer::new(self.name.clone(), std::io::stdout);
let formatting_layer = BunyanFormattingLayer::new(self.name, std::io::stdout);
let _ = set_global_default(subscriber.with(JsonStorageLayer).with(formatting_layer))
.map_err(|e| format!("{:?}", e))?;

View File

@ -86,7 +86,7 @@ pub(crate) async fn delete_trash(
let _ = delete_trash_targets(
transaction as &mut DBTransaction<'_>,
vec![(trash_table.id.clone(), trash_table.ty)],
vec![(trash_table.id, trash_table.ty)],
)
.await?;

View File

@ -8,7 +8,6 @@ use backend_service::{
errors::{invalid_params, ErrorCode, ServerError},
response::FlowyResponse,
};
#[allow(dead_code)]
use chrono::Utc;
use flowy_user_infra::{
parser::{UserEmail, UserName, UserPassword},
@ -42,7 +41,7 @@ pub async fn sign_in(pool: &PgPool, params: SignInParams) -> Result<SignInRespon
response_data.set_user_id(user.id.to_string());
response_data.set_name(user.name);
response_data.set_email(user.email);
response_data.set_token(token.clone().into());
response_data.set_token(token.into());
Ok(response_data)
}

View File

@ -75,8 +75,11 @@ enum AuthStatus {
pub const EXPIRED_DURATION_DAYS: i64 = 30;
pub struct AuthorizedUsers(DashMap<LoggedUser, AuthStatus>);
impl std::default::Default for AuthorizedUsers {
fn default() -> Self { Self(DashMap::new()) }
}
impl AuthorizedUsers {
pub fn new() -> Self { Self(DashMap::new()) }
pub fn new() -> Self { AuthorizedUsers::default() }
pub fn is_authorized(&self, user: &LoggedUser) -> bool {
match self.0.get(user) {

View File

@ -100,7 +100,7 @@ pub async fn read_workspaces(
)
.await
.context("Get workspace app")
.unwrap_or(RepeatedApp::default());
.unwrap_or_default();
let mut workspace: Workspace = table.into();
workspace.set_apps(apps);

View File

@ -12,15 +12,14 @@ pub struct WsBizHandlers {
inner: HashMap<WsModule, BizHandler>,
}
impl std::default::Default for WsBizHandlers {
fn default() -> Self { Self { inner: HashMap::new() } }
}
impl WsBizHandlers {
pub fn new() -> Self { Self { inner: HashMap::new() } }
pub fn new() -> Self { WsBizHandlers::default() }
pub fn register(&mut self, source: WsModule, handler: BizHandler) { self.inner.insert(source, handler); }
pub fn get(&self, source: &WsModule) -> Option<BizHandler> {
match self.inner.get(source) {
None => None,
Some(handler) => Some(handler.clone()),
}
}
pub fn get(&self, source: &WsModule) -> Option<BizHandler> { self.inner.get(source).cloned() }
}

View File

@ -15,7 +15,7 @@ impl<T: AsRef<str>> std::convert::From<T> for SessionId {
impl std::fmt::Display for SessionId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let desc = format!("{}", &self.0);
let desc = &self.0.to_string();
f.write_str(&desc)
}
}

View File

@ -23,11 +23,11 @@ pub async fn establish_ws_connection(
tracing::info!("establish_ws_connection");
match LoggedUser::from_token(token.clone()) {
Ok(user) => {
let ws_user = WsUser::new(user.clone());
let ws_user = WsUser::new(user);
let client = WsClient::new(ws_user, server.get_ref().clone(), biz_handlers);
let result = ws::start(client, &request, payload);
match result {
Ok(response) => Ok(response.into()),
Ok(response) => Ok(response),
Err(e) => {
log::error!("ws connection error: {:?}", e);
Err(e)

View File

@ -10,12 +10,15 @@ pub struct WsServer {
sessions: DashMap<SessionId, Session>,
}
impl WsServer {
pub fn new() -> Self {
impl std::default::Default for WsServer {
fn default() -> Self {
Self {
sessions: DashMap::new(),
}
}
}
impl WsServer {
pub fn new() -> Self { WsServer::default() }
pub fn send(&self, _msg: WsMessageAdaptor) { unimplemented!() }
}

View File

@ -1,3 +1,4 @@
#![allow(clippy::all)]
use crate::util::helper::*;
use flowy_workspace_infra::entities::{
app::{AppIdentifier, UpdateAppParams},

View File

@ -159,8 +159,7 @@ fn assert_eq(expect: &str, receive: &str) {
async fn create_doc(flowy_test: &FlowyTest) -> String {
let view_test = ViewTest::new(flowy_test).await;
let doc_id = view_test.view.id.clone();
doc_id
view_test.view.id
}
async fn save_doc(doc_id: &str, json: String, rev_id: i64, pool: Data<PgPool>) {

View File

@ -197,7 +197,7 @@ pub struct TestServer {
}
pub async fn spawn_server() -> TestServer {
let database_name = format!("{}", Uuid::new_v4().to_string());
let database_name = Uuid::new_v4().to_string();
let configuration = {
let mut c = get_configuration().expect("Failed to read configuration.");
c.database.database_name = database_name.clone();