feat: run rustfmt with custom defined fmt configuration (#1848)

* chore: update rustfmt

* chore: apply rustfmt format
This commit is contained in:
Nathan.fooo
2023-02-13 09:29:49 +08:00
committed by GitHub
parent e2496e734c
commit 6bb1c4e89c
459 changed files with 50554 additions and 46600 deletions

View File

@ -1,155 +1,159 @@
use crate::FlowySDKTest;
use flowy_user::{entities::UserProfilePB, errors::FlowyError};
use lib_dispatch::prelude::{
AFPluginDispatcher, AFPluginEventResponse, AFPluginFromBytes, AFPluginRequest, StatusCode, ToBytes, *,
AFPluginDispatcher, AFPluginEventResponse, AFPluginFromBytes, AFPluginRequest, StatusCode,
ToBytes, *,
};
use std::{
convert::TryFrom,
fmt::{Debug, Display},
hash::Hash,
marker::PhantomData,
sync::Arc,
convert::TryFrom,
fmt::{Debug, Display},
hash::Hash,
marker::PhantomData,
sync::Arc,
};
pub type FolderEventBuilder = EventBuilder<FlowyError>;
impl FolderEventBuilder {
pub fn new(sdk: FlowySDKTest) -> Self {
EventBuilder::test(TestContext::new(sdk))
}
pub fn user_profile(&self) -> &Option<UserProfilePB> {
&self.user_profile
}
pub fn new(sdk: FlowySDKTest) -> Self {
EventBuilder::test(TestContext::new(sdk))
}
pub fn user_profile(&self) -> &Option<UserProfilePB> {
&self.user_profile
}
}
pub type UserModuleEventBuilder = FolderEventBuilder;
#[derive(Clone)]
pub struct EventBuilder<E> {
context: TestContext,
user_profile: Option<UserProfilePB>,
err_phantom: PhantomData<E>,
context: TestContext,
user_profile: Option<UserProfilePB>,
err_phantom: PhantomData<E>,
}
impl<E> EventBuilder<E>
where
E: AFPluginFromBytes + Debug,
E: AFPluginFromBytes + Debug,
{
fn test(context: TestContext) -> Self {
Self {
context,
user_profile: None,
err_phantom: PhantomData,
}
fn test(context: TestContext) -> Self {
Self {
context,
user_profile: None,
err_phantom: PhantomData,
}
}
pub fn payload<P>(mut self, payload: P) -> Self
where
P: ToBytes,
{
match payload.into_bytes() {
Ok(bytes) => {
let module_request = self.get_request();
self.context.request = Some(module_request.payload(bytes))
}
Err(e) => {
log::error!("Set payload failed: {:?}", e);
}
}
self
pub fn payload<P>(mut self, payload: P) -> Self
where
P: ToBytes,
{
match payload.into_bytes() {
Ok(bytes) => {
let module_request = self.get_request();
self.context.request = Some(module_request.payload(bytes))
},
Err(e) => {
log::error!("Set payload failed: {:?}", e);
},
}
self
}
pub fn event<Event>(mut self, event: Event) -> Self
where
Event: Eq + Hash + Debug + Clone + Display,
{
self.context.request = Some(AFPluginRequest::new(event));
self
}
pub fn event<Event>(mut self, event: Event) -> Self
where
Event: Eq + Hash + Debug + Clone + Display,
{
self.context.request = Some(AFPluginRequest::new(event));
self
}
pub fn sync_send(mut self) -> Self {
let request = self.get_request();
let resp = AFPluginDispatcher::sync_send(self.dispatch(), request);
self.context.response = Some(resp);
self
}
pub fn sync_send(mut self) -> Self {
let request = self.get_request();
let resp = AFPluginDispatcher::sync_send(self.dispatch(), request);
self.context.response = Some(resp);
self
}
pub async fn async_send(mut self) -> Self {
let request = self.get_request();
let resp = AFPluginDispatcher::async_send(self.dispatch(), request).await;
self.context.response = Some(resp);
self
}
pub async fn async_send(mut self) -> Self {
let request = self.get_request();
let resp = AFPluginDispatcher::async_send(self.dispatch(), request).await;
self.context.response = Some(resp);
self
}
pub fn parse<R>(self) -> R
where
R: AFPluginFromBytes,
{
let response = self.get_response();
match response.clone().parse::<R, E>() {
Ok(Ok(data)) => data,
Ok(Err(e)) => {
panic!(
"Parser {:?} failed: {:?}, response {:?}",
std::any::type_name::<R>(),
e,
response
)
}
Err(e) => panic!(
"Dispatch {:?} failed: {:?}, response {:?}",
std::any::type_name::<R>(),
e,
response
),
}
pub fn parse<R>(self) -> R
where
R: AFPluginFromBytes,
{
let response = self.get_response();
match response.clone().parse::<R, E>() {
Ok(Ok(data)) => data,
Ok(Err(e)) => {
panic!(
"Parser {:?} failed: {:?}, response {:?}",
std::any::type_name::<R>(),
e,
response
)
},
Err(e) => panic!(
"Dispatch {:?} failed: {:?}, response {:?}",
std::any::type_name::<R>(),
e,
response
),
}
}
pub fn error(self) -> E {
let response = self.get_response();
assert_eq!(response.status_code, StatusCode::Err);
<AFPluginData<E>>::try_from(response.payload).unwrap().into_inner()
}
pub fn error(self) -> E {
let response = self.get_response();
assert_eq!(response.status_code, StatusCode::Err);
<AFPluginData<E>>::try_from(response.payload)
.unwrap()
.into_inner()
}
pub fn assert_error(self) -> Self {
// self.context.assert_error();
self
}
pub fn assert_error(self) -> Self {
// self.context.assert_error();
self
}
pub fn assert_success(self) -> Self {
// self.context.assert_success();
self
}
pub fn assert_success(self) -> Self {
// self.context.assert_success();
self
}
fn dispatch(&self) -> Arc<AFPluginDispatcher> {
self.context.sdk.dispatcher()
}
fn dispatch(&self) -> Arc<AFPluginDispatcher> {
self.context.sdk.dispatcher()
}
fn get_response(&self) -> AFPluginEventResponse {
self.context
.response
.as_ref()
.expect("must call sync_send first")
.clone()
}
fn get_response(&self) -> AFPluginEventResponse {
self
.context
.response
.as_ref()
.expect("must call sync_send first")
.clone()
}
fn get_request(&mut self) -> AFPluginRequest {
self.context.request.take().expect("must call event first")
}
fn get_request(&mut self) -> AFPluginRequest {
self.context.request.take().expect("must call event first")
}
}
#[derive(Clone)]
pub struct TestContext {
pub sdk: FlowySDKTest,
request: Option<AFPluginRequest>,
response: Option<AFPluginEventResponse>,
pub sdk: FlowySDKTest,
request: Option<AFPluginRequest>,
response: Option<AFPluginEventResponse>,
}
impl TestContext {
pub fn new(sdk: FlowySDKTest) -> Self {
Self {
sdk,
request: None,
response: None,
}
pub fn new(sdk: FlowySDKTest) -> Self {
Self {
sdk,
request: None,
response: None,
}
}
}

View File

@ -1,231 +1,255 @@
use crate::prelude::*;
use flowy_folder::entities::WorkspaceIdPB;
use flowy_folder::{
entities::{
app::*,
view::*,
workspace::{CreateWorkspacePayloadPB, WorkspacePB},
},
event_map::FolderEvent::{CreateWorkspace, OpenWorkspace, *},
entities::{
app::*,
view::*,
workspace::{CreateWorkspacePayloadPB, WorkspacePB},
},
event_map::FolderEvent::{CreateWorkspace, OpenWorkspace, *},
};
use flowy_user::{
entities::{SignInPayloadPB, SignUpPayloadPB, UserProfilePB},
errors::FlowyError,
event_map::UserEvent::{InitUser, SignIn, SignOut, SignUp},
entities::{SignInPayloadPB, SignUpPayloadPB, UserProfilePB},
errors::FlowyError,
event_map::UserEvent::{InitUser, SignIn, SignOut, SignUp},
};
use lib_dispatch::prelude::{AFPluginDispatcher, AFPluginRequest, ToBytes};
use std::{fs, path::PathBuf, sync::Arc};
pub struct ViewTest {
pub sdk: FlowySDKTest,
pub workspace: WorkspacePB,
pub app: AppPB,
pub view: ViewPB,
pub sdk: FlowySDKTest,
pub workspace: WorkspacePB,
pub app: AppPB,
pub view: ViewPB,
}
impl ViewTest {
#[allow(dead_code)]
pub async fn new(
sdk: &FlowySDKTest,
data_format: ViewDataFormatPB,
layout: ViewLayoutTypePB,
data: Vec<u8>,
) -> Self {
let workspace = create_workspace(sdk, "Workspace", "").await;
open_workspace(sdk, &workspace.id).await;
let app = create_app(sdk, "App", "AppFlowy GitHub Project", &workspace.id).await;
let view = create_view(sdk, &app.id, data_format, layout, data).await;
Self {
sdk: sdk.clone(),
workspace,
app,
view,
}
}
pub async fn new_grid_view(sdk: &FlowySDKTest, data: Vec<u8>) -> Self {
Self::new(sdk, ViewDataFormatPB::DatabaseFormat, ViewLayoutTypePB::Grid, data).await
}
pub async fn new_board_view(sdk: &FlowySDKTest, data: Vec<u8>) -> Self {
Self::new(sdk, ViewDataFormatPB::DatabaseFormat, ViewLayoutTypePB::Board, data).await
}
pub async fn new_calendar_view(sdk: &FlowySDKTest, data: Vec<u8>) -> Self {
Self::new(sdk, ViewDataFormatPB::DatabaseFormat, ViewLayoutTypePB::Calendar, data).await
}
pub async fn new_document_view(sdk: &FlowySDKTest) -> Self {
let view_data_format = match sdk.document_version() {
DocumentVersionPB::V0 => ViewDataFormatPB::DeltaFormat,
DocumentVersionPB::V1 => ViewDataFormatPB::NodeFormat,
};
Self::new(sdk, view_data_format, ViewLayoutTypePB::Document, vec![]).await
}
}
async fn create_workspace(sdk: &FlowySDKTest, name: &str, desc: &str) -> WorkspacePB {
let request = CreateWorkspacePayloadPB {
name: name.to_owned(),
desc: desc.to_owned(),
};
FolderEventBuilder::new(sdk.clone())
.event(CreateWorkspace)
.payload(request)
.async_send()
.await
.parse::<WorkspacePB>()
}
async fn open_workspace(sdk: &FlowySDKTest, workspace_id: &str) {
let payload = WorkspaceIdPB {
value: Some(workspace_id.to_owned()),
};
let _ = FolderEventBuilder::new(sdk.clone())
.event(OpenWorkspace)
.payload(payload)
.async_send()
.await;
}
async fn create_app(sdk: &FlowySDKTest, name: &str, desc: &str, workspace_id: &str) -> AppPB {
let create_app_request = CreateAppPayloadPB {
workspace_id: workspace_id.to_owned(),
name: name.to_string(),
desc: desc.to_string(),
color_style: Default::default(),
};
FolderEventBuilder::new(sdk.clone())
.event(CreateApp)
.payload(create_app_request)
.async_send()
.await
.parse::<AppPB>()
}
async fn create_view(
#[allow(dead_code)]
pub async fn new(
sdk: &FlowySDKTest,
app_id: &str,
data_format: ViewDataFormatPB,
layout: ViewLayoutTypePB,
data: Vec<u8>,
) -> ViewPB {
let request = CreateViewPayloadPB {
belong_to_id: app_id.to_string(),
name: "View A".to_string(),
desc: "".to_string(),
thumbnail: Some("http://1.png".to_string()),
data_format,
layout,
initial_data: data,
};
) -> Self {
let workspace = create_workspace(sdk, "Workspace", "").await;
open_workspace(sdk, &workspace.id).await;
let app = create_app(sdk, "App", "AppFlowy GitHub Project", &workspace.id).await;
let view = create_view(sdk, &app.id, data_format, layout, data).await;
Self {
sdk: sdk.clone(),
workspace,
app,
view,
}
}
FolderEventBuilder::new(sdk.clone())
.event(CreateView)
.payload(request)
.async_send()
.await
.parse::<ViewPB>()
pub async fn new_grid_view(sdk: &FlowySDKTest, data: Vec<u8>) -> Self {
Self::new(
sdk,
ViewDataFormatPB::DatabaseFormat,
ViewLayoutTypePB::Grid,
data,
)
.await
}
pub async fn new_board_view(sdk: &FlowySDKTest, data: Vec<u8>) -> Self {
Self::new(
sdk,
ViewDataFormatPB::DatabaseFormat,
ViewLayoutTypePB::Board,
data,
)
.await
}
pub async fn new_calendar_view(sdk: &FlowySDKTest, data: Vec<u8>) -> Self {
Self::new(
sdk,
ViewDataFormatPB::DatabaseFormat,
ViewLayoutTypePB::Calendar,
data,
)
.await
}
pub async fn new_document_view(sdk: &FlowySDKTest) -> Self {
let view_data_format = match sdk.document_version() {
DocumentVersionPB::V0 => ViewDataFormatPB::DeltaFormat,
DocumentVersionPB::V1 => ViewDataFormatPB::NodeFormat,
};
Self::new(sdk, view_data_format, ViewLayoutTypePB::Document, vec![]).await
}
}
async fn create_workspace(sdk: &FlowySDKTest, name: &str, desc: &str) -> WorkspacePB {
let request = CreateWorkspacePayloadPB {
name: name.to_owned(),
desc: desc.to_owned(),
};
FolderEventBuilder::new(sdk.clone())
.event(CreateWorkspace)
.payload(request)
.async_send()
.await
.parse::<WorkspacePB>()
}
async fn open_workspace(sdk: &FlowySDKTest, workspace_id: &str) {
let payload = WorkspaceIdPB {
value: Some(workspace_id.to_owned()),
};
let _ = FolderEventBuilder::new(sdk.clone())
.event(OpenWorkspace)
.payload(payload)
.async_send()
.await;
}
async fn create_app(sdk: &FlowySDKTest, name: &str, desc: &str, workspace_id: &str) -> AppPB {
let create_app_request = CreateAppPayloadPB {
workspace_id: workspace_id.to_owned(),
name: name.to_string(),
desc: desc.to_string(),
color_style: Default::default(),
};
FolderEventBuilder::new(sdk.clone())
.event(CreateApp)
.payload(create_app_request)
.async_send()
.await
.parse::<AppPB>()
}
async fn create_view(
sdk: &FlowySDKTest,
app_id: &str,
data_format: ViewDataFormatPB,
layout: ViewLayoutTypePB,
data: Vec<u8>,
) -> ViewPB {
let request = CreateViewPayloadPB {
belong_to_id: app_id.to_string(),
name: "View A".to_string(),
desc: "".to_string(),
thumbnail: Some("http://1.png".to_string()),
data_format,
layout,
initial_data: data,
};
FolderEventBuilder::new(sdk.clone())
.event(CreateView)
.payload(request)
.async_send()
.await
.parse::<ViewPB>()
}
pub fn root_dir() -> String {
// https://doc.rust-lang.org/cargo/reference/environment-variables.html
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| "./".to_owned());
let mut path_buf = fs::canonicalize(&PathBuf::from(&manifest_dir)).unwrap();
path_buf.pop(); // rust-lib
path_buf.push("temp");
path_buf.push("flowy");
// https://doc.rust-lang.org/cargo/reference/environment-variables.html
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| "./".to_owned());
let mut path_buf = fs::canonicalize(&PathBuf::from(&manifest_dir)).unwrap();
path_buf.pop(); // rust-lib
path_buf.push("temp");
path_buf.push("flowy");
let root_dir = path_buf.to_str().unwrap().to_string();
if !std::path::Path::new(&root_dir).exists() {
std::fs::create_dir_all(&root_dir).unwrap();
}
root_dir
let root_dir = path_buf.to_str().unwrap().to_string();
if !std::path::Path::new(&root_dir).exists() {
std::fs::create_dir_all(&root_dir).unwrap();
}
root_dir
}
pub fn random_email() -> String {
format!("{}@appflowy.io", nanoid!(20))
format!("{}@appflowy.io", nanoid!(20))
}
pub fn login_email() -> String {
"annie2@appflowy.io".to_string()
"annie2@appflowy.io".to_string()
}
pub fn login_password() -> String {
"HelloWorld!123".to_string()
"HelloWorld!123".to_string()
}
pub struct SignUpContext {
pub user_profile: UserProfilePB,
pub password: String,
pub user_profile: UserProfilePB,
pub password: String,
}
pub fn sign_up(dispatch: Arc<AFPluginDispatcher>) -> SignUpContext {
let password = login_password();
let payload = SignUpPayloadPB {
email: random_email(),
name: "app flowy".to_string(),
password: password.clone(),
}
.into_bytes()
let password = login_password();
let payload = SignUpPayloadPB {
email: random_email(),
name: "app flowy".to_string(),
password: password.clone(),
}
.into_bytes()
.unwrap();
let request = AFPluginRequest::new(SignUp).payload(payload);
let user_profile = AFPluginDispatcher::sync_send(dispatch, request)
.parse::<UserProfilePB, FlowyError>()
.unwrap()
.unwrap();
let request = AFPluginRequest::new(SignUp).payload(payload);
let user_profile = AFPluginDispatcher::sync_send(dispatch, request)
.parse::<UserProfilePB, FlowyError>()
.unwrap()
.unwrap();
SignUpContext { user_profile, password }
SignUpContext {
user_profile,
password,
}
}
pub async fn async_sign_up(dispatch: Arc<AFPluginDispatcher>) -> SignUpContext {
let password = login_password();
let email = random_email();
let payload = SignUpPayloadPB {
email,
name: "app flowy".to_string(),
password: password.clone(),
}
.into_bytes()
let password = login_password();
let email = random_email();
let payload = SignUpPayloadPB {
email,
name: "app flowy".to_string(),
password: password.clone(),
}
.into_bytes()
.unwrap();
let request = AFPluginRequest::new(SignUp).payload(payload);
let user_profile = AFPluginDispatcher::async_send(dispatch.clone(), request)
.await
.parse::<UserProfilePB, FlowyError>()
.unwrap()
.unwrap();
let request = AFPluginRequest::new(SignUp).payload(payload);
let user_profile = AFPluginDispatcher::async_send(dispatch.clone(), request)
.await
.parse::<UserProfilePB, FlowyError>()
.unwrap()
.unwrap();
// let _ = create_default_workspace_if_need(dispatch.clone(), &user_profile.id);
SignUpContext { user_profile, password }
// let _ = create_default_workspace_if_need(dispatch.clone(), &user_profile.id);
SignUpContext {
user_profile,
password,
}
}
pub async fn init_user_setting(dispatch: Arc<AFPluginDispatcher>) {
let request = AFPluginRequest::new(InitUser);
let _ = AFPluginDispatcher::async_send(dispatch.clone(), request).await;
let request = AFPluginRequest::new(InitUser);
let _ = AFPluginDispatcher::async_send(dispatch.clone(), request).await;
}
#[allow(dead_code)]
fn sign_in(dispatch: Arc<AFPluginDispatcher>) -> UserProfilePB {
let payload = SignInPayloadPB {
email: login_email(),
password: login_password(),
name: "rust".to_owned(),
}
.into_bytes()
.unwrap();
let payload = SignInPayloadPB {
email: login_email(),
password: login_password(),
name: "rust".to_owned(),
}
.into_bytes()
.unwrap();
let request = AFPluginRequest::new(SignIn).payload(payload);
AFPluginDispatcher::sync_send(dispatch, request)
.parse::<UserProfilePB, FlowyError>()
.unwrap()
.unwrap()
let request = AFPluginRequest::new(SignIn).payload(payload);
AFPluginDispatcher::sync_send(dispatch, request)
.parse::<UserProfilePB, FlowyError>()
.unwrap()
.unwrap()
}
#[allow(dead_code)]
fn logout(dispatch: Arc<AFPluginDispatcher>) {
let _ = AFPluginDispatcher::sync_send(dispatch, AFPluginRequest::new(SignOut));
let _ = AFPluginDispatcher::sync_send(dispatch, AFPluginRequest::new(SignOut));
}

View File

@ -10,51 +10,53 @@ use flowy_user::entities::UserProfilePB;
use nanoid::nanoid;
pub mod prelude {
pub use crate::{event_builder::*, helper::*, *};
pub use lib_dispatch::prelude::*;
pub use crate::{event_builder::*, helper::*, *};
pub use lib_dispatch::prelude::*;
}
#[derive(Clone)]
pub struct FlowySDKTest {
pub inner: AppFlowyCore,
pub inner: AppFlowyCore,
}
impl std::ops::Deref for FlowySDKTest {
type Target = AppFlowyCore;
type Target = AppFlowyCore;
fn deref(&self) -> &Self::Target {
&self.inner
}
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl std::default::Default for FlowySDKTest {
fn default() -> Self {
Self::new(DocumentVersionPB::V0)
}
fn default() -> Self {
Self::new(DocumentVersionPB::V0)
}
}
impl FlowySDKTest {
pub fn new(document_version: DocumentVersionPB) -> Self {
let server_config = get_client_server_configuration().unwrap();
let config = AppFlowyCoreConfig::new(&root_dir(), nanoid!(6), server_config)
.with_document_version(document_version)
.log_filter("info", vec![]);
let sdk = std::thread::spawn(|| AppFlowyCore::new(config)).join().unwrap();
std::mem::forget(sdk.dispatcher());
Self { inner: sdk }
}
pub fn new(document_version: DocumentVersionPB) -> Self {
let server_config = get_client_server_configuration().unwrap();
let config = AppFlowyCoreConfig::new(&root_dir(), nanoid!(6), server_config)
.with_document_version(document_version)
.log_filter("info", vec![]);
let sdk = std::thread::spawn(|| AppFlowyCore::new(config))
.join()
.unwrap();
std::mem::forget(sdk.dispatcher());
Self { inner: sdk }
}
pub async fn sign_up(&self) -> SignUpContext {
async_sign_up(self.inner.dispatcher()).await
}
pub async fn sign_up(&self) -> SignUpContext {
async_sign_up(self.inner.dispatcher()).await
}
pub async fn init_user(&self) -> UserProfilePB {
let context = async_sign_up(self.inner.dispatcher()).await;
init_user_setting(self.inner.dispatcher()).await;
context.user_profile
}
pub async fn init_user(&self) -> UserProfilePB {
let context = async_sign_up(self.inner.dispatcher()).await;
init_user_setting(self.inner.dispatcher()).await;
context.user_profile
}
pub fn document_version(&self) -> DocumentVersionPB {
self.inner.config.document.version.clone()
}
pub fn document_version(&self) -> DocumentVersionPB {
self.inner.config.document.version.clone()
}
}