Lines
100 %
Functions
66.67 %
Branches
//! Numeric amount entry widget.
//!
//! Wraps an [`Editor`] and keeps every typed character; the server refuses
//! text that is not an amount.
use super::editor::Editor;
/// A text field for amount text.
#[derive(Debug, Clone, Default)]
pub struct AmountWidget {
editor: Editor,
}
impl AmountWidget {
#[must_use]
pub fn with_value(value: impl Into<String>) -> Self {
Self {
editor: Editor::with_buffer(value),
/// Buffer text (the current amount string).
pub fn value(&self) -> &str {
self.editor.buffer()
pub fn editor(&self) -> &Editor {
&self.editor
pub fn insert_char(&mut self, c: char) {
self.editor.insert_char(c);
/// Direct access to the inner editor for motion and delete operations.
pub fn as_editor_mut(&mut self) -> &mut Editor {
&mut self.editor
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn accepts_digits_and_separators() {
let mut w = AmountWidget::default();
for c in "100.5/3".chars() {
w.insert_char(c);
assert_eq!(w.value(), "100.5/3");
fn accepts_negative_sign() {
w.insert_char('-');
w.insert_char('5');
assert_eq!(w.value(), "-5");
fn keeps_every_typed_character() {
for c in "12,50 a".chars() {
assert_eq!(w.value(), "12,50 a");
fn delete_backward_via_editor_mut() {
w.insert_char('0');
w.as_editor_mut().delete_backward();
assert_eq!(w.value(), "5");
fn with_value_sets_initial_buffer() {
let w = AmountWidget::with_value("42/7");
assert_eq!(w.value(), "42/7");