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 vapore_struct::eresult::EResult;
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
pub enum ClientError {
#[snafu(display("Valve returned bad result {eresult} with message `{message}`"))]
EResult { eresult: u32, message: String },
#[snafu(display("Valve returned bad result {eresult:?} with message `{message}`"))]
BadResult { eresult: EResult, message: String },
#[snafu(display("Request failure"))]
Reqwest { source: reqwest::Error },

View file

@ -7,6 +7,7 @@ use vapore_proto::{
enums_clientserver::EMsg,
steammessages_base::{CMsgMulti, CMsgProtoBufHeader},
};
use vapore_struct::eresult::EResult;
use crate::error::{
ClientError, DecompressionInvalidSnafu, DecompressionSnafu, InvalidActionSnafu,
@ -53,6 +54,22 @@ impl<T: protobuf::Message> CMProtoBufMessage<T> {
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
@ -151,4 +168,20 @@ impl CMRawProtoBufMessage {
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 num_enum::TryFromPrimitive;
use serde::Deserialize;
use snafu::prelude::*;
use vapore_struct::eresult::EResult;
use crate::{
error::{EResultSnafu, ReqwestSnafu, VdfSnafu},
error::{BadResultSnafu, ReqwestSnafu, VdfSnafu},
ClientError,
};
@ -39,10 +41,11 @@ pub async fn bootstrap_find_servers() -> Result<Vec<String>, ClientError> {
let result: GetCMListForConnectResponse =
keyvalues_serde::from_str(&response).context(VdfSnafu {})?;
let eresult = EResult::try_from_primitive(result.success)?;
ensure!(
result.success == 1,
EResultSnafu {
eresult: result.success,
eresult == EResult::OK,
BadResultSnafu {
eresult,
message: result.message
}
);

View file

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