use std::fmt;
use crate::frame::REPLY_SUCCESS;
use amqp_serde::types::{AmqpPeerProperties, LongStr, LongUint, Octect, ShortStr, ShortUint};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Start {
pub(crate) version_major: Octect,
pub(crate) version_minor: Octect,
pub(crate) server_properties: AmqpPeerProperties,
pub(crate) mechanisms: LongStr,
pub(crate) locales: LongStr,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct StartOk {
client_properties: AmqpPeerProperties,
machanisms: ShortStr,
response: LongStr,
locale: ShortStr,
}
impl StartOk {
pub fn new(
client_properties: AmqpPeerProperties,
machanisms: ShortStr,
response: LongStr,
locale: ShortStr,
) -> Self {
Self {
client_properties,
machanisms,
response,
locale,
}
}
}
impl Default for StartOk {
fn default() -> Self {
Self {
client_properties: AmqpPeerProperties::new(),
machanisms: "PLAIN".try_into().unwrap(),
response: "\0guest\0guest".try_into().unwrap(),
locale: "en_US".try_into().unwrap(),
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Tune {
channel_max: ShortUint,
frame_max: LongUint,
heartbeat: ShortUint,
}
impl Tune {
pub fn channel_max(&self) -> u16 {
self.channel_max
}
pub fn frame_max(&self) -> u32 {
self.frame_max
}
pub fn heartbeat(&self) -> u16 {
self.heartbeat
}
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct TuneOk {
channel_max: ShortUint,
frame_max: LongUint,
heartbeat: ShortUint,
}
impl TuneOk {
pub fn new(channel_max: ShortUint, frame_max: LongUint, heartbeat: ShortUint) -> Self {
Self {
channel_max,
frame_max,
heartbeat,
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Open {
virtual_host: ShortStr,
capabilities: ShortStr,
insist: Octect,
}
impl Open {
pub fn new(virtual_host: ShortStr, capabilities: ShortStr) -> Self {
Self {
virtual_host,
capabilities,
insist: 0,
}
}
}
impl Default for Open {
fn default() -> Self {
Self {
virtual_host: "/".try_into().unwrap(),
capabilities: ShortStr::default(),
insist: 0,
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct OpenOk {
know_hosts: ShortStr,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Close {
reply_code: ShortUint,
reply_text: ShortStr,
class_id: ShortUint,
method_id: ShortUint,
}
impl fmt::Display for Close {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!(
"'{}: {}', (class_id = {}, method_id = {})",
self.reply_code(),
self.reply_text(),
self.class_id(),
self.method_id()
))
}
}
impl Close {
pub fn reply_code(&self) -> u16 {
self.reply_code
}
pub fn reply_text(&self) -> &String {
self.reply_text.as_ref()
}
pub fn class_id(&self) -> u16 {
self.class_id
}
pub fn method_id(&self) -> u16 {
self.method_id
}
}
impl Default for Close {
fn default() -> Self {
Self {
reply_code: REPLY_SUCCESS,
reply_text: ShortStr::default(),
class_id: 0,
method_id: 0,
}
}
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct CloseOk;
#[derive(Debug, Serialize, Deserialize)]
pub struct Secure {
pub(crate) challenge: LongStr,
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct SecureOk {
response: LongStr,
}
impl SecureOk {
#[allow(dead_code, /* challenge response not used in PLAIN/AMQPLIAN auth machanism*/)]
pub fn new(response: LongStr) -> Self {
Self { response }
}
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Blocked {
pub(crate) reason: ShortStr,
}
impl Blocked {
pub fn new(reason: ShortStr) -> Self {
Self { reason }
}
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Unblocked;
#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateSecret {
pub(crate) new_secret: LongStr,
pub(crate) reason: ShortStr,
}
impl UpdateSecret {
pub fn new(new_secret: LongStr, reason: ShortStr) -> Self {
Self { new_secret, reason }
}
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct UpdateSecretOk;