Skip to content

How do I use custom and enumeration functions to set the type of Column #597

Answered by khassar00
khassar00 asked this question in Q&A
Discussion options

You must be logged in to vote

I don't need to use either of these functions at all to handle enum correctly.

#[enum_def]
#[derive(Debug,sqlx::FromRow)]
pub struct TaskInfo  {
    pub id: i32,
    #[sqlx(flatten)]
    pub state: TaskState,

}
#[derive(Debug, Copy,Clone,Deserialize,Serialize)]
pub enum TaskState {
    Sleep,
    Running,
    Pause,
    Complete,
    Error,
    Cancel,
}

impl From<TaskState> for sea_query::Value {
    fn from(value: TaskState) -> Self {
        sea_query::Value::Int(Some(value.into()))
    }
}

impl FromRow<'_, SqliteRow> for TaskState {
    fn from_row(row: &SqliteRow) -> sqlx::Result<Self> {
        match row.try_get::<i32, &str>("state") {
            Ok(value) =>Ok(value.into()),
  …

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@khassar00
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by khassar00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants