dfx_base/
message_factory.rs1use crate::field_map::Group;
2use crate::field_map::Tag;
3use crate::message::Message;
4use crate::tags;
5use std::collections::BTreeMap;
6use std::fmt::Debug;
7use std::sync::Arc;
8use std::sync::Mutex;
9
10pub trait MessageFactory: Debug + Send {
11 fn get_supported_begin_strings(&self) -> Vec<String>;
12 fn create(&self, begin_string: &str, msg_type: &str) -> Result<Message, MessageFactoryError>;
13 fn create_group(&self, begin_string: &str, msg_type: &str, group_counter_tag: Tag) -> Option<Group>;
14}
15
16#[derive(Clone, Debug)]
17pub enum MessageFactoryError {
18 UnsupportedBeginString { begin_string: String, message: String },
19 UnsupportedMsgType { msg_type: String, message: String },
20}
21
22impl MessageFactoryError {
23 pub fn message(&self) -> String {
24 match self {
25 MessageFactoryError::UnsupportedBeginString { begin_string, message } => format!("{message}: {begin_string}"),
26 MessageFactoryError::UnsupportedMsgType { msg_type, message } => format!("{message}: {msg_type}"),
27 }
28 }
29}
30
31#[derive(Clone, Debug)]
32pub struct DefaultMessageFactory {
33 factory_map: BTreeMap<String, Arc<Mutex<Box<dyn MessageFactory>>>>,
34}
35impl DefaultMessageFactory {
36 pub fn new() -> Self {
37 DefaultMessageFactory {
38 factory_map: Default::default()
39 }
40 }
41 pub fn boxed() -> Box<dyn MessageFactory> {
42 Box::new(DefaultMessageFactory::new())
43 }
44}
45
46impl MessageFactory for DefaultMessageFactory {
48 fn get_supported_begin_strings(&self) -> Vec<String> {
49 todo!()
50 }
51
52 fn create(&self, begin_string: &str, msg_type: &str) -> Result<Message, MessageFactoryError> {
53 let mut msg = Message::default();
54 msg.header_mut().set_tag_value(tags::BeginString, begin_string);
55 msg.header_mut().set_tag_value(tags::MsgType, msg_type);
56 Ok(msg)
57 }
58
59 fn create_group(&self, begin_string: &str, msg_type: &str, group_counter_tag: Tag) -> Option<Group> {
60 if let Some(factory) = self.factory_map.get(begin_string) {
61 todo!("# TODO create_group({begin_string}, {msg_type}, {group_counter_tag}, {factory:?})");
62 }
64 None
65 }
66}