Expand description
Defines a set of special string types that simplify validation. The string types chosen have characteristics that would be useful for basic validation of inputs and/or serve as the basis for more specific validations. The last two are more specific, but occur often enough in some programming problems to be worth including.
NonEmptyString- cannot be emptyTrimmedString- string guaranteed to not have whitespace before or after the stringTrimmedNonBlankString- cannot be empty or contain only whitespace, with both sides trimmedNonBlankString- cannot be empty or contain only whitespace, without trimmingDigitString- cannot be empty and only contains ASCII digitsNumericString- cannot be empty and only contains Unicode numeric charactersHexString- cannot be empty, only contains hexadecimal digits, has an even lengthLowerHexString- cannot be empty, only contains lowercase hexadecimal digits, has an even lengthUpperHexString- cannot be empty, only contains uppercase hexadecimal digits, has an even lengthIdentifierStr- cannot be empty, starts with a letter or underscore, the rest are alphanumeric or underscore.UnixUsername- cannot be empty, matches definition enforced byuseradd.
There are three features supported by this crate.
- The
serdefeature addsSerializeandDeserializesupport to the definedStringTypes - The
book_idsfeature definesIsbn10,Isbn13, andLccn. - The
card_idsfeature definesCreditCard
The StringTypes included by the book_ids and card_ids features are not nearly as commonly
useful as the default set, but are interesting enough to be good examples of how you might
create StringTypes that extend the basic examples to do cover more interesting string types.
StringTypes are expected to be defined as a tuple struct with a single field. This field
should be either a String or another StringType in order to work seamlessly.
To simplify creating StringTypes, we make the following macros public in the macros module.
string_typeis an attribute macro that implements theStringTypetrait and appropriateFrom<T>traits for the tuple struct it is applied to. It also derives some standard traits:Debug,Clone,Hash,Eq,Ord,PartialEq, andPartialOrd.Displayis a derive macro that implements theDisplaytrait in a pretty obvious way.FromStris a derive macro that implements theFromStrtrait usingensure_valid.
§Trait Reasoning
Any StringTypes implement a number of traits, automatically (if you use the macros). You
might wonder why so many, despite the fact that we don’t implement all of the String
interface.
StringType- obviousEnsureValid- define the validation interfaceFrom<S> for String- Easy conversion of theStringTypeto rawStringFrom<S> for StrType::Inner- Easy conversion to inner type, unless it’s aStringDisplay- for formatted printingFromStr- for parsing a string slice into the StringTypeDebug- debug printingClone- easily make a copyHash- make the StringType hashableEqandPartialEq- easy comparison of two objects of the same StringTypeOrdandPartialOrd- easy ordering of two objects of the same StringType
Much of the rest of the String interface is designed for changing the String. Most of those
features would violate the guarantees of most StringTypes. If you need to manipulate the
value of the StringType, you can either convert to String or grab the string slice with
as_str. You can then read or manipulate the raw string. When you are finished, parse it back
into the StringType, which reasserts the guarantees.
Structs§
- Credit
Card - A
StringTypethat defines most credit card strings. Guaranteed to be only digits, within a certain length, and that the check digit is correct. - Digit
String - String guaranteed to have at least one character, and all characters are ASCII digits
- HexString
- String guaranteed to have at least two characters, and all characters are hexadecimal digits
- Identifier
Str - Isbn10
- String containing a 10-character ISBN code.
- Isbn13
- String containing a 13-character ISBN code.
- Lccn
- String representing a Library of Congress Control Number
- Lower
HexString - String guaranteed to have at least two characters, and all characters are lowercase hexadecimal digits
- NonBlank
String - String guaranteed to have at least one non-whitespace character
- NonEmpty
String - String guaranteed to have at least one character
- Numeric
String - String guaranteed to have at least one character, and all characters are Unicode numeric characters
- Trimmed
NonBlank String - String guaranteed to have at least one non-whitespace character
- Trimmed
String - String guaranteed to have at least one non-whitespace character, and no leading/trailing whitespace
- Unix
Username - String guaranteed to have 1-32 characters that are lowercase alphanumeric, ‘’, or ‘-’. The first character is guaranteed to be lowercase letter or ‘’. For historical reasons, the final character is allowed to be a ‘$’.
- Upper
HexString - String guaranteed to have at least two characters, and all characters are uppercase hexadecimal digits
Enums§
- CCParse
Error - Parse
Error - Enumeration specifying likely parsing errors for different string types.
Traits§
- Ensure
Valid - The
EnsureValidtrait defines a method for checking the input string to ensure that it passes any validation checks. The method will not perform any modification of the supplied string slice, it just does validation. - String
Type - The
StringTypetrait defines the public interface for the StringType new types.
Attribute Macros§
- string_
type - Expose the macros that simplify creating string types
Generate the base implementations for a
StringTypeincluding theStringTypetrait and the appropriateFrom<T>implementations.