diff options
| author | Max Audron <me@audron.dev> | 2026-02-21 13:32:00 +0100 |
|---|---|---|
| committer | Max Audron <me@audron.dev> | 2026-02-21 13:32:00 +0100 |
| commit | 8882c121f83cf4513eaee7515d6dcea133a65d69 (patch) | |
| tree | e2818c5d99f209159fd904e0c75d4bc30c262e82 /src | |
| parent | remove old readme.org (diff) | |
replace all match_variant instances with try_into
the match_variant macro was unclear, unreadable and no longer needed as
we have automaticly derived from implementations for all Variant enum
fields now
Diffstat (limited to '')
26 files changed, 111 insertions, 134 deletions
diff --git a/src/message/handshake/clientinit.rs b/src/message/handshake/clientinit.rs index 32e8595..94d52cf 100644 --- a/src/message/handshake/clientinit.rs +++ b/src/message/handshake/clientinit.rs @@ -64,12 +64,12 @@ impl HandshakeSerialize for ClientInit { } impl From<VariantMap> for ClientInit { - fn from(input: VariantMap) -> Self { + fn from(mut input: VariantMap) -> Self { ClientInit { - client_version: match_variant!(input.get("ClientVersion").unwrap(), Variant::String), - client_date: match_variant!(input.get("ClientDate").unwrap(), Variant::String), - client_features: match_variant!(input.get("Features").unwrap(), Variant::u32), - feature_list: match_variant!(input.get("FeatureList").unwrap(), Variant::StringList), + client_version: input.remove("ClientVersion").unwrap().try_into().unwrap(), + client_date: input.remove("ClientDate").unwrap().try_into().unwrap(), + client_features: input.remove("Features").unwrap().try_into().unwrap(), + feature_list: input.remove("FeatureList").unwrap().try_into().unwrap(), } } } diff --git a/src/message/handshake/clientinitack.rs b/src/message/handshake/clientinitack.rs index 610cdc0..21e65de 100644 --- a/src/message/handshake/clientinitack.rs +++ b/src/message/handshake/clientinitack.rs @@ -50,11 +50,11 @@ impl From<VariantMap> for ClientInitAck { ClientInitAck { // TODO make this compatible with older clients core_features: 0, - core_configured: match_variant!(input.get("Configured").unwrap(), Variant::bool), - storage_backends: match_variant!(input.get("StorageBackends").unwrap(), Variant::VariantList), + core_configured: input.get("Configured").unwrap().try_into().unwrap(), + storage_backends: input.get("StorageBackends").unwrap().try_into().unwrap(), #[cfg(feature = "authenticators")] - authenticators: match_variant!(input.get("Authenticators").unwrap(), Variant::VariantList), - feature_list: match_variant!(input.get("FeatureList").unwrap(), Variant::StringList), + authenticators: input.get("Authenticators").unwrap().try_into().unwrap(), + feature_list: input.get("FeatureList").unwrap().try_into().unwrap(), } } } diff --git a/src/message/handshake/clientinitreject.rs b/src/message/handshake/clientinitreject.rs index 8c2fd34..7c4bb72 100644 --- a/src/message/handshake/clientinitreject.rs +++ b/src/message/handshake/clientinitreject.rs @@ -22,9 +22,9 @@ impl HandshakeSerialize for ClientInitReject { } impl From<VariantMap> for ClientInitReject { - fn from(input: VariantMap) -> Self { + fn from(mut input: VariantMap) -> Self { ClientInitReject { - error: match_variant!(input.get("ErrorString").unwrap(), Variant::String), + error: input.remove("ErrorString").unwrap().try_into().unwrap(), } } } diff --git a/src/message/handshake/clientlogin.rs b/src/message/handshake/clientlogin.rs index c589810..b619e48 100644 --- a/src/message/handshake/clientlogin.rs +++ b/src/message/handshake/clientlogin.rs @@ -21,10 +21,10 @@ impl HandshakeSerialize for ClientLogin { } impl From<VariantMap> for ClientLogin { - fn from(input: VariantMap) -> Self { + fn from(mut input: VariantMap) -> Self { ClientLogin { - user: match_variant!(input.get("User").unwrap(), Variant::String), - password: match_variant!(input.get("Password").unwrap(), Variant::String), + user: input.remove("User").unwrap().try_into().unwrap(), + password: input.remove("Password").unwrap().try_into().unwrap(), } } } diff --git a/src/message/handshake/clientloginack.rs b/src/message/handshake/clientloginack.rs index c8650f9..8a12f60 100644 --- a/src/message/handshake/clientloginack.rs +++ b/src/message/handshake/clientloginack.rs @@ -20,9 +20,9 @@ impl HandshakeSerialize for ClientLoginAck { impl HandshakeDeserialize for ClientLoginAck { fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { - let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; + let (len, mut values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - let msgtype = match_variant!(&values["MsgType"], Variant::ByteArray); + let msgtype: String = values.remove("MsgType").unwrap().try_into().unwrap(); if msgtype == "ClientLogin" { Ok((len, Self {})) diff --git a/src/message/handshake/clientloginreject.rs b/src/message/handshake/clientloginreject.rs index 964ce0c..c4f09cc 100644 --- a/src/message/handshake/clientloginreject.rs +++ b/src/message/handshake/clientloginreject.rs @@ -17,14 +17,14 @@ impl HandshakeSerialize for ClientLoginReject { Variant::String("ClientLoginReject".to_string()), ); values.insert("ErrorString".to_string(), Variant::String(self.error.clone())); - return HandshakeSerialize::serialize(&values); + HandshakeSerialize::serialize(&values) } } impl From<VariantMap> for ClientLoginReject { - fn from(input: VariantMap) -> Self { + fn from(mut input: VariantMap) -> Self { ClientLoginReject { - error: match_variant!(input.get("ErrorString").unwrap(), Variant::String), + error: input.remove("ErrorString").unwrap().try_into().unwrap(), } } } diff --git a/src/message/handshake/mod.rs b/src/message/handshake/mod.rs index 022b71f..9bcbe6e 100644 --- a/src/message/handshake/mod.rs +++ b/src/message/handshake/mod.rs @@ -56,9 +56,9 @@ impl HandshakeSerialize for HandshakeMessage { impl HandshakeDeserialize for HandshakeMessage { fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { - let (size, res) = VariantMap::parse(b)?; + let (size, mut res) = VariantMap::parse(b)?; - let msgtype: String = (&res["MsgType"]).into(); + let msgtype: String = res.remove("MsgType").unwrap().try_into().unwrap(); match msgtype.as_str() { "ClientInit" => Ok((size, HandshakeMessage::ClientInit(res.into()))), "ClientInitAck" => Ok((size, HandshakeMessage::ClientInitAck(res.into()))), diff --git a/src/message/handshake/sessioninit.rs b/src/message/handshake/sessioninit.rs index 81b72f4..e2a29ad 100644 --- a/src/message/handshake/sessioninit.rs +++ b/src/message/handshake/sessioninit.rs @@ -1,6 +1,6 @@ use crate::error::ProtocolError; use crate::message::objects::Identity; -use crate::primitive::{BufferInfo, NetworkId, Variant, VariantMap}; +use crate::primitive::{BufferInfo, NetworkId, Variant, VariantList, VariantMap}; use crate::HandshakeSerialize; /// SessionInit is received along with ClientLoginAck to initialize that user Session @@ -10,31 +10,31 @@ pub struct SessionInit { /// List of all configured identities pub identities: Vec<Identity>, /// List of all existing buffers - pub buffers: Vec<BufferInfo>, + pub buffers: Vec<BufferInfo>, // Vec<Variant::BufferInfo()> /// Ids of all networks pub network_ids: Vec<NetworkId>, } impl From<VariantMap> for SessionInit { fn from(input: VariantMap) -> Self { - let state: VariantMap = input.get("SessionState").unwrap().try_into().unwrap(); + let mut state: VariantMap = input.get("SessionState").unwrap().try_into().unwrap(); log::trace!("sessionstate: {:#?}", state); + let identities: VariantList = state.remove("Identities").unwrap().try_into().unwrap(); + let buffers: VariantList = state.remove("BufferInfos").unwrap().try_into().unwrap(); + let network_ids: VariantList = state.remove("NetworkIds").unwrap().try_into().unwrap(); + SessionInit { - identities: std::convert::TryInto::<Vec<Variant>>::try_into(state.get("Identities").unwrap()) - .unwrap() - .into_iter() - .map(|x| x.try_into().unwrap()) - .collect(), - buffers: match_variant!(state.get("BufferInfos").unwrap(), Variant::VariantList) + identities: identities.into_iter().map(|x| x.try_into().unwrap()).collect(), + buffers: buffers .iter() .map(|buffer| match buffer { Variant::BufferInfo(buffer) => buffer.clone(), _ => unimplemented!(), }) .collect(), - network_ids: match_variant!(state.get("NetworkIds").unwrap(), Variant::VariantList) + network_ids: network_ids .iter() .map(|network| match network { Variant::NetworkId(network) => network.clone(), diff --git a/src/message/signalproxy/heartbeat.rs b/src/message/signalproxy/heartbeat.rs index 9c952e5..7bb5b19 100644 --- a/src/message/signalproxy/heartbeat.rs +++ b/src/message/signalproxy/heartbeat.rs @@ -28,7 +28,7 @@ impl Deserialize for HeartBeat { Ok(( size, Self { - timestamp: match_variant!(res.remove(0), Variant::DateTime), + timestamp: res.remove(0).try_into().unwrap(), }, )) } @@ -59,7 +59,7 @@ impl Deserialize for HeartBeatReply { Ok(( size, Self { - timestamp: match_variant!(res.remove(0), Variant::DateTime), + timestamp: res.remove(0).try_into().unwrap(), }, )) } diff --git a/src/message/signalproxy/initdata.rs b/src/message/signalproxy/initdata.rs index efdd8d7..f95e0b2 100644 --- a/src/message/signalproxy/initdata.rs +++ b/src/message/signalproxy/initdata.rs @@ -32,8 +32,8 @@ impl Deserialize for InitData { res.remove(0); - let class_name: String = res.remove(0).into(); - let object_name: String = res.remove(0).into(); + let class_name: String = res.remove(0).try_into().unwrap(); + let object_name: String = res.remove(0).try_into().unwrap(); Ok(( size, diff --git a/src/message/signalproxy/initrequest.rs b/src/message/signalproxy/initrequest.rs index 1990dba..bf2f200 100644 --- a/src/message/signalproxy/initrequest.rs +++ b/src/message/signalproxy/initrequest.rs @@ -30,8 +30,8 @@ impl Deserialize for InitRequest { Ok(( size, Self { - class_name: match_variant!(res.remove(0), Variant::ByteArray), - object_name: match_variant!(res.remove(0), Variant::ByteArray), + class_name: res.remove(0).try_into().unwrap(), + object_name: res.remove(0).try_into().unwrap(), }, )) } diff --git a/src/message/signalproxy/objects/chanmodes.rs b/src/message/signalproxy/objects/chanmodes.rs index 1161e28..b297126 100644 --- a/src/message/signalproxy/objects/chanmodes.rs +++ b/src/message/signalproxy/objects/chanmodes.rs @@ -62,20 +62,24 @@ impl NetworkMap for ChanModes { } fn from_network_map(input: &mut Self::Item) -> Self { + let channel_modes_a: VariantMap = input.remove("A").unwrap().try_into().unwrap(); + let channel_modes_b: VariantMap = input.remove("B").unwrap().try_into().unwrap(); + let channel_modes_c: VariantMap = input.remove("C").unwrap().try_into().unwrap(); + ChanModes { - channel_modes_a: match_variant!(input.remove("A").unwrap(), Variant::VariantMap) + channel_modes_a: channel_modes_a .into_iter() - .map(|(mut k, v)| (k.remove(0), match_variant!(v, Variant::StringList))) + .map(|(mut k, v)| (k.remove(0), v.try_into().unwrap())) .collect(), - channel_modes_b: match_variant!(input.remove("B").unwrap(), Variant::VariantMap) + channel_modes_b: channel_modes_b .into_iter() - .map(|(mut k, v)| (k.remove(0), match_variant!(v, Variant::String))) + .map(|(mut k, v)| (k.remove(0), v.try_into().unwrap())) .collect(), - channel_modes_c: match_variant!(input.remove("C").unwrap(), Variant::VariantMap) + channel_modes_c: channel_modes_c .into_iter() - .map(|(mut k, v)| (k.remove(0), match_variant!(v, Variant::String))) + .map(|(mut k, v)| (k.remove(0), v.try_into().unwrap())) .collect(), - channel_modes_d: match_variant!(input.remove("D").unwrap(), Variant::String), + channel_modes_d: input.remove("D").unwrap().try_into().unwrap(), } } } diff --git a/src/message/signalproxy/objects/network.rs b/src/message/signalproxy/objects/network.rs index 138f63f..a606537 100644 --- a/src/message/signalproxy/objects/network.rs +++ b/src/message/signalproxy/objects/network.rs @@ -346,6 +346,7 @@ impl crate::message::signalproxy::NetworkList for Network { res } + // TODO VariantList -> VariantMap conversion fn from_network_list(input: &mut VariantList) -> Self { let mut i = input.iter().cycle(); @@ -363,7 +364,7 @@ impl crate::message::signalproxy::NetworkList for Network { i.position(|x| *x == Variant::ByteArray(String::from("myNick"))) .unwrap(); - i.next().unwrap().try_into().unwrap() + i.next().unwrap().clone().try_into().unwrap() }, latency: { i.position(|x| *x == Variant::ByteArray(String::from("latency"))) @@ -375,7 +376,7 @@ impl crate::message::signalproxy::NetworkList for Network { i.position(|x| *x == Variant::ByteArray(String::from("currentServer"))) .unwrap(); - i.next().unwrap().try_into().unwrap() + i.next().unwrap().clone().try_into().unwrap() }, is_connected: { i.position(|x| *x == Variant::ByteArray(String::from("isConnected"))) @@ -529,23 +530,23 @@ impl crate::message::signalproxy::NetworkMap for Network { } fn from_network_map(input: &mut Self::Item) -> Self { - let users_and_channels: VariantMap = - { input.get("IrcUsersAndChannels").unwrap().try_into().unwrap() }; - - return Self { - my_nick: input.get("myNick").unwrap().into(), - latency: input.get("latency").unwrap().try_into().unwrap(), - current_server: input.get("currentServer").unwrap().into(), - is_connected: input.get("isConnected").unwrap().try_into().unwrap(), + let mut users_and_channels: VariantMap = + { input.remove("IrcUsersAndChannels").unwrap().try_into().unwrap() }; + + Self { + my_nick: input.remove("myNick").unwrap().try_into().unwrap(), + latency: input.remove("latency").unwrap().try_into().unwrap(), + current_server: input.remove("currentServer").unwrap().try_into().unwrap(), + is_connected: input.remove("isConnected").unwrap().try_into().unwrap(), connection_state: ConnectionState::from_i32( - input.get("connectionState").unwrap().try_into().unwrap(), + input.remove("connectionState").unwrap().try_into().unwrap(), ) .unwrap(), prefixes: Vec::new(), prefix_modes: Vec::new(), channel_modes: HashMap::with_capacity(4), irc_users: { - match users_and_channels.get("Users") { + match users_and_channels.remove("Users") { Some(users) => { let users: Vec<IrcUser> = Vec::<IrcUser>::from_network_map( &mut users.try_into().expect("failed to convert Users"), @@ -557,7 +558,7 @@ impl crate::message::signalproxy::NetworkMap for Network { } }, irc_channels: { - match users_and_channels.get("Channels") { + match users_and_channels.remove("Channels") { Some(channels) => { let channels: Vec<IrcChannel> = Vec::<IrcChannel>::from_network_map(&mut channels.try_into().unwrap()); @@ -572,20 +573,20 @@ impl crate::message::signalproxy::NetworkMap for Network { supports: VariantMap::try_from(input.get("Supports").unwrap()) .unwrap() .into_iter() - .map(|(k, v)| (k, v.into())) + .map(|(k, v)| (k, v.try_into().unwrap())) .collect(), caps: VariantMap::try_from(input.get("Caps").unwrap()) .unwrap() .into_iter() - .map(|(k, v)| (k, v.into())) + .map(|(k, v)| (k, v.try_into().unwrap())) .collect(), caps_enabled: VariantList::try_from(input.get("CapsEnabled").unwrap()) .unwrap() .into_iter() - .map(|v| v.into()) + .map(|v| v.try_into().unwrap()) .collect(), network_info: NetworkInfo::from_network_map(input), - }; + } } } @@ -627,7 +628,7 @@ impl NetworkList for Vec<NetworkServer> { } fn from_network_list(input: &mut super::VariantList) -> Self { - input.iter().map(|b| match_variant!(b, Variant::NetworkServer)).collect() + input.iter().map(|b| b.try_into().unwrap()).collect() } } diff --git a/src/message/signalproxy/rpccall/bufferinfo.rs b/src/message/signalproxy/rpccall/bufferinfo.rs index 7ba2757..cc8ad3a 100644 --- a/src/message/signalproxy/rpccall/bufferinfo.rs +++ b/src/message/signalproxy/rpccall/bufferinfo.rs @@ -28,7 +28,7 @@ impl RpcCallType for BufferInfoUpdated { Ok(( size, Self { - buffer: match_variant!(input.remove(0), Variant::BufferInfo), + buffer: input.remove(0).try_into().unwrap(), } .into(), )) diff --git a/src/message/signalproxy/rpccall/client.rs b/src/message/signalproxy/rpccall/client.rs index 5fb5326..818b32c 100644 --- a/src/message/signalproxy/rpccall/client.rs +++ b/src/message/signalproxy/rpccall/client.rs @@ -29,7 +29,7 @@ impl RpcCallType for KickClient { Ok(( size, Self { - id: match_variant!(input.remove(0), Variant::i32), + id: input.remove(0).try_into().unwrap(), } .into(), )) diff --git a/src/message/signalproxy/rpccall/identity.rs b/src/message/signalproxy/rpccall/identity.rs index 288dec5..1672e98 100644 --- a/src/message/signalproxy/rpccall/identity.rs +++ b/src/message/signalproxy/rpccall/identity.rs @@ -34,8 +34,8 @@ impl RpcCallType for CreateIdentity { Ok(( size, CreateIdentity { - identity: match_variant!(input.remove(0), Variant::Identity), - additional: match_variant!(input.remove(0), Variant::VariantMap), + identity: input.remove(0).try_into().unwrap(), + additional: input.remove(0).try_into().unwrap(), } .into(), )) @@ -68,7 +68,7 @@ impl RpcCallType for RemoveIdentity { Ok(( size, Self { - identity_id: match_variant!(input.remove(0), Variant::IdentityId), + identity_id: input.remove(0).try_into().unwrap(), } .into(), )) @@ -101,7 +101,7 @@ impl RpcCallType for IdentityCreated { Ok(( size, IdentityCreated { - identity: match_variant!(input.remove(0), Variant::Identity), + identity: input.remove(0).try_into().unwrap(), } .into(), )) @@ -134,7 +134,7 @@ impl RpcCallType for IdentityRemoved { Ok(( size, Self { - identity_id: match_variant!(input.remove(0), Variant::IdentityId), + identity_id: input.remove(0).try_into().unwrap(), } .into(), )) diff --git a/src/message/signalproxy/rpccall/message.rs b/src/message/signalproxy/rpccall/message.rs index 93888b5..bf0629a 100644 --- a/src/message/signalproxy/rpccall/message.rs +++ b/src/message/signalproxy/rpccall/message.rs @@ -29,7 +29,7 @@ impl RpcCallType for DisplayMessage { Ok(( size, RpcCall::DisplayMessage(DisplayMessage { - message: match_variant!(input.remove(0), Variant::Message), + message: input.remove(0).try_into().unwrap(), }), )) } @@ -64,8 +64,8 @@ impl RpcCallType for DisplayStatusMessage { Ok(( size, DisplayStatusMessage { - network: match_variant!(input.remove(0), Variant::String), - message: match_variant!(input.remove(0), Variant::String), + network: input.remove(0).try_into().unwrap(), + message: input.remove(0).try_into().unwrap(), } .into(), )) @@ -100,8 +100,8 @@ impl RpcCallType for SendInput { Ok(( size, Self { - buffer: match_variant!(input.remove(0), Variant::BufferInfo), - message: match_variant!(input.remove(0), Variant::String), + buffer: input.remove(0).try_into().unwrap(), + message: input.remove(0).try_into().unwrap(), } .into(), )) diff --git a/src/message/signalproxy/rpccall/mod.rs b/src/message/signalproxy/rpccall/mod.rs index 378d626..c30d462 100644 --- a/src/message/signalproxy/rpccall/mod.rs +++ b/src/message/signalproxy/rpccall/mod.rs @@ -110,7 +110,7 @@ impl Deserialize for RpcCall { res.remove(0); - let rpc = match_variant!(res.remove(0), Variant::ByteArray); + let rpc: String = res.remove(0).try_into().unwrap(); match rpc.as_str() { DisplayMessage::NAME => DisplayMessage::from_network(size, &mut res), diff --git a/src/message/signalproxy/rpccall/network.rs b/src/message/signalproxy/rpccall/network.rs index 18f9f74..aeb3f80 100644 --- a/src/message/signalproxy/rpccall/network.rs +++ b/src/message/signalproxy/rpccall/network.rs @@ -33,8 +33,8 @@ impl RpcCallType for CreateNetwork { Ok(( size, Self { - network: match_variant!(input.remove(0), Variant::NetworkInfo), - channels: match_variant!(input.remove(0), Variant::StringList), + network: input.remove(0).try_into().unwrap(), + channels: input.remove(0).try_into().unwrap(), } .into(), )) @@ -67,7 +67,7 @@ impl RpcCallType for RemoveNetwork { Ok(( size, Self { - network_id: match_variant!(input.remove(0), Variant::NetworkId), + network_id: input.remove(0).try_into().unwrap(), } .into(), )) @@ -100,7 +100,7 @@ impl RpcCallType for NetworkCreated { Ok(( size, Self { - network_id: match_variant!(input.remove(0), Variant::NetworkId), + network_id: input.remove(0).try_into().unwrap(), } .into(), )) @@ -133,7 +133,7 @@ impl RpcCallType for NetworkRemoved { Ok(( size, Self { - network_id: match_variant!(input.remove(0), Variant::NetworkId), + network_id: input.remove(0).try_into().unwrap(), } .into(), )) diff --git a/src/message/signalproxy/rpccall/objectrenamed.rs b/src/message/signalproxy/rpccall/objectrenamed.rs index d103345..1d698ef 100644 --- a/src/message/signalproxy/rpccall/objectrenamed.rs +++ b/src/message/signalproxy/rpccall/objectrenamed.rs @@ -33,9 +33,9 @@ impl RpcCallType for ObjectRenamed { Ok(( size, Self { - classname: match_variant!(input.remove(0), Variant::ByteArray), - oldname: match_variant!(input.remove(0), Variant::String), - newname: match_variant!(input.remove(0), Variant::String), + classname: input.remove(0).try_into().unwrap(), + oldname: input.remove(0).try_into().unwrap(), + newname: input.remove(0).try_into().unwrap(), } .into(), )) diff --git a/src/message/signalproxy/rpccall/passwordchange.rs b/src/message/signalproxy/rpccall/passwordchange.rs index 38bfc97..b96b926 100644 --- a/src/message/signalproxy/rpccall/passwordchange.rs +++ b/src/message/signalproxy/rpccall/passwordchange.rs @@ -38,10 +38,10 @@ impl RpcCallType for ChangePassword { Ok(( size, Self { - peer: match_variant!(input.remove(0), Variant::PeerPtr), - user: match_variant!(input.remove(0), Variant::String), - before: match_variant!(input.remove(0), Variant::String), - after: match_variant!(input.remove(0), Variant::String), + peer: input.remove(0).try_into().unwrap(), + user: input.remove(0).try_into().unwrap(), + before: input.remove(0).try_into().unwrap(), + after: input.remove(0).try_into().unwrap(), } .into(), )) @@ -79,8 +79,8 @@ impl RpcCallType for PasswordChanged { Ok(( size, Self { - peer: match_variant!(input.remove(0), Variant::PeerPtr), - success: match_variant!(input.remove(0), Variant::bool), + peer: input.remove(0).try_into().unwrap(), + success: input.remove(0).try_into().unwrap(), } .into(), )) diff --git a/src/message/signalproxy/syncmessage.rs b/src/message/signalproxy/syncmessage.rs index 94930af..bb61d7c 100644 --- a/src/message/signalproxy/syncmessage.rs +++ b/src/message/signalproxy/syncmessage.rs @@ -105,16 +105,18 @@ impl Serialize for SyncMessage { impl Deserialize for SyncMessage { fn parse(b: &[std::primitive::u8]) -> Result<(std::primitive::usize, Self), ProtocolError> { - let (size, mut res) = VariantList::parse(&b)?; + let (size, mut res) = VariantList::parse(b)?; res.remove(0); + let class_name: String = res.remove(0).try_into().unwrap(); + Ok(( size, Self { - class_name: Class::from(match_variant!(res.remove(0), Variant::ByteArray)), - object_name: match_variant!(res.remove(0), Variant::ByteArray), - slot_name: match_variant!(res.remove(0), Variant::ByteArray), + class_name: Class::from(class_name), + object_name: res.remove(0).try_into().unwrap(), + slot_name: res.remove(0).try_into().unwrap(), params: res, }, )) diff --git a/src/primitive/bufferid.rs b/src/primitive/bufferid.rs index 59e1ace..334e976 100644 --- a/src/primitive/bufferid.rs +++ b/src/primitive/bufferid.rs @@ -53,7 +53,7 @@ impl NetworkList for Vec<BufferId> { } fn from_network_list(input: &mut super::VariantList) -> Self { - input.iter().map(|b| match_variant!(b, Variant::BufferId)).collect() + input.iter().map(|b| b.try_into().unwrap()).collect() } } diff --git a/src/primitive/signedint.rs b/src/primitive/signedint.rs index 6bf009f..8591c03 100644 --- a/src/primitive/signedint.rs +++ b/src/primitive/signedint.rs @@ -17,7 +17,7 @@ impl Serialize for i64 { impl Deserialize for i64 { fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { let mut rdr = Cursor::new(&b[0..8]); - return Ok((8, rdr.read_i64::<BigEndian>()?)); + Ok((8, rdr.read_i64::<BigEndian>()?)) } } @@ -34,7 +34,7 @@ impl Serialize for i32 { impl Deserialize for i32 { fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { let mut rdr = Cursor::new(&b[0..4]); - return Ok((4, rdr.read_i32::<BigEndian>()?)); + Ok((4, rdr.read_i32::<BigEndian>()?)) } } @@ -51,7 +51,7 @@ impl Serialize for i16 { impl Deserialize for i16 { fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { let mut rdr = Cursor::new(&b[0..2]); - return Ok((2, rdr.read_i16::<BigEndian>()?)); + Ok((2, rdr.read_i16::<BigEndian>()?)) } } @@ -68,7 +68,7 @@ impl Serialize for i8 { impl Deserialize for i8 { fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { let mut rdr = Cursor::new(&b[0..1]); - return Ok((1, rdr.read_i8()?)); + Ok((1, rdr.read_i8()?)) } } diff --git a/src/primitive/variant.rs b/src/primitive/variant.rs index 8a1c777..61e6752 100644 --- a/src/primitive/variant.rs +++ b/src/primitive/variant.rs @@ -61,22 +61,14 @@ pub enum Variant { i8(i8), } -impl From<Variant> for String { - fn from(input: Variant) -> Self { - match input { - Variant::String(value) => value, - Variant::ByteArray(value) => value, - _ => panic!("unknown variant expected string or bytearray {:?}", input), - } - } -} +impl TryFrom<Variant> for String { + type Error = ProtocolError; -impl From<&Variant> for String { - fn from(input: &Variant) -> Self { + fn try_from(input: Variant) -> Result<Self, Self::Error> { match input { - Variant::String(value) => value.clone(), - Variant::ByteArray(value) => value.clone(), - _ => panic!("unknown variant expected string or bytearray {:?}", input), + Variant::String(value) => Ok(value), + Variant::ByteArray(value) => Ok(value), + _ => Err(ProtocolError::UnknownVariant) } } } diff --git a/src/util.rs b/src/util.rs index eb5a595..9c78bdb 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,25 +1,3 @@ -/// Match a VariantMaps field and return it's contents if successfull -/// -/// # Example -/// -/// ``` -/// use libquassel::match_variant; -/// use libquassel::primitive::{VariantMap, Variant}; -/// -/// let var = Variant::String("test string".to_string()); -/// let result = match_variant!(var, Variant::String); -/// ``` -#[macro_export] -macro_rules! match_variant { - ( $values:expr, $x:path ) => { - match &$values { - $x(x) => Ok(x.clone()), - err => Err(err), - } - .unwrap() - }; -} - /// Prepend the length of `buf` to `buf` pub fn prepend_byte_len(buf: &mut Vec<u8>) { let len: i32 = buf.len().try_into().unwrap(); |
