use crate::{Timestamp, Uint32, bytes::JsonBytes};
use ckb_types::{packed, prelude::*};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
pub type AlertId = Uint32;
pub type AlertPriority = Uint32;
#[derive(Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash, Debug, JsonSchema)]
pub struct Alert {
pub id: AlertId,
pub cancel: AlertId,
pub min_version: Option<String>,
pub max_version: Option<String>,
pub priority: AlertPriority,
pub notice_until: Timestamp,
pub message: String,
pub signatures: Vec<JsonBytes>,
}
#[derive(Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash, Debug, JsonSchema)]
pub struct AlertMessage {
pub id: AlertId,
pub priority: AlertPriority,
pub notice_until: Timestamp,
pub message: String,
}
impl From<Alert> for packed::Alert {
fn from(json: Alert) -> Self {
let Alert {
id,
cancel,
min_version,
max_version,
priority,
notice_until,
message,
signatures,
} = json;
let raw = packed::RawAlert::new_builder()
.id(id)
.cancel(cancel)
.min_version(min_version)
.max_version(max_version)
.priority(priority)
.notice_until(notice_until)
.message(message)
.build();
packed::Alert::new_builder()
.raw(raw)
.signatures(
signatures
.into_iter()
.map(Into::into)
.collect::<Vec<packed::Bytes>>(),
)
.build()
}
}
impl From<packed::Alert> for Alert {
fn from(input: packed::Alert) -> Self {
let raw = input.raw();
Alert {
id: raw.id().into(),
cancel: raw.cancel().into(),
min_version: raw
.as_reader()
.min_version()
.to_opt()
.map(|b| unsafe { b.as_utf8_unchecked() }.to_owned()),
max_version: raw
.as_reader()
.max_version()
.to_opt()
.map(|b| unsafe { b.as_utf8_unchecked() }.to_owned()),
priority: raw.priority().into(),
notice_until: raw.notice_until().into(),
message: unsafe { raw.as_reader().message().as_utf8_unchecked().to_string() },
signatures: input.signatures().into_iter().map(Into::into).collect(),
}
}
}
impl From<packed::Alert> for AlertMessage {
fn from(input: packed::Alert) -> Self {
let raw = input.raw();
AlertMessage {
id: raw.id().into(),
priority: raw.priority().into(),
notice_until: raw.notice_until().into(),
message: unsafe { raw.as_reader().message().as_utf8_unchecked().to_string() },
}
}
}