Compare commits
2 Commits
d47f62cdbb
...
a5c975d113
Author | SHA1 | Date | |
---|---|---|---|
a5c975d113 | |||
ef1faf5bd9 |
21
Cargo.lock
generated
21
Cargo.lock
generated
@ -306,6 +306,7 @@ dependencies = [
|
||||
"ron",
|
||||
"serde",
|
||||
"syn",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"tokio-test",
|
||||
]
|
||||
@ -973,6 +974,26 @@ version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.61"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.61"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "1.1.8"
|
||||
|
@ -29,6 +29,7 @@ tokio-test = "0.4.4"
|
||||
env_logger = { version = "0.11.3" }
|
||||
log = { version = "0.4.21" }
|
||||
console-subscriber = "0.3.0"
|
||||
thiserror = "1.0.61"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
44
src/lib.rs
44
src/lib.rs
@ -352,39 +352,13 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
|
||||
|
||||
// Create an error and result type for sending messages
|
||||
let error_enum = quote! {
|
||||
#[derive(::std::fmt::Debug)]
|
||||
#[derive(::std::fmt::Debug, ::std::clone::Clone, ::std::fmt::Display, ::thiserror::Error)]
|
||||
#vis enum #error_enum_name {
|
||||
#[error("Failed to send query: {0}")]
|
||||
SendError(::tokio::sync::mpsc::error::SendError<(u64, #question_enum_name)>),
|
||||
#[error("Connection to sender thread closed")]
|
||||
Closed,
|
||||
}
|
||||
impl ::std::fmt::Display for #error_enum_name {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
|
||||
match self {
|
||||
#error_enum_name::SendError(e) => write!(f, "Failed to send query: {}", e),
|
||||
#error_enum_name::Closed => write!(f, "Connection closed"),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl ::std::error::Error for #error_enum_name {
|
||||
fn source(&self) -> ::std::option::Option<&(dyn ::std::error::Error + 'static)> {
|
||||
match self {
|
||||
#error_enum_name::SendError(e) => ::std::option::Option::Some(e),
|
||||
#error_enum_name::Closed => ::std::option::Option::None,
|
||||
}
|
||||
}
|
||||
fn description(&self) -> &str {
|
||||
match self {
|
||||
#error_enum_name::SendError(_) => "Failed to send query",
|
||||
#error_enum_name::Closed => "Connection closed",
|
||||
}
|
||||
}
|
||||
fn cause(&self) -> ::std::option::Option<&dyn ::std::error::Error> {
|
||||
match self {
|
||||
#error_enum_name::SendError(e) => ::std::option::Option::Some(e),
|
||||
#error_enum_name::Closed => ::std::option::Option::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
// Create enums for the types of messages the server and client will use
|
||||
|
||||
@ -565,21 +539,21 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
|
||||
}
|
||||
|
||||
pub fn insert(&self, nonce: u64, query: #query_enum_name) {
|
||||
self.queries.lock().unwrap().insert(nonce, query);
|
||||
self.queries.lock().expect("Could not insert new query; Mutex has been poisoned.").insert(nonce, query);
|
||||
}
|
||||
|
||||
pub fn get(&self, nonce: &u64) -> ::std::option::Option<#query_enum_name> {
|
||||
self.queries.lock().unwrap().get(nonce).cloned()
|
||||
self.queries.lock().expect("Could not get query; Mutex has been poisoned.").get(nonce).cloned()
|
||||
}
|
||||
|
||||
pub fn set_answer(&self, nonce: u64, answer: #answer_enum_name) {
|
||||
if let ::std::option::Option::Some(query) = self.queries.lock().unwrap().get_mut(&nonce) {
|
||||
if let ::std::option::Option::Some(query) = self.queries.lock().expect("Could not set answer to query; Mutex has been poisoned.").get_mut(&nonce) {
|
||||
query.set_answer(answer);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.queries.lock().unwrap().len()
|
||||
self.queries.lock().expect("Could not get query count; Mutex has been poisoned.").len()
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -638,7 +612,9 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
|
||||
} else {
|
||||
match self.received.send(response).await {
|
||||
::std::result::Result::Ok(_) => {},
|
||||
::std::result::Result::Err(e) => #error("Failed to send received answer to : {:?}", e),
|
||||
::std::result::Result::Err(e) => {
|
||||
#error("Failed to send received answer to Client: {:?}", e);
|
||||
},
|
||||
};
|
||||
}
|
||||
buf.drain(0..(4 + len as usize));
|
||||
|
Loading…
Reference in New Issue
Block a user