snarkvm_ledger_narwhal_transmission_id/
string.rsuse super::*;
impl<N: Network> FromStr for TransmissionID<N> {
type Err = Error;
fn from_str(input: &str) -> Result<Self, Self::Err> {
let (id, checksum) = input.split_once('.').ok_or_else(|| anyhow!("Invalid transmission ID: {input}"))?;
if id.starts_with(SOLUTION_ID_PREFIX) {
Ok(Self::Solution(
SolutionID::from_str(id)?,
N::TransmissionChecksum::from_str(checksum)
.map_err(|_| anyhow!("Failed to parse checksum: {checksum}"))?,
))
} else if id.starts_with(TRANSACTION_PREFIX) {
Ok(Self::Transaction(
N::TransactionID::from_str(id).map_err(|_| anyhow!("Failed to parse transaction ID: {id}"))?,
N::TransmissionChecksum::from_str(checksum)
.map_err(|_| anyhow!("Failed to parse checksum: {checksum}"))?,
))
} else {
bail!("Invalid transmission ID: {input}")
}
}
}
impl<N: Network> Debug for TransmissionID<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
impl<N: Network> Display for TransmissionID<N> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::Ratification => write!(f, "ratification"),
Self::Solution(id, checksum) => write!(f, "{}.{}", id, checksum),
Self::Transaction(id, checksum) => write!(f, "{}.{}", id, checksum),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use console::network::MainnetV0;
type CurrentNetwork = MainnetV0;
#[test]
fn test_string() {
let rng = &mut TestRng::default();
for expected in crate::test_helpers::sample_transmission_ids(rng) {
let candidate = format!("{expected}");
assert_eq!(expected, TransmissionID::from_str(&candidate).unwrap());
assert!(TransmissionID::<CurrentNetwork>::from_str(&candidate[1..]).is_err());
}
}
}