diff options
| author | Max Audron <audron@cocaine.farm> | 2021-08-20 14:11:54 +0200 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2021-09-28 17:59:22 +0200 |
| commit | 04de7e03c5ff792ba5b0a4df6ecbde18c6f25ad9 (patch) | |
| tree | fdbdf8b4dd2f9d52ab7d9da71e277862d85f1d4f /examples/statetracker/src/formatter.rs | |
| parent | rename override_type to type (diff) | |
statetracker: first iteration of working rpc object
Diffstat (limited to '')
| -rw-r--r-- | examples/statetracker/src/formatter.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/examples/statetracker/src/formatter.rs b/examples/statetracker/src/formatter.rs new file mode 100644 index 0000000..4ceb68f --- /dev/null +++ b/examples/statetracker/src/formatter.rs @@ -0,0 +1,47 @@ +use druid::text::{Formatter, Validation, ValidationError}; + +pub struct U16Formatter; + +impl Formatter<u16> for U16Formatter { + fn format(&self, value: &u16) -> String { + value.to_string() + } + + fn validate_partial_input(&self, input: &str, _sel: &druid::text::Selection) -> Validation { + if input.is_empty() { + return Validation::success(); + } + + if input.len() > 6 { + return Validation::failure(U16ValidationError::WrongNumberOfCharacters); + } + + match input.parse::<u16>() { + Ok(_) => Validation::success(), + Err(err) => Validation::failure(err), + } + } + + fn value(&self, input: &str) -> Result<u16, ValidationError> { + if input.is_empty() || input.len() > 5 { + return Err(ValidationError::new( + U16ValidationError::WrongNumberOfCharacters, + )); + } + + input.parse().map_err(|err| ValidationError::new(err)) + } +} + +#[derive(Debug, Clone)] +pub enum U16ValidationError { + WrongNumberOfCharacters, +} + +impl std::fmt::Display for U16ValidationError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", std::any::type_name_of_val(self)) + } +} + +impl std::error::Error for U16ValidationError {} |
