lib: Add utility functions for checking response

This commit is contained in:
Artemis Tosini 2024-09-10 02:39:23 +00:00
parent bd945a48a6
commit e64e563e11
Signed by: artemist
GPG key ID: ADFFE553DCBB831E
4 changed files with 47 additions and 10 deletions

View file

@ -1,10 +1,11 @@
use snafu::prelude::*; use snafu::prelude::*;
use vapore_struct::eresult::EResult;
#[derive(Debug, Snafu)] #[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))] #[snafu(visibility(pub(crate)))]
pub enum ClientError { pub enum ClientError {
#[snafu(display("Valve returned bad result {eresult} with message `{message}`"))] #[snafu(display("Valve returned bad result {eresult:?} with message `{message}`"))]
EResult { eresult: u32, message: String }, BadResult { eresult: EResult, message: String },
#[snafu(display("Request failure"))] #[snafu(display("Request failure"))]
Reqwest { source: reqwest::Error }, Reqwest { source: reqwest::Error },

View file

@ -7,6 +7,7 @@ use vapore_proto::{
enums_clientserver::EMsg, enums_clientserver::EMsg,
steammessages_base::{CMsgMulti, CMsgProtoBufHeader}, steammessages_base::{CMsgMulti, CMsgProtoBufHeader},
}; };
use vapore_struct::eresult::EResult;
use crate::error::{ use crate::error::{
ClientError, DecompressionInvalidSnafu, DecompressionSnafu, InvalidActionSnafu, ClientError, DecompressionInvalidSnafu, DecompressionSnafu, InvalidActionSnafu,
@ -53,6 +54,22 @@ impl<T: protobuf::Message> CMProtoBufMessage<T> {
body, body,
}) })
} }
pub fn eresult(&self) -> Result<EResult, ClientError> {
Ok((self.header.eresult() as u32).try_into()?)
}
pub fn ok(&self) -> Result<(), ClientError> {
let eresult = self.eresult()?;
if eresult == EResult::OK {
Ok(())
} else {
Err(ClientError::BadResult {
eresult,
message: self.header.error_message().to_string(),
})
}
}
} }
/// A message sent over the socket, but the body is still serialized /// A message sent over the socket, but the body is still serialized
@ -151,4 +168,20 @@ impl CMRawProtoBufMessage {
Ok(items) Ok(items)
} }
pub fn eresult(&self) -> Result<EResult, ClientError> {
Ok((self.header.eresult() as u32).try_into()?)
}
pub fn ok(&self) -> Result<(), ClientError> {
let eresult = self.eresult()?;
if eresult == EResult::OK {
Ok(())
} else {
Err(ClientError::BadResult {
eresult,
message: self.header.error_message().to_string(),
})
}
}
} }

View file

@ -1,10 +1,12 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use num_enum::TryFromPrimitive;
use serde::Deserialize; use serde::Deserialize;
use snafu::prelude::*; use snafu::prelude::*;
use vapore_struct::eresult::EResult;
use crate::{ use crate::{
error::{EResultSnafu, ReqwestSnafu, VdfSnafu}, error::{BadResultSnafu, ReqwestSnafu, VdfSnafu},
ClientError, ClientError,
}; };
@ -39,10 +41,11 @@ pub async fn bootstrap_find_servers() -> Result<Vec<String>, ClientError> {
let result: GetCMListForConnectResponse = let result: GetCMListForConnectResponse =
keyvalues_serde::from_str(&response).context(VdfSnafu {})?; keyvalues_serde::from_str(&response).context(VdfSnafu {})?;
let eresult = EResult::try_from_primitive(result.success)?;
ensure!( ensure!(
result.success == 1, eresult == EResult::OK,
EResultSnafu { BadResultSnafu {
eresult: result.success, eresult,
message: result.message message: result.message
} }
); );

View file

@ -4,8 +4,8 @@
pub mod eresult { pub mod eresult {
use num_enum::{IntoPrimitive, TryFromPrimitive}; use num_enum::{IntoPrimitive, TryFromPrimitive};
#[repr(u32)] #[repr(u32)]
#[derive(Debug, Copy, Clone, TryFromPrimitive, IntoPrimitive)] #[derive(Debug, Copy, Clone, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
enum EResult { pub enum EResult {
Invalid = 0, Invalid = 0,
OK = 1, OK = 1,
@ -166,7 +166,7 @@ pub mod enums {
} }
} }
#[derive(Debug, Copy, Clone, TryFromPrimitive, IntoPrimitive)] #[derive(Debug, Copy, Clone, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)] #[repr(u32)]
pub enum ELicenseType { pub enum ELicenseType {
NoLicense = 0, NoLicense = 0,
@ -180,7 +180,7 @@ pub mod enums {
} }
#[repr(u32)] #[repr(u32)]
#[derive(Debug, Copy, Clone, TryFromPrimitive, IntoPrimitive)] #[derive(Debug, Copy, Clone, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
pub enum EPaymentMethod { pub enum EPaymentMethod {
None = 0, None = 0,
ActivationCode = 1, ActivationCode = 1,