mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: run rustfmt with custom defined fmt configuration (#1848)
* chore: update rustfmt * chore: apply rustfmt format
This commit is contained in:
@ -1,3 +1,3 @@
|
||||
fn main() {
|
||||
flowy_codegen::protobuf_file::gen(env!("CARGO_PKG_NAME"));
|
||||
flowy_codegen::protobuf_file::gen(env!("CARGO_PKG_NAME"));
|
||||
}
|
||||
|
@ -3,45 +3,45 @@ use std::{fmt, fmt::Formatter};
|
||||
|
||||
#[derive(Debug, Clone, ProtoBuf, serde::Serialize)]
|
||||
pub struct SubscribeObject {
|
||||
#[pb(index = 1)]
|
||||
pub source: String,
|
||||
#[pb(index = 1)]
|
||||
pub source: String,
|
||||
|
||||
#[pb(index = 2)]
|
||||
pub ty: i32,
|
||||
#[pb(index = 2)]
|
||||
pub ty: i32,
|
||||
|
||||
#[pb(index = 3)]
|
||||
pub id: String,
|
||||
#[pb(index = 3)]
|
||||
pub id: String,
|
||||
|
||||
#[pb(index = 4, one_of)]
|
||||
pub payload: Option<Vec<u8>>,
|
||||
#[pb(index = 4, one_of)]
|
||||
pub payload: Option<Vec<u8>>,
|
||||
|
||||
#[pb(index = 5, one_of)]
|
||||
pub error: Option<Vec<u8>>,
|
||||
#[pb(index = 5, one_of)]
|
||||
pub error: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for SubscribeObject {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(&format!("{} changed: ", &self.source))?;
|
||||
if let Some(payload) = &self.payload {
|
||||
f.write_str(&format!("send {} payload", payload.len()))?;
|
||||
}
|
||||
|
||||
if let Some(payload) = &self.error {
|
||||
f.write_str(&format!("receive {} error", payload.len()))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(&format!("{} changed: ", &self.source))?;
|
||||
if let Some(payload) = &self.payload {
|
||||
f.write_str(&format!("send {} payload", payload.len()))?;
|
||||
}
|
||||
|
||||
if let Some(payload) = &self.error {
|
||||
f.write_str(&format!("receive {} error", payload.len()))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::default::Default for SubscribeObject {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
source: "".to_string(),
|
||||
ty: 0,
|
||||
id: "".to_string(),
|
||||
payload: None,
|
||||
error: None,
|
||||
}
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
source: "".to_string(),
|
||||
ty: 0,
|
||||
id: "".to_string(),
|
||||
payload: None,
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,87 +8,87 @@ use lib_dispatch::prelude::ToBytes;
|
||||
use std::sync::RwLock;
|
||||
|
||||
lazy_static! {
|
||||
static ref NOTIFICATION_SENDER: RwLock<Vec<Box<dyn NotificationSender>>> = RwLock::new(vec![]);
|
||||
static ref NOTIFICATION_SENDER: RwLock<Vec<Box<dyn NotificationSender>>> = RwLock::new(vec![]);
|
||||
}
|
||||
|
||||
pub fn register_notification_sender<T: NotificationSender>(sender: T) {
|
||||
let box_sender = Box::new(sender);
|
||||
match NOTIFICATION_SENDER.write() {
|
||||
Ok(mut write_guard) => write_guard.push(box_sender),
|
||||
Err(err) => tracing::error!("Failed to push notification sender: {:?}", err),
|
||||
}
|
||||
let box_sender = Box::new(sender);
|
||||
match NOTIFICATION_SENDER.write() {
|
||||
Ok(mut write_guard) => write_guard.push(box_sender),
|
||||
Err(err) => tracing::error!("Failed to push notification sender: {:?}", err),
|
||||
}
|
||||
}
|
||||
|
||||
pub trait NotificationSender: Send + Sync + 'static {
|
||||
fn send_subject(&self, subject: SubscribeObject) -> Result<(), String>;
|
||||
fn send_subject(&self, subject: SubscribeObject) -> Result<(), String>;
|
||||
}
|
||||
|
||||
pub struct NotificationBuilder {
|
||||
id: String,
|
||||
payload: Option<Bytes>,
|
||||
error: Option<Bytes>,
|
||||
source: String,
|
||||
ty: i32,
|
||||
id: String,
|
||||
payload: Option<Bytes>,
|
||||
error: Option<Bytes>,
|
||||
source: String,
|
||||
ty: i32,
|
||||
}
|
||||
|
||||
impl NotificationBuilder {
|
||||
pub fn new<T: Into<i32>>(id: &str, ty: T, source: &str) -> Self {
|
||||
Self {
|
||||
id: id.to_owned(),
|
||||
ty: ty.into(),
|
||||
payload: None,
|
||||
error: None,
|
||||
source: source.to_owned(),
|
||||
}
|
||||
pub fn new<T: Into<i32>>(id: &str, ty: T, source: &str) -> Self {
|
||||
Self {
|
||||
id: id.to_owned(),
|
||||
ty: ty.into(),
|
||||
payload: None,
|
||||
error: None,
|
||||
source: source.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn payload<T>(mut self, payload: T) -> Self
|
||||
where
|
||||
T: ToBytes,
|
||||
{
|
||||
match payload.into_bytes() {
|
||||
Ok(bytes) => self.payload = Some(bytes),
|
||||
Err(e) => {
|
||||
tracing::error!("Set observable payload failed: {:?}", e);
|
||||
},
|
||||
}
|
||||
|
||||
pub fn payload<T>(mut self, payload: T) -> Self
|
||||
where
|
||||
T: ToBytes,
|
||||
{
|
||||
match payload.into_bytes() {
|
||||
Ok(bytes) => self.payload = Some(bytes),
|
||||
Err(e) => {
|
||||
tracing::error!("Set observable payload failed: {:?}", e);
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
self
|
||||
pub fn error<T>(mut self, error: T) -> Self
|
||||
where
|
||||
T: ToBytes,
|
||||
{
|
||||
match error.into_bytes() {
|
||||
Ok(bytes) => self.error = Some(bytes),
|
||||
Err(e) => {
|
||||
tracing::error!("Set observable error failed: {:?}", e);
|
||||
},
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn error<T>(mut self, error: T) -> Self
|
||||
where
|
||||
T: ToBytes,
|
||||
{
|
||||
match error.into_bytes() {
|
||||
Ok(bytes) => self.error = Some(bytes),
|
||||
Err(e) => {
|
||||
tracing::error!("Set observable error failed: {:?}", e);
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn send(self) {
|
||||
let payload = self.payload.map(|bytes| bytes.to_vec());
|
||||
let error = self.error.map(|bytes| bytes.to_vec());
|
||||
let subject = SubscribeObject {
|
||||
source: self.source,
|
||||
ty: self.ty,
|
||||
id: self.id,
|
||||
payload,
|
||||
error,
|
||||
};
|
||||
|
||||
match NOTIFICATION_SENDER.read() {
|
||||
Ok(read_guard) => read_guard.iter().for_each(|sender| {
|
||||
if let Err(e) = sender.send_subject(subject.clone()) {
|
||||
tracing::error!("Post notification failed: {}", e);
|
||||
}
|
||||
}),
|
||||
Err(err) => {
|
||||
tracing::error!("Read notification sender failed: {}", err);
|
||||
}
|
||||
pub fn send(self) {
|
||||
let payload = self.payload.map(|bytes| bytes.to_vec());
|
||||
let error = self.error.map(|bytes| bytes.to_vec());
|
||||
let subject = SubscribeObject {
|
||||
source: self.source,
|
||||
ty: self.ty,
|
||||
id: self.id,
|
||||
payload,
|
||||
error,
|
||||
};
|
||||
|
||||
match NOTIFICATION_SENDER.read() {
|
||||
Ok(read_guard) => read_guard.iter().for_each(|sender| {
|
||||
if let Err(e) = sender.send_subject(subject.clone()) {
|
||||
tracing::error!("Post notification failed: {}", e);
|
||||
}
|
||||
}),
|
||||
Err(err) => {
|
||||
tracing::error!("Read notification sender failed: {}", err);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user