fix format and clippy warnings

This commit is contained in:
nathan
2022-01-23 12:14:00 +08:00
parent dccbd45a00
commit cfb589dda1
209 changed files with 2113 additions and 1296 deletions

View File

@ -20,7 +20,7 @@ where
// Err(format!("{:?}", e))
Err(InternalError::ProtobufError(format!("{:?}", e)).into())
},
}
}
}
}

View File

@ -11,17 +11,23 @@ use std::ops;
pub struct Data<T>(pub T);
impl<T> Data<T> {
pub fn into_inner(self) -> T { self.0 }
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> ops::Deref for Data<T> {
type Target = T;
fn deref(&self) -> &T { &self.0 }
fn deref(&self) -> &T {
&self.0
}
}
impl<T> ops::DerefMut for Data<T> {
fn deref_mut(&mut self) -> &mut T { &mut self.0 }
fn deref_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T> FromRequest for Data<T>
@ -60,7 +66,9 @@ where
T: FromBytes,
{
type Error = DispatchError;
fn try_from(payload: &Payload) -> Result<Data<T>, Self::Error> { parse_payload(payload) }
fn try_from(payload: &Payload) -> Result<Data<T>, Self::Error> {
parse_payload(payload)
}
}
impl<T> std::convert::TryFrom<Payload> for Data<T>
@ -68,7 +76,9 @@ where
T: FromBytes,
{
type Error = DispatchError;
fn try_from(payload: Payload) -> Result<Data<T>, Self::Error> { parse_payload(&payload) }
fn try_from(payload: Payload) -> Result<Data<T>, Self::Error> {
parse_payload(&payload)
}
}
fn parse_payload<T>(payload: &Payload) -> Result<Data<T>, DispatchError>
@ -80,7 +90,7 @@ where
Payload::Bytes(bytes) => {
let data = T::parse_from_bytes(bytes.clone())?;
Ok(Data(data))
},
}
}
}
@ -98,5 +108,7 @@ where
}
impl ToBytes for Data<String> {
fn into_bytes(self) -> Result<Bytes, DispatchError> { Ok(Bytes::from(self.0)) }
fn into_bytes(self) -> Result<Bytes, DispatchError> {
Ok(Bytes::from(self.0))
}
}

View File

@ -144,12 +144,12 @@ impl Service<DispatchContext> for DispatchService {
let fut = module.new_service(());
let service_fut = fut.await?.call(request);
service_fut.await
},
}
None => {
let msg = format!("Can not find the event handler. {:?}", request);
log::error!("{}", msg);
Err(InternalError::HandleNotFound(msg).into())
},
}
}
};

View File

@ -17,7 +17,9 @@ pub trait Error: fmt::Debug + DynClone + Send + Sync {
dyn_clone::clone_trait_object!(Error);
impl<T: Error + 'static> From<T> for DispatchError {
fn from(err: T) -> DispatchError { DispatchError { inner: Box::new(err) } }
fn from(err: T) -> DispatchError {
DispatchError { inner: Box::new(err) }
}
}
#[derive(Clone)]
@ -26,34 +28,50 @@ pub struct DispatchError {
}
impl DispatchError {
pub fn inner_error(&self) -> &dyn Error { self.inner.as_ref() }
pub fn inner_error(&self) -> &dyn Error {
self.inner.as_ref()
}
}
impl fmt::Display for DispatchError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", &self.inner) }
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", &self.inner)
}
}
impl fmt::Debug for DispatchError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", &self.inner) }
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", &self.inner)
}
}
impl std::error::Error for DispatchError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
fn cause(&self) -> Option<&dyn std::error::Error> { None }
fn cause(&self) -> Option<&dyn std::error::Error> {
None
}
}
impl From<SendError<EventRequest>> for DispatchError {
fn from(err: SendError<EventRequest>) -> Self { InternalError::Other(format!("{}", err)).into() }
fn from(err: SendError<EventRequest>) -> Self {
InternalError::Other(format!("{}", err)).into()
}
}
impl From<String> for DispatchError {
fn from(s: String) -> Self { InternalError::Other(s).into() }
fn from(s: String) -> Self {
InternalError::Other(s).into()
}
}
#[cfg(feature = "use_protobuf")]
impl From<protobuf::ProtobufError> for DispatchError {
fn from(e: protobuf::ProtobufError) -> Self { InternalError::ProtobufError(format!("{:?}", e)).into() }
fn from(e: protobuf::ProtobufError) -> Self {
InternalError::ProtobufError(format!("{:?}", e)).into()
}
}
impl FromBytes for DispatchError {
@ -64,7 +82,9 @@ impl FromBytes for DispatchError {
}
impl From<DispatchError> for EventResponse {
fn from(err: DispatchError) -> Self { err.inner_error().as_response() }
fn from(err: DispatchError) -> Self {
err.inner_error().as_response()
}
}
impl Serialize for DispatchError {
@ -109,5 +129,7 @@ impl Error for InternalError {
}
impl std::convert::From<JoinError> for InternalError {
fn from(e: JoinError) -> Self { InternalError::JoinError(format!("{}", e)) }
fn from(e: JoinError) -> Self {
InternalError::JoinError(format!("{}", e))
}
}

View File

@ -55,7 +55,9 @@ impl ModuleDataMap {
self.map.contains_key(&TypeId::of::<T>())
}
pub fn extend(&mut self, other: ModuleDataMap) { self.map.extend(other.map); }
pub fn extend(&mut self, other: ModuleDataMap) {
self.map.extend(other.map);
}
}
fn downcast_owned<T: 'static + Send + Sync>(boxed: Box<dyn Any + Send + Sync>) -> Option<T> {

View File

@ -11,9 +11,13 @@ impl<T> Unit<T>
where
T: Send + Sync,
{
pub fn new(data: T) -> Self { Unit(Arc::new(data)) }
pub fn new(data: T) -> Self {
Unit(Arc::new(data))
}
pub fn get_ref(&self) -> &T { self.0.as_ref() }
pub fn get_ref(&self) -> &T {
self.0.as_ref()
}
}
impl<T> Deref for Unit<T>
@ -22,21 +26,27 @@ where
{
type Target = Arc<T>;
fn deref(&self) -> &Arc<T> { &self.0 }
fn deref(&self) -> &Arc<T> {
&self.0
}
}
impl<T> Clone for Unit<T>
where
T: ?Sized + Send + Sync,
{
fn clone(&self) -> Unit<T> { Unit(self.0.clone()) }
fn clone(&self) -> Unit<T> {
Unit(self.0.clone())
}
}
impl<T> From<Arc<T>> for Unit<T>
where
T: ?Sized + Send + Sync,
{
fn from(arc: Arc<T>) -> Self { Unit(arc) }
fn from(arc: Arc<T>) -> Self {
Unit(arc)
}
}
impl<T> FromRequest for Unit<T>

View File

@ -17,14 +17,7 @@ use crate::{
request::{payload::Payload, EventRequest, FromRequest},
response::{EventResponse, Responder},
service::{
factory,
BoxService,
BoxServiceFactory,
Handler,
HandlerService,
Service,
ServiceFactory,
ServiceRequest,
factory, BoxService, BoxServiceFactory, Handler, HandlerService, Service, ServiceFactory, ServiceRequest,
ServiceResponse,
},
};
@ -48,7 +41,9 @@ pub(crate) fn as_module_map(modules: Vec<Module>) -> ModuleMap {
pub struct Event(String);
impl<T: Display + Eq + Hash + Debug + Clone> std::convert::From<T> for Event {
fn from(t: T) -> Self { Event(format!("{}", t)) }
fn from(t: T) -> Self {
Event(format!("{}", t))
}
}
pub type EventServiceFactory = BoxServiceFactory<(), ServiceRequest, ServiceResponse, DispatchError>;
@ -70,7 +65,9 @@ impl std::default::Default for Module {
}
impl Module {
pub fn new() -> Self { Module::default() }
pub fn new() -> Self {
Module::default()
}
pub fn name(mut self, s: &str) -> Self {
self.name = s.to_owned();
@ -103,7 +100,9 @@ impl Module {
self
}
pub fn events(&self) -> Vec<Event> { self.service_map.keys().cloned().collect::<Vec<_>>() }
pub fn events(&self) -> Vec<Event> {
self.service_map.keys().cloned().collect::<Vec<_>>()
}
}
#[derive(Debug, Clone)]
@ -135,7 +134,9 @@ impl ModuleRequest {
}
impl std::fmt::Display for ModuleRequest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}:{:?}", self.id, self.event) }
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{:?}", self.id, self.event)
}
}
impl ServiceFactory<ModuleRequest> for Module {
@ -185,11 +186,11 @@ impl Service<ModuleRequest> for ModuleService {
}),
};
Box::pin(async move { Ok(fut.await.unwrap_or_else(|e| e.into())) })
},
}
None => {
let msg = format!("Can not find service factory for event: {:?}", request.event);
Box::pin(async { Err(InternalError::ServiceNotFound(msg).into()) })
},
}
}
}
}

View File

@ -11,11 +11,15 @@ pub enum Payload {
}
impl std::fmt::Debug for Payload {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { format_payload_print(self, f) }
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
format_payload_print(self, f)
}
}
impl std::fmt::Display for Payload {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { format_payload_print(self, f) }
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
format_payload_print(self, f)
}
}
fn format_payload_print(payload: &Payload, f: &mut Formatter<'_>) -> fmt::Result {
@ -26,24 +30,36 @@ fn format_payload_print(payload: &Payload, f: &mut Formatter<'_>) -> fmt::Result
}
impl std::convert::From<String> for Payload {
fn from(s: String) -> Self { Payload::Bytes(Bytes::from(s)) }
fn from(s: String) -> Self {
Payload::Bytes(Bytes::from(s))
}
}
impl std::convert::From<&'_ String> for Payload {
fn from(s: &String) -> Self { Payload::Bytes(Bytes::from(s.to_owned())) }
fn from(s: &String) -> Self {
Payload::Bytes(Bytes::from(s.to_owned()))
}
}
impl std::convert::From<Bytes> for Payload {
fn from(bytes: Bytes) -> Self { Payload::Bytes(bytes) }
fn from(bytes: Bytes) -> Self {
Payload::Bytes(bytes)
}
}
impl std::convert::From<()> for Payload {
fn from(_: ()) -> Self { Payload::None }
fn from(_: ()) -> Self {
Payload::None
}
}
impl std::convert::From<Vec<u8>> for Payload {
fn from(bytes: Vec<u8>) -> Self { Payload::Bytes(Bytes::from(bytes)) }
fn from(bytes: Vec<u8>) -> Self {
Payload::Bytes(Bytes::from(bytes))
}
}
impl std::convert::From<&str> for Payload {
fn from(s: &str) -> Self { s.to_string().into() }
fn from(s: &str) -> Self {
s.to_string().into()
}
}

View File

@ -60,7 +60,9 @@ impl FromRequest for () {
type Error = DispatchError;
type Future = Ready<Result<(), DispatchError>>;
fn from_request(_req: &EventRequest, _payload: &mut Payload) -> Self::Future { ready(Ok(())) }
fn from_request(_req: &EventRequest, _payload: &mut Payload) -> Self::Future {
ready(Ok(()))
}
}
#[doc(hidden)]

View File

@ -6,7 +6,9 @@ use crate::{
macro_rules! static_response {
($name:ident, $status:expr) => {
#[allow(non_snake_case, missing_docs)]
pub fn $name() -> ResponseBuilder { ResponseBuilder::new($status) }
pub fn $name() -> ResponseBuilder {
ResponseBuilder::new($status)
}
};
}

View File

@ -13,7 +13,9 @@ pub trait Responder {
macro_rules! impl_responder {
($res: ty) => {
impl Responder for $res {
fn respond_to(self, _: &EventRequest) -> EventResponse { ResponseBuilder::Ok().data(self).build() }
fn respond_to(self, _: &EventRequest) -> EventResponse {
ResponseBuilder::Ok().data(self).build()
}
}
};
}

View File

@ -10,8 +10,8 @@ use std::{convert::TryFrom, fmt, fmt::Formatter};
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
pub enum StatusCode {
Ok = 0,
Err = 1,
Ok = 0,
Err = 1,
Internal = 2,
}
@ -40,11 +40,11 @@ impl EventResponse {
StatusCode::Ok => {
let data = <Data<T>>::try_from(self.payload)?;
Ok(Ok(data.into_inner()))
},
}
StatusCode::Err | StatusCode::Internal => {
let err = <Data<E>>::try_from(self.payload)?;
Ok(Err(err.into_inner()))
},
}
}
}
}
@ -64,7 +64,9 @@ impl std::fmt::Display for EventResponse {
impl Responder for EventResponse {
#[inline]
fn respond_to(self, _: &EventRequest) -> EventResponse { self }
fn respond_to(self, _: &EventRequest) -> EventResponse {
self
}
}
pub type DataResult<T, E> = std::result::Result<Data<T>, E>;

View File

@ -41,7 +41,9 @@ where
type Context = Cfg;
type Future = BoxFuture<'static, Result<Self::Service, Self::Error>>;
fn new_service(&self, cfg: Cfg) -> Self::Future { self.0.new_service(cfg) }
fn new_service(&self, cfg: Cfg) -> Self::Future {
self.0.new_service(cfg)
}
}
pub type BoxService<Req, Res, Err> =
@ -65,7 +67,9 @@ where
type Error = S::Error;
type Future = S::Future;
fn call(&self, request: Req) -> S::Future { (**self).call(request) }
fn call(&self, request: Req) -> S::Future {
(**self).call(request)
}
}
struct ServiceWrapper<S> {
@ -73,7 +77,9 @@ struct ServiceWrapper<S> {
}
impl<S> ServiceWrapper<S> {
fn new(inner: S) -> Self { Self { inner } }
fn new(inner: S) -> Self {
Self { inner }
}
}
impl<S, Req, Res, Err> Service<Req> for ServiceWrapper<S>
@ -85,7 +91,9 @@ where
type Error = Err;
type Future = BoxFuture<'static, Result<Res, Err>>;
fn call(&self, req: Req) -> Self::Future { Box::pin(self.inner.call(req)) }
fn call(&self, req: Req) -> Self::Future {
Box::pin(self.inner.call(req))
}
}
struct FactoryWrapper<SF>(SF);

View File

@ -78,7 +78,9 @@ where
type Context = ();
type Future = Ready<Result<Self::Service, Self::Error>>;
fn new_service(&self, _: ()) -> Self::Future { ready(Ok(self.clone())) }
fn new_service(&self, _: ()) -> Self::Future {
ready(Ok(self.clone()))
}
}
impl<H, T, R> Service<ServiceRequest> for HandlerService<H, T, R>
@ -129,21 +131,21 @@ where
let fut = handle.call(params);
let state = HandlerServiceFuture::Handle(fut, req.take());
self.as_mut().set(state);
},
}
Err(err) => {
let req = req.take().unwrap();
let system_err: DispatchError = err.into();
let res: EventResponse = system_err.into();
return Poll::Ready(Ok(ServiceResponse::new(req, res)));
},
}
};
},
}
HandlerServiceProj::Handle(fut, req) => {
let result = ready!(fut.poll(cx));
let req = req.take().unwrap();
let resp = result.respond_to(&req);
return Poll::Ready(Ok(ServiceResponse::new(req, resp)));
},
}
}
}
}

View File

@ -29,10 +29,14 @@ pub struct ServiceRequest {
}
impl ServiceRequest {
pub fn new(req: EventRequest, payload: Payload) -> Self { Self { req, payload } }
pub fn new(req: EventRequest, payload: Payload) -> Self {
Self { req, payload }
}
#[inline]
pub fn into_parts(self) -> (EventRequest, Payload) { (self.req, self.payload) }
pub fn into_parts(self) -> (EventRequest, Payload) {
(self.req, self.payload)
}
}
pub struct ServiceResponse {
@ -41,7 +45,11 @@ pub struct ServiceResponse {
}
impl ServiceResponse {
pub fn new(request: EventRequest, response: EventResponse) -> Self { ServiceResponse { request, response } }
pub fn new(request: EventRequest, response: EventResponse) -> Self {
ServiceResponse { request, response }
}
pub fn into_parts(self) -> (EventRequest, EventResponse) { (self.request, self.response) }
pub fn into_parts(self) -> (EventRequest, EventResponse) {
(self.request, self.response)
}
}

View File

@ -50,10 +50,10 @@ impl FlowySystem {
#[allow(dead_code)]
pub fn stop(&self) {
match self.sys_cmd_tx.send(SystemCommand::Exit(0)) {
Ok(_) => {},
Ok(_) => {}
Err(e) => {
log::error!("Stop system error: {}", e);
},
}
}
}
@ -89,7 +89,7 @@ impl Future for SystemController {
if let Some(tx) = self.stop_tx.take() {
let _ = tx.send(code);
}
},
}
},
}
}
@ -115,7 +115,7 @@ impl SystemRunner {
} else {
Ok(())
}
},
}
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
}
}

View File

@ -10,7 +10,9 @@ pub struct Ready<T> {
impl<T> Ready<T> {
#[inline]
pub fn into_inner(mut self) -> T { self.val.take().unwrap() }
pub fn into_inner(mut self) -> T {
self.val.take().unwrap()
}
}
impl<T> Unpin for Ready<T> {}
@ -25,4 +27,6 @@ impl<T> Future for Ready<T> {
}
}
pub fn ready<T>(val: T) -> Ready<T> { Ready { val: Some(val) } }
pub fn ready<T>(val: T) -> Ready<T> {
Ready { val: Some(val) }
}

View File

@ -1,7 +1,9 @@
use lib_dispatch::prelude::*;
use std::sync::Arc;
pub async fn hello() -> String { "say hello".to_string() }
pub async fn hello() -> String {
"say hello".to_string()
}
#[tokio::test]
async fn test() {