Deduplicate StoredUuid wrapper
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
use crate::server::{
|
use crate::server::{
|
||||||
AddVersionResult, GetVersionResult, HistorySegment, Server, VersionId, NO_VERSION_ID,
|
AddVersionResult, GetVersionResult, HistorySegment, Server, VersionId, NO_VERSION_ID,
|
||||||
};
|
};
|
||||||
|
use crate::storage::sqlite::StoredUuid;
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use rusqlite::params;
|
use rusqlite::params;
|
||||||
use rusqlite::types::{FromSql, ToSql};
|
use rusqlite::types::{FromSql, ToSql};
|
||||||
@@ -9,35 +10,6 @@ use serde::{Deserialize, Serialize};
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
// FIXME: Duplicated
|
|
||||||
/// Newtype to allow implementing `FromSql` for foreign `uuid::Uuid`
|
|
||||||
pub struct StoredUuid(Uuid);
|
|
||||||
|
|
||||||
/// Conversion from Uuid stored as a string (rusqlite's uuid feature stores as binary blob)
|
|
||||||
impl FromSql for StoredUuid {
|
|
||||||
fn column_result(value: rusqlite::types::ValueRef<'_>) -> rusqlite::types::FromSqlResult<Self> {
|
|
||||||
let u = Uuid::parse_str(value.as_str()?)
|
|
||||||
.map_err(|_| rusqlite::types::FromSqlError::InvalidType)?;
|
|
||||||
Ok(StoredUuid(u))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Store Uuid as string in database
|
|
||||||
impl ToSql for StoredUuid {
|
|
||||||
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
|
|
||||||
let s = self.0.to_string();
|
|
||||||
Ok(s.into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromSql for Version {
|
|
||||||
fn column_result(value: rusqlite::types::ValueRef<'_>) -> rusqlite::types::FromSqlResult<Self> {
|
|
||||||
let u = serde_json::from_str(value.as_str()?)
|
|
||||||
.map_err(|_| rusqlite::types::FromSqlError::InvalidType)?;
|
|
||||||
Ok(u)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
struct Version {
|
struct Version {
|
||||||
version_id: VersionId,
|
version_id: VersionId,
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ use uuid::Uuid;
|
|||||||
mod config;
|
mod config;
|
||||||
mod inmemory;
|
mod inmemory;
|
||||||
mod operation;
|
mod operation;
|
||||||
mod sqlite;
|
pub(crate) mod sqlite;
|
||||||
|
|
||||||
pub use config::StorageConfig;
|
pub use config::StorageConfig;
|
||||||
pub use inmemory::InMemoryStorage;
|
pub use inmemory::InMemoryStorage;
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ enum SqliteError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Newtype to allow implementing `FromSql` for foreign `uuid::Uuid`
|
/// Newtype to allow implementing `FromSql` for foreign `uuid::Uuid`
|
||||||
struct StoredUuid(Uuid);
|
pub(crate) struct StoredUuid(pub(crate) Uuid);
|
||||||
|
|
||||||
/// Conversion from Uuid stored as a string (rusqlite's uuid feature stores as binary blob)
|
/// Conversion from Uuid stored as a string (rusqlite's uuid feature stores as binary blob)
|
||||||
impl FromSql for StoredUuid {
|
impl FromSql for StoredUuid {
|
||||||
|
|||||||
Reference in New Issue
Block a user