Skip to main content

diesel/pg/types/
enum_.rs

1use crate::pg::PgValue;
2use crate::types::enum_::{EnumMapping, EnumTypeMapping, EnumVariant};
3use std::io::Write;
4
5impl EnumMapping<crate::pg::Pg> for EnumTypeMapping {
6    fn map_to_database_value<'b>(
7        output: &mut crate::serialize::Output<'b, '_, crate::pg::Pg>,
8        variant: &'static EnumVariant,
9    ) -> crate::serialize::Result {
10        output.write_all(variant.sql_name.as_bytes())?;
11        Ok(crate::serialize::IsNull::No)
12    }
13
14    fn map_from_database_value(
15        raw: PgValue<'_>,
16        type_name: &'static str,
17        variants: &'static [EnumVariant],
18    ) -> crate::deserialize::Result<usize> {
19        let name = raw.as_bytes();
20        variants
21            .iter()
22            .position(|v| v.sql_name.as_bytes() == name)
23            .ok_or_else(|| {
24                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("Invalid enum variant `{0}` for `{2}`. Allowed variants are {1}",
                String::from_utf8_lossy(name),
                variants.iter().map(|v|
                                ::alloc::__export::must_use({
                                        ::alloc::fmt::format(format_args!("`{0}`", v.sql_name))
                                    })).collect::<Vec<_>>().join(", "), type_name))
    })format!(
25                    "Invalid enum variant `{}` for `{type_name}`. Allowed variants are {}",
26                    String::from_utf8_lossy(name),
27                    variants
28                        .iter()
29                        .map(|v| format!("`{}`", v.sql_name))
30                        .collect::<Vec<_>>()
31                        .join(", ")
32                )
33                .into()
34            })
35    }
36}