Skip to content

Commit

Permalink
Merge pull request #1 from mycognosist/cargo_fmt
Browse files Browse the repository at this point in the history
Apply idiomatic formatting with cargo fmt
  • Loading branch information
mycognosist authored Jun 29, 2023
2 parents 2291cc3 + 68b6f04 commit c86dda3
Show file tree
Hide file tree
Showing 7 changed files with 1,432 additions and 1,167 deletions.
113 changes: 59 additions & 54 deletions examples/chat.rs
Original file line number Diff line number Diff line change
@@ -1,64 +1,69 @@
use async_std::{prelude::*,io,task,net};
use cable::{Cable,MemoryStore,ChannelOptions};
use async_std::{io, net, prelude::*, task};
use cable::{Cable, ChannelOptions, MemoryStore};

type Error = Box<dyn std::error::Error+Send+Sync+'static>;
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

fn main() -> Result<(),Error> {
let (args,argv) = argmap::parse(std::env::args());
fn main() -> Result<(), Error> {
let (args, argv) = argmap::parse(std::env::args());

task::block_on(async move {
let store = MemoryStore::default();
let cable = Cable::new(store);
{
let opts = ChannelOptions {
channel: "default".as_bytes().to_vec(),
time_start: 0,
//time_end: now(),
time_end: 0,
limit: 20,
};
let mut client = cable.clone();
task::spawn(async move {
let mut msg_stream = client.open_channel(&opts).await.unwrap();
while let Some(msg) = msg_stream.next().await {
println!["msg={:?}", msg];
task::block_on(async move {
let store = MemoryStore::default();
let cable = Cable::new(store);
{
let opts = ChannelOptions {
channel: "default".as_bytes().to_vec(),
time_start: 0,
//time_end: now(),
time_end: 0,
limit: 20,
};
let mut client = cable.clone();
task::spawn(async move {
let mut msg_stream = client.open_channel(&opts).await.unwrap();
while let Some(msg) = msg_stream.next().await {
println!["msg={:?}", msg];
}
});
}
});
}
{
let mut client = cable.clone();
task::spawn(async move {
let stdin = io::stdin();
let mut line = String::new();
loop {
stdin.read_line(&mut line).await.unwrap();
if line.is_empty() { break }
let channel = "default".as_bytes();
let text = line.trim_end().as_bytes();
client.post_text(channel, &text).await.unwrap();
line.clear();
{
let mut client = cable.clone();
task::spawn(async move {
let stdin = io::stdin();
let mut line = String::new();
loop {
stdin.read_line(&mut line).await.unwrap();
if line.is_empty() {
break;
}
let channel = "default".as_bytes();
let text = line.trim_end().as_bytes();
client.post_text(channel, &text).await.unwrap();
line.clear();
}
});
}
});
}

if let Some(port) = argv.get("l").and_then(|x| x.first()) {
let listener = net::TcpListener::bind(format!["0.0.0.0:{}",port]).await?;
let mut incoming = listener.incoming();
while let Some(rstream) = incoming.next().await {
let stream = rstream.unwrap();
let client = cable.clone();
task::spawn(async move {
client.listen(stream).await.unwrap();
});
}
} else if let Some(addr) = args.get(1) {
let stream = net::TcpStream::connect(addr).await?;
cable.listen(stream).await?;
}
Ok(())
})
if let Some(port) = argv.get("l").and_then(|x| x.first()) {
let listener = net::TcpListener::bind(format!["0.0.0.0:{}", port]).await?;
let mut incoming = listener.incoming();
while let Some(rstream) = incoming.next().await {
let stream = rstream.unwrap();
let client = cable.clone();
task::spawn(async move {
client.listen(stream).await.unwrap();
});
}
} else if let Some(addr) = args.get(1) {
let stream = net::TcpStream::connect(addr).await?;
cable.listen(stream).await?;
}
Ok(())
})
}

fn _now() -> u64 {
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs()
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs()
}
109 changes: 62 additions & 47 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,80 @@ use std::backtrace::Backtrace;

#[derive(Debug)]
pub struct CableError {
kind: CableErrorKind,
backtrace: Backtrace,
kind: CableErrorKind,
backtrace: Backtrace,
}

#[derive(Debug)]
pub enum CableErrorKind {
DstTooSmall { provided: usize, required: usize },
MessageEmpty {},
MessageWriteUnrecognizedType { msg_type: u64 },
MessageHashResponseEnd {},
MessageDataResponseEnd {},
MessageHashRequestEnd {},
MessageCancelRequestEnd {},
MessageChannelTimeRangeRequestEnd {},
MessageChannelStateRequestEnd {},
MessageChannelListRequestEnd {},
PostWriteUnrecognizedType { post_type: u64 },
DstTooSmall { provided: usize, required: usize },
MessageEmpty {},
MessageWriteUnrecognizedType { msg_type: u64 },
MessageHashResponseEnd {},
MessageDataResponseEnd {},
MessageHashRequestEnd {},
MessageCancelRequestEnd {},
MessageChannelTimeRangeRequestEnd {},
MessageChannelStateRequestEnd {},
MessageChannelListRequestEnd {},
PostWriteUnrecognizedType { post_type: u64 },
}

impl CableErrorKind {
pub fn raise<T>(self) -> Result<T,Error> {
Err(Box::new(CableError {
kind: self,
backtrace: Backtrace::capture(),
}))
}
pub fn raise<T>(self) -> Result<T, Error> {
Err(Box::new(CableError {
kind: self,
backtrace: Backtrace::capture(),
}))
}
}

impl std::error::Error for CableError {
fn backtrace<'a>(&'a self) -> Option<&'a Backtrace> {
Some(&self.backtrace)
}
fn backtrace<'a>(&'a self) -> Option<&'a Backtrace> {
Some(&self.backtrace)
}
}

impl std::fmt::Display for CableError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.kind {
CableErrorKind::MessageEmpty {} => {
write![f, "empty message"]
},
CableErrorKind::MessageWriteUnrecognizedType { msg_type } => {
write![f, "cannot write unrecognized msg_type={}", msg_type]
},
CableErrorKind::DstTooSmall { provided, required } => {
write![f, "destination buffer too small. {} bytes required, {} provided",
required, provided]
},
CableErrorKind::MessageHashResponseEnd {} => { write![f, "unexpected end of HashResponse"] },
CableErrorKind::MessageDataResponseEnd {} => { write![f, "unexpected end of DataResponse"] },
CableErrorKind::MessageHashRequestEnd {} => { write![f, "unexpected end of HashRequest"] },
CableErrorKind::MessageCancelRequestEnd {} => { write![f, "unexpected end of CancelRequest"] },
CableErrorKind::MessageChannelTimeRangeRequestEnd {} => {
write![f, "unexpected end of ChannelTimeRangeRequest"]
},
CableErrorKind::MessageChannelStateRequestEnd {} => { write![f, "unexpected end of ChannelStateRequest"] },
CableErrorKind::MessageChannelListRequestEnd {} => { write![f, "unexpected end of ChannelListRequest"] },
CableErrorKind::PostWriteUnrecognizedType { post_type } => {
write![f, "cannot write unrecognized post_type={}", post_type]
},
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.kind {
CableErrorKind::MessageEmpty {} => {
write![f, "empty message"]
}
CableErrorKind::MessageWriteUnrecognizedType { msg_type } => {
write![f, "cannot write unrecognized msg_type={}", msg_type]
}
CableErrorKind::DstTooSmall { provided, required } => {
write![
f,
"destination buffer too small. {} bytes required, {} provided",
required, provided
]
}
CableErrorKind::MessageHashResponseEnd {} => {
write![f, "unexpected end of HashResponse"]
}
CableErrorKind::MessageDataResponseEnd {} => {
write![f, "unexpected end of DataResponse"]
}
CableErrorKind::MessageHashRequestEnd {} => {
write![f, "unexpected end of HashRequest"]
}
CableErrorKind::MessageCancelRequestEnd {} => {
write![f, "unexpected end of CancelRequest"]
}
CableErrorKind::MessageChannelTimeRangeRequestEnd {} => {
write![f, "unexpected end of ChannelTimeRangeRequest"]
}
CableErrorKind::MessageChannelStateRequestEnd {} => {
write![f, "unexpected end of ChannelStateRequest"]
}
CableErrorKind::MessageChannelListRequestEnd {} => {
write![f, "unexpected end of ChannelListRequest"]
}
CableErrorKind::PostWriteUnrecognizedType { post_type } => {
write![f, "cannot write unrecognized post_type={}", post_type]
}
}
}
}
}
Loading

0 comments on commit c86dda3

Please sign in to comment.