fuel_streams_core/utxos/subjects.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
use fuel_streams_macros::subject::{IntoSubject, Subject};
use crate::types::*;
/// Represents a subject for utxos messages in the Fuel ecosystem.
///
/// This struct is used to create and parse subjects related to utxos messages,
/// which can be used for subscribing to or publishing events about utxos messages.
///
/// # Examples
///
/// Creating and parsing a subject:
///
/// ```
/// # use fuel_streams_core::utxos::subjects::UtxosSubject;
/// # use fuel_streams_core::types::*;
/// # use fuel_streams_macros::subject::*;
/// let subject = UtxosSubject {
/// hash: Some(MessageId::from([1u8; 32])),
/// utxo_type: Some(UtxoType::Message),
/// };
/// assert_eq!(
/// subject.parse(),
/// "utxos.message.0x0101010101010101010101010101010101010101010101010101010101010101"
/// );
/// ```
///
/// All utxos messages wildcard:
///
/// ```
/// # use fuel_streams_core::utxos::subjects::UtxosSubject;
/// # use fuel_streams_macros::subject::*;
/// assert_eq!(UtxosSubject::WILDCARD, "utxos.>");
/// ```
///
/// Creating a subject query using the `wildcard` method:
///
/// ```
/// # use fuel_streams_core::utxos::subjects::UtxosSubject;
/// # use fuel_streams_core::types::*;
/// # use fuel_streams_macros::subject::*;
/// let wildcard = UtxosSubject::wildcard(
/// Some(MessageId::from([1u8; 32])),
/// None,
/// );
/// assert_eq!(wildcard, "utxos.*.0x0101010101010101010101010101010101010101010101010101010101010101");
/// ```
///
/// Using the builder pattern:
///
/// ```
/// # use fuel_streams_core::utxos::subjects::UtxosSubject;
/// # use fuel_streams_core::types::*;
/// # use fuel_streams_macros::subject::*;
/// let subject = UtxosSubject::new()
/// .with_hash(Some(MessageId::from([1u8; 32])))
/// .with_utxo_type(Some(UtxoType::Message));
/// assert_eq!(subject.parse(), "utxos.message.0x0101010101010101010101010101010101010101010101010101010101010101");
/// ```
#[derive(Subject, Debug, Clone, Default)]
#[subject_wildcard = "utxos.>"]
#[subject_format = "utxos.{utxo_type}.{hash}"]
pub struct UtxosSubject {
pub hash: Option<MessageId>,
pub utxo_type: Option<UtxoType>,
}
#[cfg(test)]
mod tests {
use fuel_streams_macros::subject::SubjectBuildable;
use super::*;
#[test]
fn test_utxos_subject_wildcard() {
assert_eq!(UtxosSubject::WILDCARD, "utxos.>");
}
#[test]
fn test_utxos_message_subject_creation() {
let utxo_subject = UtxosSubject::new()
.with_hash(Some(MessageId::zeroed()))
.with_utxo_type(Some(UtxoType::Message));
assert_eq!(
utxo_subject.to_string(),
"utxos.message.0x0000000000000000000000000000000000000000000000000000000000000000"
);
}
#[test]
fn test_utxos_coin_subject_creation() {
let utxo_subject = UtxosSubject::new()
.with_hash(Some(MessageId::zeroed()))
.with_utxo_type(Some(UtxoType::Coin));
assert_eq!(
utxo_subject.to_string(),
"utxos.coin.0x0000000000000000000000000000000000000000000000000000000000000000"
);
}
#[test]
fn test_utxos_contract_subject_creation() {
let utxo_subject = UtxosSubject::new()
.with_hash(Some(MessageId::zeroed()))
.with_utxo_type(Some(UtxoType::Contract));
assert_eq!(
utxo_subject.to_string(),
"utxos.contract.0x0000000000000000000000000000000000000000000000000000000000000000"
);
}
}