Module gotham_formdata::conversion [−][src]
Expand description
This mod contains conversion traits for common used types, that allows them to be created from a stream of bytes. Furthermore, it allows every type that implements FromStr plus some other common types to be converted.
You cannot implement any of these traits manually. This is intentional. Instead, if you want to provide a custom conversion method, just implement it as a method for your type:
use futures_util::{FutureExt, StreamExt}; use gotham_formdata::{conversion::ConversionFuture, value::{BytesOrString, Value}, FormData}; /// This type parses Base64-encoded values to a [Vec<u8>]. struct Base64(Vec<u8>); impl Base64 { // the method signature needs to be roughly equivalent to this async fn convert_value<E>( name: &str, value: Value<'_, gotham_formdata::Error<E>> ) -> Result<Self, gotham_formdata::Error<E>> where E: std::error::Error { let decoded = match value.value { BytesOrString::Bytes(mut stream) => { let mut buf: Vec<u8> = Vec::new(); while let Some(data) = stream.next().await { buf.extend_from_slice(&data?); } base64::decode(&buf) }, BytesOrString::String(string) => base64::decode(string.as_bytes()) }.map_err(|err| gotham_formdata::Error::IllegalField(name.to_owned(), err.into()))?; Ok(Self(decoded)) } } #[derive(FormData)] struct MyData { foo: Base64 }
Traits
ConvertFromStr | This trait is used to convert types that implement FromStr from a stream of bytes. |
ConvertRawBytes | This trait is used to convert |
Type Definitions
ConversionFuture | The future returned from conversion methods. |