Buttplug sex toy control library
1use getset::{CopyGetters, Getters};
2use uuid::Uuid;
3
4use super::server_device_feature::ServerDeviceFeature;
5#[derive(Debug, Clone, Getters, CopyGetters)]
6pub struct ServerDeviceDefinition {
7 #[getset(get = "pub")]
8 /// Given name of the device this instance represents.
9 name: String,
10 #[getset(get_copy = "pub")]
11 id: Uuid,
12 #[getset(get_copy = "pub")]
13 base_id: Option<Uuid>,
14 #[getset(get = "pub")]
15 protocol_variant: Option<String>,
16 #[getset(get_copy = "pub")]
17 message_gap_ms: Option<u32>,
18 #[getset(get = "pub")]
19 display_name: Option<String>,
20 #[getset(get_copy = "pub")]
21 allow: bool,
22 #[getset(get_copy = "pub")]
23 deny: bool,
24 #[getset(get_copy = "pub")]
25 index: u32,
26 #[getset(get = "pub")]
27 features: Vec<ServerDeviceFeature>,
28}
29
30#[derive(Debug)]
31pub struct ServerDeviceDefinitionBuilder {
32 def: ServerDeviceDefinition,
33}
34
35impl ServerDeviceDefinitionBuilder {
36 pub fn new(name: &str, id: &Uuid) -> Self {
37 Self {
38 def: ServerDeviceDefinition {
39 name: name.to_owned(),
40 id: *id,
41 base_id: None,
42 protocol_variant: None,
43 message_gap_ms: None,
44 display_name: None,
45 allow: false,
46 deny: false,
47 index: 0,
48 features: vec![],
49 },
50 }
51 }
52
53 // Used to create new user definitions from a base definition.
54 pub fn from_base(value: &ServerDeviceDefinition, id: Uuid, with_features: bool) -> Self {
55 let mut value = value.clone();
56 value.base_id = Some(value.id);
57 value.id = id;
58 if with_features {
59 value.features = value
60 .features()
61 .iter()
62 .map(|x| x.as_new_user_feature())
63 .collect();
64 } else {
65 value.features = vec![];
66 }
67 ServerDeviceDefinitionBuilder { def: value }
68 }
69
70 pub fn from_user(value: &ServerDeviceDefinition) -> Self {
71 ServerDeviceDefinitionBuilder { def: value.clone() }
72 }
73
74 pub fn id(&mut self, id: Uuid) -> &mut Self {
75 self.def.id = id;
76 self
77 }
78
79 pub fn base_id(&mut self, id: Uuid) -> &mut Self {
80 self.def.base_id = Some(id);
81 self
82 }
83
84 pub fn display_name(&mut self, name: &Option<String>) -> &mut Self {
85 self.def.display_name = name.clone();
86 self
87 }
88
89 pub fn protocol_variant(&mut self, variant: &str) -> &mut Self {
90 self.def.protocol_variant = Some(variant.to_owned());
91 self
92 }
93
94 pub fn message_gap_ms(&mut self, gap: u32) -> &mut Self {
95 self.def.message_gap_ms = Some(gap);
96 self
97 }
98
99 pub fn allow(&mut self, allow: bool) -> &mut Self {
100 self.def.allow = allow;
101 self
102 }
103
104 pub fn deny(&mut self, deny: bool) -> &mut Self {
105 self.def.deny = deny;
106 self
107 }
108
109 pub fn index(&mut self, index: u32) -> &mut Self {
110 self.def.index = index;
111 self
112 }
113
114 pub fn add_feature(&mut self, feature: &ServerDeviceFeature) -> &mut Self {
115 self.def.features.push(feature.clone());
116 self
117 }
118
119 pub fn replace_feature(&mut self, feature: &ServerDeviceFeature) -> &mut Self {
120 if let Some(f) = self
121 .def
122 .features
123 .iter_mut()
124 .find(|x| x.id() == feature.id())
125 {
126 *f = feature.clone();
127 }
128 self
129 }
130
131 pub fn finish(&self) -> ServerDeviceDefinition {
132 self.def.clone()
133 }
134}