use crate::i2cp::message::{MessageType, I2CP_HEADER_SIZE};
use bytes::{BufMut, Bytes, BytesMut};
pub enum HostReplyKind {
Success {
destination: Bytes,
},
Failure,
#[allow(unused)]
PasswordRequired,
#[allow(unused)]
PrivateKeyRequired,
#[allow(unused)]
PaswordAndPrivateKeyRequired,
#[allow(unused)]
LeasesetDecryptionFailed,
}
impl HostReplyKind {
fn as_u8(&self) -> u8 {
match self {
Self::Success { .. } => 0,
Self::Failure => 1,
Self::PasswordRequired => 2,
Self::PrivateKeyRequired => 3,
Self::PaswordAndPrivateKeyRequired => 4,
Self::LeasesetDecryptionFailed => 5,
}
}
fn serialized_len(&self) -> usize {
match self {
Self::Success { destination } => 1usize + destination.len(),
_ => 1usize,
}
}
}
pub struct HostReply(());
impl HostReply {
pub fn new(session_id: u16, request_id: u32, kind: HostReplyKind) -> BytesMut {
let payload_len = 2 + 3 + kind.serialized_len();
let mut out = BytesMut::with_capacity(I2CP_HEADER_SIZE + payload_len);
out.put_u32(payload_len as u32);
out.put_u8(MessageType::HostReply.as_u8());
out.put_u16(session_id);
out.put_u32(request_id);
out.put_u8(kind.as_u8());
if let HostReplyKind::Success { destination } = kind {
out.put_slice(&destination);
}
out
}
}