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
use std::collections::HashMap;
use crate::runtime::dev::prelude::*;
/// Add fixed fields to messages.
///
/// With a `payload_field_name`, any incoming message is wrapped in a
/// `Pmt::MapStrPmt` and stored under that field. Without one, incoming messages
/// must already be `Pmt::MapStrPmt`; the annotation fields are merged into the
/// map.
///
/// # Message Inputs
///
/// `in`: Message to annotate. `Pmt::Finished` terminates the block.
///
/// # Message Outputs
///
/// `out`: Annotated `Pmt::MapStrPmt` messages.
///
/// # Usage
/// ```
/// use std::collections::HashMap;
///
/// use futuresdr::blocks::MessageAnnotator;
/// use futuresdr::runtime::Pmt;
///
/// let mut fields = HashMap::new();
/// fields.insert("source".to_string(), Pmt::String("rx0".to_string()));
/// let annotator = MessageAnnotator::new(fields, Some("payload"));
/// ```
#[derive(Block)]
#[message_inputs(r#in)]
#[message_outputs(out)]
#[null_kernel]
pub struct MessageAnnotator {
annotation_prototype: HashMap<String, Pmt>,
payload_field_name: Option<String>,
}
impl MessageAnnotator {
/// Create [`MessageAnnotator`] block.
pub fn new(annotation: HashMap<String, Pmt>, payload_field_name: Option<&str>) -> Self {
Self {
annotation_prototype: annotation,
payload_field_name: payload_field_name.map(String::from),
}
}
async fn r#in(
&mut self,
io: &mut WorkIo,
mo: &mut MessageOutputs,
_meta: &mut BlockMeta,
p: Pmt,
) -> Result<Pmt> {
if let Some(payload_field_name) = self.payload_field_name.clone() {
match p {
Pmt::Finished => {
io.finished = true;
}
p => {
let mut annotated_message = self.annotation_prototype.clone();
annotated_message.insert(payload_field_name, p);
mo.post("out", Pmt::MapStrPmt(annotated_message)).await?;
}
}
} else {
match p {
Pmt::Finished => {
io.finished = true;
}
Pmt::MapStrPmt(mut annotated_message) => {
annotated_message.extend(self.annotation_prototype.clone());
mo.post("out", Pmt::MapStrPmt(annotated_message)).await?;
}
_ => return Ok(Pmt::InvalidValue),
}
}
Ok(Pmt::Ok)
}
}