use std::collections::HashSet;
use std::fmt::{self, Display, Formatter};
use internet2::tlv;
use lnpbp::chain::AssetId;
use super::{ChannelId, ProtocolList};
#[derive(Clone, PartialEq, Eq, Debug, Display, NetworkEncode, NetworkDecode)]
#[network_encoding(use_tlv)]
#[display("init({protocols}, {assets:#?})")]
pub struct Init {
pub protocols: ProtocolList,
pub assets: HashSet<AssetId>,
#[network_encoding(unknown_tlvs)]
pub unknown_tlvs: tlv::Stream,
}
#[derive(Clone, PartialEq, Eq, Debug, Display, NetworkEncode, NetworkDecode)]
#[display("ping({pong_size})")]
pub struct Ping {
pub ignored: Vec<u8>,
pub pong_size: u16,
}
#[derive(Clone, PartialEq, Eq, Debug, Error, NetworkEncode, NetworkDecode)]
#[network_encoding(use_tlv)]
pub struct Error {
pub channel_id: Option<ChannelId>,
pub errno: u64,
pub message: Option<String>,
#[network_encoding(unknown_tlvs)]
pub unknown_tlvs: tlv::Stream,
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "Error #{}", self.errno)?;
if let Some(channel_id) = self.channel_id {
write!(f, " on channel {}", channel_id)?;
}
if let Some(ref msg) = self.message {
write!(f, ": {}", msg)?;
}
Ok(())
}
}
#[cfg(test)]
mod test {
use amplify::hex::FromHex;
use internet2::TypedEnum;
use super::*;
use crate::bifrost::Messages;
#[test]
fn bolt1_testvec() {
let init_msg = Messages::Init(Init {
protocols: none!(),
assets: none!(),
unknown_tlvs: none!(),
});
assert_eq!(
init_msg.serialize(),
Vec::<u8>::from_hex("100000000000").unwrap()
);
}
}