    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222

    use serde::{Deserialize,Serialize,Deserializer};
    //use sqlx::{sqlite::SqlitePool, Row, Sqlite};
    // use sqlx::{
    //     FromRow,
    //     decode::Decode,
    //     any::{AnyRow, AnyValueRef},
    //     database::{
    //         Database,
    //         HasValueRef,
    //     },
    //     Row,
    // //    Error,
    // };
    // use std::error::Error;
    // use std::str::FromStr;

    //use super::User;

    // #[derive(sqlx::FromRow)][derive(sqlx::Type)]
    //#[derive(sqlx::Type)]
    //#[sqlx(type_name = "userstatus_type")]
    // #[derive(sqlx::FromRow)]
    //#[sqlx(display_fromstr)]
    #[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
    pub enum UserStatus {
        Created,
        Active,
        Pending,
        Lock,
        Unknown,
    }
    impl UserStatus {
        #[allow(dead_code)]
        pub fn from_str(status: &str) -> Self {
            match status {
                "created"|"Created" => UserStatus::Created,
                "active"|"Active" => UserStatus::Active,
                "pending"|"Pending" => UserStatus::Pending,
                "lock"|"Lock" => UserStatus::Lock,
                _ => UserStatus::Unknown,
            }
        }
    }
    impl std::fmt::Display for UserStatus {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                UserStatus::Created => write!(f,"Created"),
                UserStatus::Active => write!(f,"Active"),
                UserStatus::Pending => write!(f,"Pending"),
                UserStatus::Lock => write!(f,"Lock"),
                UserStatus::Unknown => write!(f,"Unknown"),
            }
        }
    }

    impl Default for UserStatus {
        fn default() -> Self {
           UserStatus::Unknown
        }
    }
    #[allow(dead_code)]
    pub fn deserialize_user_status<'de, D>(deserializer: D) -> Result<UserStatus, D::Error>
    where D: Deserializer<'de> {
        let buf = String::deserialize(deserializer)?;
        Ok(UserStatus::from_str(&buf))
    }
    // impl sqlx::Type<sqlx::Any> for UserStatus {
    //     fn type_info() -> sqlx::any::AnyTypeInfo {
    //         //let res: sqlx::any::AnyTypeInfo = String::type_info();
    // let res = <str as sqlx::Type<sqlx::Any>>::type_info();
    //        //  let res = sqlx::any::AnyTypeInfo::default(); //  String::type_info();
    //         res
    //         // String::type_info()
    //     }

    //     // fn compatible(ty: &<sqlx::Any>::AnyTypeInfo) -> bool {
    //     //     *ty == Self::type_info()
    //     // }
    // }
    /*
    impl sqlx::Type<sqlx::Sqlite> for UserStatus {
        fn type_info() -> sqlx::sqlite::SqliteTypeInfo {
            let t = String::type_info();
            t
        }
    }
    impl sqlx::Type<sqlx::Mssql> for UserStatus {
        fn type_info() -> sqlx::mssql::MssqlTypeInfo {
            String::type_info()
        }
    }
    impl sqlx::Type<sqlx::MySql> for UserStatus {
        fn type_info() -> sqlx::mysql::MySqlTypeInfo {
            String::type_info()
        }
    }
    impl sqlx::Type<sqlx::Postgres> for UserStatus {
        fn type_info() -> sqlx::postgres::PgTypeInfo {
            String::type_info()
        }
    }

    impl<'q> sqlx::Encode<'q, sqlx::Any> for UserStatus {
        fn encode_by_ref(
            &self,
            args: &mut sqlx::any::AnyArgumentBuffer<'q>,
        ) -> sqlx::encode::IsNull {
            self.encode_by_ref(args)
        }
    }
    */

    /*
    impl<'r> sqlx::Decode<'r, sqlx::Any> for UserStatus {
        fn decode(
           //  value: <DB as HasValueRef<'r>>::AnyValueRef,
            value: sqlx::any::AnyValueRef<'r>,
        ) -> Result<Self, sqlx::error::BoxDynError> {
            //let string = value.     try_decode()?;
            //let string = <&str as Decode<AnyValueRef>>::decode(value)?;
            let v = value.to_owned();
            let string = value.parse()?;
            let value = UserStatus::from_str(&string);
            Ok(value)
        }
    }

    impl<'r, DB: Database> Decode<'r, DB> for UserStatus
    where
        // we want to delegate some of the work to string decoding so let's make sure strings
        // are supported by the database
        &'r str: Decode<'r, DB>
    {
        fn decode(
            value: <DB as HasValueRef<'r>>::ValueRef,
        ) -> Result<UserStatus, Box<dyn Error + 'static + Send + Sync>> {
            // the interface of ValueRef is largely unstable at the moment
            // so this is not directly implementable
            // however, you can delegate to a type that matches the format of the type you want
            // to decode (such as a UTF-8 string)
            let val = <&str as Decode<DB>>::decode(value)?;
            // now you can parse this into your type (assuming there is a `FromStr`)
            //Ok(value.parse()?)
            Ok(UserStatus::from_str(&val))
        }
    }
    */

    /*
    impl<'r> Decode<'r, sqlx::Any> for UserStatus {
        fn decode(
            value: sqlx::any::AnyValueRef<'r>,
        ) -> Result<Self, sqlx::error::BoxDynError> {
            //let mut decoder = <dyn sqlx::any::AnyDecode>::new(value)?;
            //let string = decoder.try_decode::<String>()?;
            //let username = decoder.try_decode::<String>()?;
            //let photo = decoder.try_decode::<Option<String>>()?;
            let string = <&str as Decode<sqlx::Any>>::decode(value)?;
            let value = UserStatus::from_str(&string);
            Ok(value)
        }
    }
    impl<'r> Decode<'r, sqlx::Sqlite> for UserStatus {
        fn decode(
            value: sqlx::sqlite::SqliteValueRef<'r>,
        ) -> Result<Self, sqlx::error::BoxDynError> {
            let string = <&str as Decode<sqlx::Sqlite>>::decode(value)?;
            let value = UserStatus::from_str(string);
            Ok(value)
        }
    }
    // impl<'r> Decode<'r, sqlx::Mssql> for UserStatus {
    //     fn decode(
    //         value: sqlx::mssql::MssqlValueRef<'r>,
    //     ) -> Result<UserStatus, Box<dyn Error + 'static + Send + Sync>> {
    //     //) -> Result<Self, sqlx::error::BoxDynError> {
    //         let string = <&str as Decode<sqlx::Mssql>>::decode(value)?;
    //         let value = UserStatus::from_str(string);
    //         Ok(value)
    //     }
    // }
    impl<'r> Decode<'r, sqlx::MySql> for UserStatus {
        fn decode(
            value: sqlx::mysql::MySqlValueRef<'r>,
        ) -> Result<Self, sqlx::error::BoxDynError> {
            let string = <&str as Decode<sqlx::MySql>>::decode(value)?;
            let value = UserStatus::from_str(string);
            Ok(value)
        }
    }
    impl<'r> Decode<'r, sqlx::Postgres> for UserStatus {
        fn decode(
            value: sqlx::postgres::PgValueRef<'r>,
        ) -> Result<Self, sqlx::error::BoxDynError> {
            //let mut decoder = sqlx::postgres::types::PgRecordDecoder::new(value)?;
            ////let username = decoder.try_decode::<String>()?;
            //let string = decoder.try_decode::<String>()?;
            let string = <&str as Decode<sqlx::Postgres>>::decode(value)?;
            let value = UserStatus::from_str(string);
            Ok(value)
        }
    }

    impl FromRow<'_, AnyRow> for UserStatus {
        fn from_row(row: &AnyRow) -> sqlx::Result<Self> {
            let v: String = row.try_get("status")?;
            Ok(
               UserStatus::from_str(&v)
            )
        }
    }
    impl FromStr for UserStatus {
        type Err = ();

        fn from_str(s: &str) -> Result<Self, Self::Err> {
            Ok(UserStatus::from_str(s))
        }
    }

    */