inset_services/
keyboard_inserted_content.rs1#[derive(Clone, Debug, PartialEq, Eq, Hash)]
11pub struct KeyboardInsertedContent {
12 pub mime_type: String,
14 pub uri: String,
16 pub data: Option<Vec<u8>>,
18}
19
20impl KeyboardInsertedContent {
21 pub fn new(mime_type: impl Into<String>, uri: impl Into<String>) -> KeyboardInsertedContent {
26 KeyboardInsertedContent {
27 mime_type: mime_type.into(),
28 uri: uri.into(),
29 data: None,
30 }
31 }
32
33 pub fn data(mut self, data: Vec<u8>) -> KeyboardInsertedContent {
35 self.data = Some(data);
36 self
37 }
38
39 pub fn has_data(&self) -> bool {
41 self.data.as_ref().is_some_and(|data| !data.is_empty())
42 }
43}
44
45impl std::fmt::Display for KeyboardInsertedContent {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 write!(
48 f,
49 "KeyboardInsertedContent({}, {}, {:?})",
50 self.mime_type, self.uri, self.data
51 )
52 }
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn equality_and_has_data() {
61 let empty = KeyboardInsertedContent::new("image/png", "content://item");
62 let with_bytes =
63 KeyboardInsertedContent::new("image/png", "content://item").data(vec![1, 2]);
64 assert_eq!(
65 empty,
66 KeyboardInsertedContent::new("image/png", "content://item")
67 );
68 assert_ne!(empty, with_bytes);
69 assert!(!empty.has_data());
70 assert!(with_bytes.has_data());
71 assert!(
72 !KeyboardInsertedContent::new("image/png", "content://item")
73 .data(Vec::new())
74 .has_data()
75 );
76 }
77}