Skip to content

Commit 0642d49

Browse files
committed
Use new InfoBuilder API in example
Automatically creates queue names
1 parent 569ec15 commit 0642d49

2 files changed

Lines changed: 24 additions & 24 deletions

File tree

examples/repeat.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,12 @@ extern crate msgflo;
44
use msgflo::participant::{ParticipantInfo, Participant, ParticipantPort, InfoBuilder};
55

66
fn main() {
7-
let info = ParticipantInfo {
8-
id: "repeat113".to_string(),
9-
role: "repeat".to_string(),
10-
component: "rust/Repeat".to_string(),
11-
label: Some("Repeats input as-is".to_string()),
12-
icon: None,
13-
inports: vec! [ ParticipantPort { id: "in".to_string(), queue: "repeat.IN".to_string() } ],
14-
outports: vec! [ ParticipantPort { id: "out".to_string(), queue: "repeat.OUT".to_string() } ],
15-
};
16-
17-
let mut c = InfoBuilder::new("rust/Repeat")
7+
let info = InfoBuilder::new("rust/Repeat")
188
.label("Repeats input as-is")
199
.inport("in")
20-
.inport("out")
10+
.outport("out")
2111
.build();
2212

23-
//let s = std::str::from_utf8(&body).unwrap();
24-
//let json_obj: json::Object = json::decode(s).expect("json parse error");
25-
2613
fn process_repeat(input: Vec<u8>) -> Result<Vec<u8>, Vec<u8>> {
2714
println!("process_repeat:");
2815
return Ok(input);

src/participant.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct InfoBuilder {
3535
impl InfoBuilder {
3636
pub fn new(component: &str) -> InfoBuilder {
3737
InfoBuilder {
38-
info: ParticipantInfo { .. Default::default() }
38+
info: ParticipantInfo { component: component.to_string(), .. Default::default() }
3939
}
4040
}
4141

@@ -44,6 +44,11 @@ impl InfoBuilder {
4444
self
4545
}
4646

47+
pub fn role(&mut self, role: &str) -> &mut InfoBuilder {
48+
self.info.role = role.to_string();
49+
self
50+
}
51+
4752
pub fn inport(&mut self, id: &str) -> &mut InfoBuilder {
4853
let port = ParticipantPort { id: id.to_string(), queue: "".to_string() };
4954
self.info.inports.push(port);
@@ -206,10 +211,10 @@ impl Default for ParticipantOptions {
206211
}
207212
}
208213

209-
fn normalize_info(info: &ParticipantInfo, options: &ParticipantOptions) -> ParticipantInfo {
214+
fn normalize_info(old: &ParticipantInfo, options: &ParticipantOptions) -> ParticipantInfo {
210215
use rand::{thread_rng, Rng};
211216

212-
let mut new = info.clone();
217+
let mut new = old.clone();
213218

214219
// normalize role name
215220
if options.role != "" {
@@ -225,15 +230,22 @@ fn normalize_info(info: &ParticipantInfo, options: &ParticipantOptions) -> Parti
225230
new.id = format!("{}-{}", new.role, id_rnd);
226231

227232
// generate port defaults
228-
229-
// FIXME: allow to specify queue explicitly
230-
//new.inports =
233+
let role = new.role.to_string(); // NOTE: would be nice to be able to pass reference to info?
234+
let normalize_port = | o: &ParticipantPort | -> ParticipantPort {
235+
let mut p = o.clone();
236+
if p.queue == "" {
237+
p.queue = default_queue(role.to_string(), p.id.to_string());
238+
}
239+
return p;
240+
};
241+
new.inports = old.inports.iter().map(&normalize_port).collect();
242+
new.outports = old.outports.iter().map(&normalize_port).collect();
231243

232244
return new;
233245
}
234246

235247
fn default_queue(role: String, port_name: String) -> String {
236-
return role + "." + &port_name.to_uppercase();
248+
return format!("{}.{}", role, port_name.to_uppercase());
237249
}
238250

239251
fn parse(options: &mut ParticipantOptions) {
@@ -253,12 +265,13 @@ fn parse(options: &mut ParticipantOptions) {
253265
// XXX: seems rust-amqp makes program hangs forever if error occurs / channel is borked?
254266
// TODO: pass port info in/out of process()
255267
// TODO: nicer way to declare ports? ideally they are enums not stringly typed?
256-
pub fn main(p: Participant) {
268+
pub fn main(orig: Participant) {
257269

258270
let mut options = ParticipantOptions { .. Default::default() };
259271
parse(&mut options);
260272

261-
normalize_info(&p.info, &options);
273+
let info = normalize_info(&orig.info, &options);
274+
let p = Participant { info: info, process: orig.process }; // XXX: hack
262275

263276
let mut c = start_participant(&p, &options);
264277
println!("{}({}) started", &options.role, &p.info.component);

0 commit comments

Comments
 (0)