glory_render/
input_edit.rs1use crate::{Error, TextSelection};
4pub const MAX_INPUT_EDIT_BYTES: usize = 4096;
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub struct InputEditCapture {
7 pub max_bytes: usize,
8}
9impl InputEditCapture {
10 pub fn validate(self) -> Result<(), Error> {
11 if self.max_bytes > 0 && self.max_bytes <= MAX_INPUT_EDIT_BYTES {
12 Ok(())
13 } else {
14 Err(Error::InvalidProps(
15 "input edit byte budget must be between 1 and 4096",
16 ))
17 }
18 }
19}
20#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
21pub enum InputEditIntent {
22 Insert,
23 DeleteBackward,
24 DeleteForward,
25 #[default]
26 Replace,
27}
28#[derive(Clone, Copy, Debug, PartialEq, Eq)]
29pub enum InputEditRejection {
30 TooLong,
31 SelectionUnavailable,
32}
33#[derive(Clone, Debug, PartialEq, Eq)]
34pub enum InputEdit {
35 Value {
36 value: String,
37 revision: u64,
38 selection: TextSelection,
39 before: Option<TextSelection>,
40 intent: InputEditIntent,
41 composing: bool,
42 },
43 Rejected {
44 revision: u64,
45 reason: InputEditRejection,
46 },
47}
48impl InputEdit {
49 pub fn revision(&self) -> u64 {
50 match self {
51 Self::Value { revision, .. } | Self::Rejected { revision, .. } => *revision,
52 }
53 }
54 pub fn validate(&self, capture: InputEditCapture) -> Result<(), Error> {
55 capture.validate()?;
56 if let Self::Value {
57 value,
58 selection,
59 before,
60 ..
61 } = self
62 {
63 if value.len() > capture.max_bytes || value.contains(['\0', '\r', '\n']) {
64 return Err(Error::InvalidEvent(
65 "input edit exceeds its budget or is not normalized single-line text",
66 ));
67 }
68 selection
69 .validate(value.encode_utf16().count())
70 .map_err(|_| Error::InvalidEvent("invalid input edit selection"))?;
71 if let Some(before) = before {
72 before
73 .validate(capture.max_bytes)
74 .map_err(|_| Error::InvalidEvent("invalid prior input edit selection"))?;
75 }
76 }
77 Ok(())
78 }
79}