Defined ErrorResponse type
This commit is contained in:
@@ -26,6 +26,10 @@ pub struct RegisterResponse {
|
||||
pub auth_token: AuthToken,
|
||||
}
|
||||
|
||||
pub struct ErrorResponse {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl PullResponse {
|
||||
pub fn new(messages: Vec<Vec<u8>>) -> Self {
|
||||
PullResponse { messages }
|
||||
@@ -38,6 +42,14 @@ impl RegisterResponse {
|
||||
}
|
||||
}
|
||||
|
||||
impl ErrorResponse {
|
||||
pub fn new<S: Into<String>>(message: S) -> Self {
|
||||
ErrorResponse {
|
||||
message: message.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: This should go into some kind of utils module/crate
|
||||
fn read_be_u16(input: &mut &[u8]) -> u16 {
|
||||
let (int_bytes, rest) = input.split_at(std::mem::size_of::<u16>());
|
||||
@@ -126,6 +138,19 @@ impl ProviderResponse for RegisterResponse {
|
||||
}
|
||||
}
|
||||
|
||||
impl ProviderResponse for ErrorResponse {
|
||||
fn to_bytes(&self) -> Vec<u8> {
|
||||
self.message.clone().into_bytes()
|
||||
}
|
||||
|
||||
fn from_bytes(bytes: &[u8]) -> Result<Self, ProviderResponseError> {
|
||||
match String::from_utf8(bytes.to_vec()) {
|
||||
Err(_) => Err(ProviderResponseError::UnmarshalError),
|
||||
Ok(message) => Ok(ErrorResponse { message }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod creating_pull_response {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user