1use crate::internal::ffi;
6use crate::{Error, Result};
7
8#[derive(Debug)]
10pub struct Domain {
11 pub(crate) id: cyclonedds_sys::dds_domainid_t,
12 pub(crate) inner: cyclonedds_sys::dds_entity_t,
13}
14
15impl Domain {
16 pub fn new(domain_id: u32) -> Result<Self> {
36 let inner = ffi::dds_create_domain(domain_id)?;
37 Ok(Self {
38 id: domain_id,
39 inner,
40 })
41 }
42
43 pub fn new_with_xml_config(domain_id: u32, config: &str) -> Result<Self> {
75 let config = std::ffi::CString::new(config).map_err(|_err| Error::BadParameter)?;
76 let inner = ffi::dds_create_domain_with_config(domain_id, &config)?;
77 Ok(Self {
78 id: domain_id,
79 inner,
80 })
81 }
82}
83
84impl Drop for Domain {
85 fn drop(&mut self) {
86 if self.inner != 0 {
87 let result = ffi::dds_delete(self.inner);
88 debug_assert!(
89 result.is_ok(),
90 "unable to delete {self:?}: failed with {result:?}"
91 );
92 }
93 }
94}
95
96impl Default for Domain {
97 fn default() -> Self {
98 Self {
99 id: cyclonedds_sys::DOMAIN_DEFAULT,
100 inner: 0,
101 }
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn test_domain_create() {
111 let domain_id = crate::tests::domain::unique_id();
112 Domain::new(domain_id).unwrap();
113 }
114
115 #[test]
116 fn test_domain_create_default() {
117 let domain = Domain::default();
118 assert_eq!(domain.id, cyclonedds_sys::DOMAIN_DEFAULT);
119 }
120
121 #[test]
122 fn test_domain_create_with_explicit_default_id() {
123 let domain_id = cyclonedds_sys::DOMAIN_DEFAULT;
124 let domain = Domain::new(domain_id).unwrap_err();
125 assert_eq!(domain, Error::BadParameter);
126 }
127
128 #[test]
129 fn test_domain_create_with_empty_xml_config() {
130 let domain_id = crate::tests::domain::unique_id();
131 let xml_config = "";
132 Domain::new_with_xml_config(domain_id, xml_config).unwrap();
133 }
134
135 #[test]
136 fn test_domain_create_with_xml_config_with_invalid_string() {
137 let domain_id = crate::tests::domain::unique_id();
138 let xml_config = "\0";
139 let domain = Domain::new_with_xml_config(domain_id, xml_config).unwrap_err();
140 assert_eq!(domain, Error::BadParameter);
141 }
142
143 #[test]
144 fn test_domain_create_with_xml_config_with_explicit_default_id() {
145 let domain_id = cyclonedds_sys::DOMAIN_DEFAULT;
146 let xml_config = "";
147 let domain = Domain::new_with_xml_config(domain_id, xml_config).unwrap_err();
148 assert_eq!(domain, Error::BadParameter);
149 }
150
151 #[test]
152 fn test_domain_create_with_xml_config_with_malformed_xml() {
153 let domain_id = crate::tests::domain::unique_id();
154 let xml_config = "<>";
155 let domain = Domain::new_with_xml_config(domain_id, xml_config).unwrap_err();
156 assert_eq!(domain, Error::NonSpecific);
157 }
158
159 #[test]
160 fn test_domain_create_with_xml_config_with_valid_xml() {
161 let domain_id = crate::tests::domain::unique_id();
162 let xml_config = "<Domain><General><MaxMessageSize>1400B</MaxMessageSize></General>";
163 Domain::new_with_xml_config(domain_id, xml_config).unwrap();
164 }
165}