1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Flutter counterpart: `services/keyboard_inserted_content.dart`.
/// A class representing rich content (such as a PNG image) inserted via the
/// system input method.
///
/// The following data is represented in this class:
/// - MIME Type
/// - Bytes
/// - URI
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct KeyboardInsertedContent {
/// The mime type of the inserted content.
pub mime_type: String,
/// The URI (location) of the inserted content, usually a "content://" URI.
pub uri: String,
/// The bytedata of the inserted content.
pub data: Option<Vec<u8>>,
}
impl KeyboardInsertedContent {
/// Creates an object to represent content that is inserted from the virtual
/// keyboard.
///
/// The mime type and URI will always be provided, but the bytedata may be null.
pub fn new(mime_type: impl Into<String>, uri: impl Into<String>) -> KeyboardInsertedContent {
KeyboardInsertedContent {
mime_type: mime_type.into(),
uri: uri.into(),
data: None,
}
}
/// Dart `KeyboardInsertedContent(data:)`.
pub fn data(mut self, data: Vec<u8>) -> KeyboardInsertedContent {
self.data = Some(data);
self
}
/// Convenience getter to check if bytedata is available for the inserted content.
pub fn has_data(&self) -> bool {
self.data.as_ref().is_some_and(|data| !data.is_empty())
}
}
impl std::fmt::Display for KeyboardInsertedContent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"KeyboardInsertedContent({}, {}, {:?})",
self.mime_type, self.uri, self.data
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn equality_and_has_data() {
let empty = KeyboardInsertedContent::new("image/png", "content://item");
let with_bytes =
KeyboardInsertedContent::new("image/png", "content://item").data(vec![1, 2]);
assert_eq!(
empty,
KeyboardInsertedContent::new("image/png", "content://item")
);
assert_ne!(empty, with_bytes);
assert!(!empty.has_data());
assert!(with_bytes.has_data());
assert!(
!KeyboardInsertedContent::new("image/png", "content://item")
.data(Vec::new())
.has_data()
);
}
}