Skip to content

Commit

Permalink
Fix deserialisation too
Browse files Browse the repository at this point in the history
  • Loading branch information
neacsu committed Sep 12, 2024
1 parent d1f4446 commit 367137a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions common/gateway-storage/src/clients.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
// Copyright 2024 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: GPL-3.0-only

use sqlx::{error::BoxDynError, sqlite::SqliteValueRef, Decode, Sqlite};
use std::str::FromStr;

use crate::models::Client;

#[derive(Debug, PartialEq, sqlx::Type)]
#[sqlx(type_name = "TEXT")] // SQLite TEXT type
#[derive(Debug, PartialEq)]
pub enum ClientType {
EntryMixnet,
ExitMixnet,
EntryWireguard,
ExitWireguard,
}

impl<'r> Decode<'r, Sqlite> for ClientType {
fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
let text: &str = <&str as Decode<Sqlite>>::decode(value)?;
Ok(text
.parse::<ClientType>()
.map_err(|_| sqlx::Error::Decode(format!("Invalid client type: {}", text).into()))?)
}
}

impl FromStr for ClientType {
type Err = &'static str;

Expand Down

0 comments on commit 367137a

Please sign in to comment.