Add essential packets
This commit is contained in:
parent
286ace55b4
commit
7458eac931
6 changed files with 190 additions and 55 deletions
|
|
@ -1,9 +1,36 @@
|
|||
// Use `pub(crate) use <macro_name>` to make a macro importable from elsewhere.
|
||||
// See https://stackoverflow.com/a/31749071
|
||||
|
||||
macro_rules! id_alias {
|
||||
($name:ident) => {
|
||||
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
|
||||
pub struct $name(Id);
|
||||
|
||||
impl $name {
|
||||
pub fn of(str: &str) -> Self {
|
||||
Self(Id::of(str))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for $name {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use id_alias;
|
||||
|
||||
macro_rules! packets {
|
||||
( $( $name:ident($cmd:ident, $rpl:ident), )* ) => {
|
||||
(
|
||||
$( cmd $cmdName:ident($cmd:ident, $rpl:ident), )* // Commands with reply
|
||||
$( ntf $ntfName:ident($ntf:ident), )* // Notifications
|
||||
) => {
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(tag = "name", content = "data")]
|
||||
pub enum Cmd {
|
||||
$( $name($cmd), )*
|
||||
$( $cmdName($cmd), )*
|
||||
}
|
||||
|
||||
$(
|
||||
|
|
@ -11,7 +38,7 @@ macro_rules! packets {
|
|||
type Error = ();
|
||||
fn try_from(cmd: Cmd) -> Result<Self, Self::Error> {
|
||||
match cmd {
|
||||
Cmd::$name(val) => Ok(val),
|
||||
Cmd::$cmdName(val) => Ok(val),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
|
|
@ -21,7 +48,7 @@ macro_rules! packets {
|
|||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(tag = "name", content = "data")]
|
||||
pub enum Rpl {
|
||||
$( $name($rpl), )*
|
||||
$( $cmdName($rpl), )*
|
||||
}
|
||||
|
||||
$(
|
||||
|
|
@ -29,7 +56,25 @@ macro_rules! packets {
|
|||
type Error = ();
|
||||
fn try_from(rpl: Rpl) -> Result<Self, Self::Error> {
|
||||
match rpl {
|
||||
Rpl::$name(val) => Ok(val),
|
||||
Rpl::$cmdName(val) => Ok(val),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
)*
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(tag = "name", content = "data")]
|
||||
pub enum Ntf {
|
||||
$( $ntfName($ntf), )*
|
||||
}
|
||||
|
||||
$(
|
||||
impl std::convert::TryFrom<Ntf> for $ntf {
|
||||
type Error = ();
|
||||
fn try_from(ntf: Ntf) -> Result<Self, Self::Error> {
|
||||
match ntf {
|
||||
Ntf::$ntfName(val) => Ok(val),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
|
|
@ -38,6 +83,4 @@ macro_rules! packets {
|
|||
};
|
||||
}
|
||||
|
||||
// Make macro importable from elsewhere
|
||||
// See https://stackoverflow.com/a/31749071
|
||||
pub(crate) use packets;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue