Trait druid::text::Formatter

source ·
pub trait Formatter<T> {
    // Required methods
    fn format(&self, value: &T) -> String;
    fn validate_partial_input(&self, input: &str, sel: &Selection) -> Validation;
    fn value(&self, input: &str) -> Result<T, ValidationError>;

    // Provided method
    fn format_for_editing(&self, value: &T) -> String { ... }
}
Expand description

A trait for types that create, interpret, and validate textual representations of values.

A formatter has two responsibilities: converting a value into an appropriate string representation, and attempting to convert a string back into the appropriate value.

In addition, a formatter performs validation on partial strings; that is, it determines whether or not a string represents a potentially valid value, even if it is not currently valid.

Required Methods§

source

fn format(&self, value: &T) -> String

Return the string representation of this value.

source

fn validate_partial_input(&self, input: &str, sel: &Selection) -> Validation

Determine whether the newly edited text is valid for this value type.

This always returns a Validation object which indicates if validation was successful or not, and which can also optionally, regardless of success or failure, include new text and selection values that should replace the current ones.

Replacing the text or selection during validation

Your Formatter may wish to change the current text or selection during editing for a number of reasons. For instance if validation fails, you may wish to allow editing to continue, but select the invalid region; alternatively you may consider input valid but want to transform it, such as by changing case or inserting spaces.

If you do not explicitly set replacement text, and validation is not successful, the edit will be ignored.

source

fn value(&self, input: &str) -> Result<T, ValidationError>

The value represented by the input, or an error if the input is invalid.

This must return Ok() for any string created by format.

Provided Methods§

source

fn format_for_editing(&self, value: &T) -> String

Return the string representation of this value, to be used during editing.

This can be used if you want the text to differ based on whether or not it is being edited; for instance you might display a dollar sign when not editing, but not display it during editing.

Implementors§

source§

impl<T> Formatter<T> for ParseFormatter<T>where T: FromStr, <T as FromStr>::Err: Error + 'static,