pub trait FieldValue: Sized + 'static {
// Required method
fn from_field_text(text: &str) -> Result<Self, FieldValueError>;
}Expand description
A field’s value converted from its submitted text, the conversion trait for scalar fields.
String, the integer types, f32, f64, and bool implement it, as does an enumeration with #[derive(FieldValue)]. Implement it for a type of your own to accept that type as a field, with the text converted by your code: a failure is an invalid_type violation, like any other.
The text is valid UTF-8, whatever character set the part declared, since text in another character set is refused before it is converted.
use base_web_content::{FieldValue, FieldValueError};
/// A colour written as `#rrggbb`.
struct Colour(u32);
impl FieldValue for Colour {
fn from_field_text(text: &str) -> Result<Self, FieldValueError> {
let digits = text.strip_prefix('#').filter(|digits| digits.len() == 6 && digits.bytes().all(|byte| byte.is_ascii_hexdigit()));
let digits = digits.ok_or(FieldValueError)?;
u32::from_str_radix(digits, 16).map(Colour).map_err(|_| FieldValueError)
}
}
assert_eq!(Colour::from_field_text("#ff8000").map(|colour| colour.0), Ok(0xff8000));
assert!(Colour::from_field_text("orange").is_err());Required Methods§
Sourcefn from_field_text(text: &str) -> Result<Self, FieldValueError>
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
The value the text spells.
§Errors
FieldValueError when the text does not spell a value of this type.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl FieldValue for String
Any text, unchanged.
impl FieldValue for String
Any text, unchanged.
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
Source§impl FieldValue for bool
Exactly true or false.
impl FieldValue for bool
Exactly true or false.
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
Source§impl FieldValue for f32
A finite decimal number with an optional sign, fraction, and exponent. Whitespace, _ separators, hexadecimal, and NaN or infinity in any spelling, including a number too large for the type, are refused.
impl FieldValue for f32
A finite decimal number with an optional sign, fraction, and exponent. Whitespace, _ separators, hexadecimal, and NaN or infinity in any spelling, including a number too large for the type, are refused.
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
Source§impl FieldValue for f64
A finite decimal number with an optional sign, fraction, and exponent. Whitespace, _ separators, hexadecimal, and NaN or infinity in any spelling, including a number too large for the type, are refused.
impl FieldValue for f64
A finite decimal number with an optional sign, fraction, and exponent. Whitespace, _ separators, hexadecimal, and NaN or infinity in any spelling, including a number too large for the type, are refused.
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
Source§impl FieldValue for i8
An optional + or - followed by one or more ASCII decimal digits, within the type’s range.
impl FieldValue for i8
An optional + or - followed by one or more ASCII decimal digits, within the type’s range.
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
Source§impl FieldValue for i16
An optional + or - followed by one or more ASCII decimal digits, within the type’s range.
impl FieldValue for i16
An optional + or - followed by one or more ASCII decimal digits, within the type’s range.
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
Source§impl FieldValue for i32
An optional + or - followed by one or more ASCII decimal digits, within the type’s range.
impl FieldValue for i32
An optional + or - followed by one or more ASCII decimal digits, within the type’s range.
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
Source§impl FieldValue for i64
An optional + or - followed by one or more ASCII decimal digits, within the type’s range.
impl FieldValue for i64
An optional + or - followed by one or more ASCII decimal digits, within the type’s range.
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
Source§impl FieldValue for i128
An optional + or - followed by one or more ASCII decimal digits, within the type’s range.
impl FieldValue for i128
An optional + or - followed by one or more ASCII decimal digits, within the type’s range.
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
Source§impl FieldValue for isize
An optional + or - followed by one or more ASCII decimal digits, within the type’s range.
impl FieldValue for isize
An optional + or - followed by one or more ASCII decimal digits, within the type’s range.
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
Source§impl FieldValue for u8
An optional + or - followed by one or more ASCII decimal digits, within the type’s range, so -0 is zero.
impl FieldValue for u8
An optional + or - followed by one or more ASCII decimal digits, within the type’s range, so -0 is zero.
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
Source§impl FieldValue for u16
An optional + or - followed by one or more ASCII decimal digits, within the type’s range, so -0 is zero.
impl FieldValue for u16
An optional + or - followed by one or more ASCII decimal digits, within the type’s range, so -0 is zero.
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
Source§impl FieldValue for u32
An optional + or - followed by one or more ASCII decimal digits, within the type’s range, so -0 is zero.
impl FieldValue for u32
An optional + or - followed by one or more ASCII decimal digits, within the type’s range, so -0 is zero.
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
Source§impl FieldValue for u64
An optional + or - followed by one or more ASCII decimal digits, within the type’s range, so -0 is zero.
impl FieldValue for u64
An optional + or - followed by one or more ASCII decimal digits, within the type’s range, so -0 is zero.
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
Source§impl FieldValue for u128
An optional + or - followed by one or more ASCII decimal digits, within the type’s range, so -0 is zero.
impl FieldValue for u128
An optional + or - followed by one or more ASCII decimal digits, within the type’s range, so -0 is zero.
fn from_field_text(text: &str) -> Result<Self, FieldValueError>
Source§impl FieldValue for usize
An optional + or - followed by one or more ASCII decimal digits, within the type’s range, so -0 is zero.
impl FieldValue for usize
An optional + or - followed by one or more ASCII decimal digits, within the type’s range, so -0 is zero.