snarkvm_ledger_narwhal_transmission_id/
string.rs1use super::*;
17
18impl<N: Network> FromStr for TransmissionID<N> {
19 type Err = Error;
20
21 fn from_str(input: &str) -> Result<Self, Self::Err> {
23 let (id, checksum) = input.split_once('.').ok_or_else(|| anyhow!("Invalid transmission ID: {input}"))?;
25 if id.starts_with(SOLUTION_ID_PREFIX) {
27 Ok(Self::Solution(
28 SolutionID::from_str(id)?,
29 N::TransmissionChecksum::from_str(checksum)
30 .map_err(|_| anyhow!("Failed to parse checksum: {checksum}"))?,
31 ))
32 } else if id.starts_with(TRANSACTION_PREFIX) {
33 Ok(Self::Transaction(
34 N::TransactionID::from_str(id).map_err(|_| anyhow!("Failed to parse transaction ID: {id}"))?,
35 N::TransmissionChecksum::from_str(checksum)
36 .map_err(|_| anyhow!("Failed to parse checksum: {checksum}"))?,
37 ))
38 } else {
39 bail!("Invalid transmission ID: {input}")
40 }
41 }
42}
43
44impl<N: Network> Debug for TransmissionID<N> {
45 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
47 Display::fmt(self, f)
48 }
49}
50
51impl<N: Network> Display for TransmissionID<N> {
52 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
54 match self {
55 Self::Ratification => write!(f, "ratification"),
56 Self::Solution(id, checksum) => write!(f, "{}.{}", id, checksum),
57 Self::Transaction(id, checksum) => write!(f, "{}.{}", id, checksum),
58 }
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65 use console::network::MainnetV0;
66
67 type CurrentNetwork = MainnetV0;
68
69 #[test]
70 fn test_string() {
71 let rng = &mut TestRng::default();
72
73 for expected in crate::test_helpers::sample_transmission_ids(rng) {
74 let candidate = format!("{expected}");
76 assert_eq!(expected, TransmissionID::from_str(&candidate).unwrap());
77 assert!(TransmissionID::<CurrentNetwork>::from_str(&candidate[1..]).is_err());
78 }
79 }
80}