diff options
| author | Max Audron <me@audron.dev> | 2026-02-21 17:25:52 +0100 |
|---|---|---|
| committer | Max Audron <me@audron.dev> | 2026-02-21 17:25:52 +0100 |
| commit | e63ecc10aa426e3aba416fd05a3f568d719a79a3 (patch) | |
| tree | e1a8dc6e4197e51ddeb3d631410acec9eda3cf6a | |
| parent | use ProtocolError for From derive (diff) | |
handshare and signalproxy/rpccall error handling
21 files changed, 165 insertions, 91 deletions
diff --git a/src/error.rs b/src/error.rs index 7414062..3140901 100644 --- a/src/error.rs +++ b/src/error.rs @@ -12,6 +12,8 @@ pub enum ProtocolError { UnknownUserType(String), #[error("wrong variant has been given")] WrongVariant, + #[error("missing required field: {0}")] + MissingField(String), #[error("io error: {0}")] IOError(#[from] std::io::Error), #[error("could not convert from int: {0}")] diff --git a/src/message/handshake/clientinit.rs b/src/message/handshake/clientinit.rs index 9d35243..3011821 100644 --- a/src/message/handshake/clientinit.rs +++ b/src/message/handshake/clientinit.rs @@ -63,13 +63,27 @@ impl HandshakeSerialize for ClientInit { } } -impl From<VariantMap> for ClientInit { - fn from(mut input: VariantMap) -> Self { - ClientInit { - 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(), - } +impl TryFrom<VariantMap> for ClientInit { + type Error = ProtocolError; + + fn try_from(mut input: VariantMap) -> Result<Self, Self::Error> { + Ok(ClientInit { + client_version: input + .remove("ClientVersion") + .ok_or_else(|| ProtocolError::MissingField("ClientVersion".to_string()))? + .try_into()?, + client_date: input + .remove("ClientDate") + .ok_or_else(|| ProtocolError::MissingField("ClientDate".to_string()))? + .try_into()?, + client_features: input + .remove("Features") + .ok_or_else(|| ProtocolError::MissingField("Features".to_string()))? + .try_into()?, + feature_list: input + .remove("FeatureList") + .ok_or_else(|| ProtocolError::MissingField("FeatureList".to_string()))? + .try_into()?, + }) } } diff --git a/src/message/handshake/clientinitack.rs b/src/message/handshake/clientinitack.rs index f3f4640..adb7425 100644 --- a/src/message/handshake/clientinitack.rs +++ b/src/message/handshake/clientinitack.rs @@ -45,16 +45,30 @@ impl HandshakeSerialize for ClientInitAck { } } -impl From<VariantMap> for ClientInitAck { - fn from(input: VariantMap) -> Self { - ClientInitAck { +impl TryFrom<VariantMap> for ClientInitAck { + type Error = ProtocolError; + + fn try_from(input: VariantMap) -> Result<Self, Self::Error> { + Ok(ClientInitAck { // TODO make this compatible with older clients core_features: 0, - core_configured: input.get("Configured").unwrap().try_into().unwrap(), - storage_backends: input.get("StorageBackends").unwrap().try_into().unwrap(), + core_configured: input + .get("Configured") + .ok_or_else(|| ProtocolError::MissingField("Configured".to_string()))? + .try_into()?, + storage_backends: input + .get("StorageBackends") + .ok_or_else(|| ProtocolError::MissingField("StorageBackends".to_string()))? + .try_into()?, #[cfg(feature = "authenticators")] - authenticators: input.get("Authenticators").unwrap().try_into().unwrap(), - feature_list: input.get("FeatureList").unwrap().try_into().unwrap(), - } + authenticators: input + .get("Authenticators") + .ok_or_else(|| ProtocolError::MissingField("Authenticators".to_string()))? + .try_into()?, + feature_list: input + .get("FeatureList") + .ok_or_else(|| ProtocolError::MissingField("FeatureList".to_string()))? + .try_into()?, + }) } } diff --git a/src/message/handshake/clientinitreject.rs b/src/message/handshake/clientinitreject.rs index fca5388..6e4622b 100644 --- a/src/message/handshake/clientinitreject.rs +++ b/src/message/handshake/clientinitreject.rs @@ -21,10 +21,15 @@ impl HandshakeSerialize for ClientInitReject { } } -impl From<VariantMap> for ClientInitReject { - fn from(mut input: VariantMap) -> Self { - ClientInitReject { - error: input.remove("ErrorString").unwrap().try_into().unwrap(), - } +impl TryFrom<VariantMap> for ClientInitReject { + type Error = ProtocolError; + + fn try_from(mut input: VariantMap) -> Result<Self, Self::Error> { + Ok(ClientInitReject { + error: input + .remove("ErrorString") + .ok_or_else(|| ProtocolError::MissingField("ErrorString".to_string()))? + .try_into()?, + }) } } diff --git a/src/message/handshake/clientlogin.rs b/src/message/handshake/clientlogin.rs index 4be4442..b1d9672 100644 --- a/src/message/handshake/clientlogin.rs +++ b/src/message/handshake/clientlogin.rs @@ -20,11 +20,19 @@ impl HandshakeSerialize for ClientLogin { } } -impl From<VariantMap> for ClientLogin { - fn from(mut input: VariantMap) -> Self { - ClientLogin { - user: input.remove("User").unwrap().try_into().unwrap(), - password: input.remove("Password").unwrap().try_into().unwrap(), - } +impl TryFrom<VariantMap> for ClientLogin { + type Error = ProtocolError; + + fn try_from(mut input: VariantMap) -> Result<Self, Self::Error> { + Ok(ClientLogin { + user: input + .remove("User") + .ok_or_else(|| ProtocolError::MissingField("User".to_string()))? + .try_into()?, + password: input + .remove("Password") + .ok_or_else(|| ProtocolError::MissingField("Password".to_string()))? + .try_into()?, + }) } } diff --git a/src/message/handshake/clientloginack.rs b/src/message/handshake/clientloginack.rs index de26274..949ce55 100644 --- a/src/message/handshake/clientloginack.rs +++ b/src/message/handshake/clientloginack.rs @@ -22,7 +22,10 @@ impl HandshakeDeserialize for ClientLoginAck { fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { let (len, mut values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - let msgtype: String = values.remove("MsgType").unwrap().try_into().unwrap(); + let msgtype: String = values + .remove("MsgType") + .ok_or_else(|| ProtocolError::MissingField("MsgType".to_string()))? + .try_into()?; if msgtype == "ClientLogin" { Ok((len, Self {})) diff --git a/src/message/handshake/clientloginreject.rs b/src/message/handshake/clientloginreject.rs index c4f09cc..300bc2e 100644 --- a/src/message/handshake/clientloginreject.rs +++ b/src/message/handshake/clientloginreject.rs @@ -21,10 +21,15 @@ impl HandshakeSerialize for ClientLoginReject { } } -impl From<VariantMap> for ClientLoginReject { - fn from(mut input: VariantMap) -> Self { - ClientLoginReject { - error: input.remove("ErrorString").unwrap().try_into().unwrap(), - } +impl TryFrom<VariantMap> for ClientLoginReject { + type Error = ProtocolError; + + fn try_from(mut input: VariantMap) -> Result<Self, Self::Error> { + Ok(ClientLoginReject { + error: input + .remove("ErrorString") + .ok_or_else(|| ProtocolError::MissingField("ErrorString".to_string()))? + .try_into()?, + }) } } diff --git a/src/message/handshake/mod.rs b/src/message/handshake/mod.rs index 9bcbe6e..1b02bb4 100644 --- a/src/message/handshake/mod.rs +++ b/src/message/handshake/mod.rs @@ -58,15 +58,18 @@ impl HandshakeDeserialize for HandshakeMessage { fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { let (size, mut res) = VariantMap::parse(b)?; - let msgtype: String = res.remove("MsgType").unwrap().try_into().unwrap(); + let msgtype: String = res + .remove("MsgType") + .ok_or_else(|| ProtocolError::MissingField("MsgType".to_string()))? + .try_into()?; match msgtype.as_str() { - "ClientInit" => Ok((size, HandshakeMessage::ClientInit(res.into()))), - "ClientInitAck" => Ok((size, HandshakeMessage::ClientInitAck(res.into()))), - "ClientInitReject" => Ok((size, HandshakeMessage::ClientInitReject(res.into()))), - "ClientLogin" => Ok((size, HandshakeMessage::ClientLogin(res.into()))), + "ClientInit" => Ok((size, HandshakeMessage::ClientInit(res.try_into()?))), + "ClientInitAck" => Ok((size, HandshakeMessage::ClientInitAck(res.try_into()?))), + "ClientInitReject" => Ok((size, HandshakeMessage::ClientInitReject(res.try_into()?))), + "ClientLogin" => Ok((size, HandshakeMessage::ClientLogin(res.try_into()?))), "ClientLoginAck" => Ok((size, HandshakeMessage::ClientLoginAck)), - "ClientLoginReject" => Ok((size, HandshakeMessage::ClientLoginReject(res.into()))), - "SessionInit" => Ok((size, HandshakeMessage::SessionInit(res.into()))), + "ClientLoginReject" => Ok((size, HandshakeMessage::ClientLoginReject(res.try_into()?))), + "SessionInit" => Ok((size, HandshakeMessage::SessionInit(res.try_into()?))), _ => unimplemented!(), } } diff --git a/src/message/handshake/sessioninit.rs b/src/message/handshake/sessioninit.rs index b3a0932..18c026d 100644 --- a/src/message/handshake/sessioninit.rs +++ b/src/message/handshake/sessioninit.rs @@ -15,33 +15,50 @@ pub struct SessionInit { pub network_ids: Vec<NetworkId>, } -impl From<VariantMap> for SessionInit { - fn from(input: VariantMap) -> Self { - let mut state: VariantMap = input.get("SessionState").unwrap().try_into().unwrap(); +impl TryFrom<VariantMap> for SessionInit { + type Error = ProtocolError; + + fn try_from(input: VariantMap) -> Result<Self, Self::Error> { + let mut state: VariantMap = input + .get("SessionState") + .ok_or_else(|| ProtocolError::MissingField("SessionState".to_string()))? + .try_into()?; 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(); + let identities: VariantList = state + .remove("Identities") + .ok_or_else(|| ProtocolError::MissingField("Identities".to_string()))? + .try_into()?; + let buffers: VariantList = state + .remove("BufferInfos") + .ok_or_else(|| ProtocolError::MissingField("BufferInfos".to_string()))? + .try_into()?; + let network_ids: VariantList = state + .remove("NetworkIds") + .ok_or_else(|| ProtocolError::MissingField("NetworkIds".to_string()))? + .try_into()?; - SessionInit { - identities: identities.into_iter().map(|x| x.try_into().unwrap()).collect(), + Ok(SessionInit { + identities: identities + .into_iter() + .map(|x| x.try_into()) + .collect::<Result<Vec<_>, _>>()?, buffers: buffers .iter() .map(|buffer| match buffer { - Variant::BufferInfo(buffer) => buffer.clone(), - _ => unimplemented!(), + Variant::BufferInfo(buffer) => Ok(buffer.clone()), + _ => Err(ProtocolError::WrongVariant), }) - .collect(), + .collect::<Result<Vec<_>, _>>()?, network_ids: network_ids .iter() .map(|network| match network { - Variant::NetworkId(network) => *network, - _ => unimplemented!(), + Variant::NetworkId(network) => Ok(*network), + _ => Err(ProtocolError::WrongVariant), }) - .collect(), - } + .collect::<Result<Vec<_>, _>>()?, + }) } } diff --git a/src/message/signalproxy/heartbeat.rs b/src/message/signalproxy/heartbeat.rs index e7d17f3..920efe2 100644 --- a/src/message/signalproxy/heartbeat.rs +++ b/src/message/signalproxy/heartbeat.rs @@ -27,7 +27,7 @@ impl Deserialize for HeartBeat { Ok(( size, Self { - timestamp: res.remove(0).try_into().unwrap(), + timestamp: res.remove(0).try_into()?, }, )) } @@ -57,7 +57,7 @@ impl Deserialize for HeartBeatReply { Ok(( size, Self { - timestamp: res.remove(0).try_into().unwrap(), + timestamp: res.remove(0).try_into()?, }, )) } diff --git a/src/message/signalproxy/initdata.rs b/src/message/signalproxy/initdata.rs index c2ca6c6..e693971 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).try_into().unwrap(); - let object_name: String = res.remove(0).try_into().unwrap(); + let class_name: String = res.remove(0).try_into()?; + let object_name: String = res.remove(0).try_into()?; Ok(( size, diff --git a/src/message/signalproxy/initrequest.rs b/src/message/signalproxy/initrequest.rs index 592a703..ad6a3f2 100644 --- a/src/message/signalproxy/initrequest.rs +++ b/src/message/signalproxy/initrequest.rs @@ -29,9 +29,11 @@ impl Deserialize for InitRequest { Ok(( size, Self { - class_name: res.remove(0).try_into().unwrap(), - object_name: res.remove(0).try_into().unwrap(), + class_name: res.remove(0).try_into()?, + object_name: res.remove(0).try_into()?, }, )) } } + + diff --git a/src/message/signalproxy/rpccall/bufferinfo.rs b/src/message/signalproxy/rpccall/bufferinfo.rs index cc8ad3a..762cb4d 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: input.remove(0).try_into().unwrap(), + buffer: input.remove(0).try_into()?, } .into(), )) diff --git a/src/message/signalproxy/rpccall/client.rs b/src/message/signalproxy/rpccall/client.rs index 7e31135..13653b9 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: input.remove(0).try_into().unwrap(), + id: input.remove(0).try_into()?, } .into(), )) diff --git a/src/message/signalproxy/rpccall/identity.rs b/src/message/signalproxy/rpccall/identity.rs index 15beec4..8927f1c 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: input.remove(0).try_into().unwrap(), - additional: input.remove(0).try_into().unwrap(), + identity: input.remove(0).try_into()?, + additional: input.remove(0).try_into()?, } .into(), )) @@ -68,7 +68,7 @@ impl RpcCallType for RemoveIdentity { Ok(( size, Self { - identity_id: input.remove(0).try_into().unwrap(), + identity_id: input.remove(0).try_into()?, } .into(), )) @@ -101,7 +101,7 @@ impl RpcCallType for IdentityCreated { Ok(( size, IdentityCreated { - identity: input.remove(0).try_into().unwrap(), + identity: input.remove(0).try_into()?, } .into(), )) @@ -134,7 +134,7 @@ impl RpcCallType for IdentityRemoved { Ok(( size, Self { - identity_id: input.remove(0).try_into().unwrap(), + identity_id: input.remove(0).try_into()?, } .into(), )) diff --git a/src/message/signalproxy/rpccall/message.rs b/src/message/signalproxy/rpccall/message.rs index bf0629a..9550658 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: input.remove(0).try_into().unwrap(), + message: input.remove(0).try_into()?, }), )) } @@ -64,8 +64,8 @@ impl RpcCallType for DisplayStatusMessage { Ok(( size, DisplayStatusMessage { - network: input.remove(0).try_into().unwrap(), - message: input.remove(0).try_into().unwrap(), + network: input.remove(0).try_into()?, + message: input.remove(0).try_into()?, } .into(), )) @@ -100,8 +100,8 @@ impl RpcCallType for SendInput { Ok(( size, Self { - buffer: input.remove(0).try_into().unwrap(), - message: input.remove(0).try_into().unwrap(), + buffer: input.remove(0).try_into()?, + message: input.remove(0).try_into()?, } .into(), )) diff --git a/src/message/signalproxy/rpccall/mod.rs b/src/message/signalproxy/rpccall/mod.rs index 2a3e7b1..3d869d2 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: String = res.remove(0).try_into().unwrap(); + let rpc: String = res.remove(0).try_into()?; 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 738dddf..95315c1 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: input.remove(0).try_into().unwrap(), - channels: input.remove(0).try_into().unwrap(), + network: input.remove(0).try_into()?, + channels: input.remove(0).try_into()?, } .into(), )) @@ -67,7 +67,7 @@ impl RpcCallType for RemoveNetwork { Ok(( size, Self { - network_id: input.remove(0).try_into().unwrap(), + network_id: input.remove(0).try_into()?, } .into(), )) @@ -100,7 +100,7 @@ impl RpcCallType for NetworkCreated { Ok(( size, Self { - network_id: input.remove(0).try_into().unwrap(), + network_id: input.remove(0).try_into()?, } .into(), )) @@ -133,7 +133,7 @@ impl RpcCallType for NetworkRemoved { Ok(( size, Self { - network_id: input.remove(0).try_into().unwrap(), + network_id: input.remove(0).try_into()?, } .into(), )) diff --git a/src/message/signalproxy/rpccall/objectrenamed.rs b/src/message/signalproxy/rpccall/objectrenamed.rs index 1d698ef..bcef760 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: input.remove(0).try_into().unwrap(), - oldname: input.remove(0).try_into().unwrap(), - newname: input.remove(0).try_into().unwrap(), + classname: input.remove(0).try_into()?, + oldname: input.remove(0).try_into()?, + newname: input.remove(0).try_into()?, } .into(), )) diff --git a/src/message/signalproxy/rpccall/passwordchange.rs b/src/message/signalproxy/rpccall/passwordchange.rs index 16bf78f..6affc44 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: 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(), + peer: input.remove(0).try_into()?, + user: input.remove(0).try_into()?, + before: input.remove(0).try_into()?, + after: input.remove(0).try_into()?, } .into(), )) @@ -79,8 +79,8 @@ impl RpcCallType for PasswordChanged { Ok(( size, Self { - peer: input.remove(0).try_into().unwrap(), - success: input.remove(0).try_into().unwrap(), + peer: input.remove(0).try_into()?, + success: input.remove(0).try_into()?, } .into(), )) diff --git a/src/message/signalproxy/syncmessage.rs b/src/message/signalproxy/syncmessage.rs index b124d07..d38ea20 100644 --- a/src/message/signalproxy/syncmessage.rs +++ b/src/message/signalproxy/syncmessage.rs @@ -109,16 +109,17 @@ impl Deserialize for SyncMessage { res.remove(0); - let class_name: String = res.remove(0).try_into().unwrap(); + let class_name: String = res.remove(0).try_into()?; Ok(( size, Self { class_name: Class::from(class_name), - object_name: res.remove(0).try_into().unwrap(), - slot_name: res.remove(0).try_into().unwrap(), + object_name: res.remove(0).try_into()?, + slot_name: res.remove(0).try_into()?, params: res, }, )) } } + |
