mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 12:52:27 +00:00
store, store_sqlite: Refactor error typing
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -2646,7 +2646,6 @@ dependencies = [
|
|||||||
name = "rustical_store_sqlite"
|
name = "rustical_store_sqlite"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"rustical_store",
|
"rustical_store",
|
||||||
"serde",
|
"serde",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
error::Error,
|
|
||||||
model::{AddressObject, Addressbook},
|
model::{AddressObject, Addressbook},
|
||||||
|
Error,
|
||||||
};
|
};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ pub enum Error {
|
|||||||
InvalidData(String),
|
InvalidData(String),
|
||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Other(#[from] anyhow::Error),
|
ParserError(#[from] ical::parser::ParserError),
|
||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
ParserError(#[from] ical::parser::ParserError),
|
Other(#[from] anyhow::Error),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -130,9 +130,9 @@ impl CalDateTime {
|
|||||||
return Ok(CalDateTime::OlsonTZ(datetime));
|
return Ok(CalDateTime::OlsonTZ(datetime));
|
||||||
} else {
|
} else {
|
||||||
// This time does not exist because there's a gap in local time
|
// This time does not exist because there's a gap in local time
|
||||||
return Err(Error::Other(anyhow!(
|
return Err(Error::InvalidData(
|
||||||
"Timestamp doesn't exist because of gap in local time"
|
"Timestamp doesn't exist because of gap in local time".to_owned(),
|
||||||
)));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Ok(CalDateTime::Local(datetime));
|
return Ok(CalDateTime::Local(datetime));
|
||||||
@@ -145,7 +145,7 @@ impl CalDateTime {
|
|||||||
return Ok(CalDateTime::Date(date));
|
return Ok(CalDateTime::Date(date));
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(Error::Other(anyhow!("Invalid datetime format")))
|
Err(Error::InvalidData("Invalid datetime format".to_owned()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn utc(&self) -> DateTime<Utc> {
|
pub fn utc(&self) -> DateTime<Utc> {
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ publish = false
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rustical_store = { workspace = true }
|
rustical_store = { workspace = true }
|
||||||
anyhow = { workspace = true }
|
|
||||||
async-trait = { workspace = true }
|
async-trait = { workspace = true }
|
||||||
serde = { workspace = true }
|
serde = { workspace = true }
|
||||||
sqlx = { workspace = true }
|
sqlx = { workspace = true }
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
use super::{ChangeOperation, SqliteStore};
|
use super::{ChangeOperation, SqliteStore};
|
||||||
use crate::Error;
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use rustical_store::{
|
use rustical_store::{
|
||||||
model::{AddressObject, Addressbook},
|
model::{AddressObject, Addressbook},
|
||||||
@@ -15,9 +14,9 @@ struct AddressObjectRow {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<AddressObjectRow> for AddressObject {
|
impl TryFrom<AddressObjectRow> for AddressObject {
|
||||||
type Error = Error;
|
type Error = crate::Error;
|
||||||
|
|
||||||
fn try_from(value: AddressObjectRow) -> Result<Self, Error> {
|
fn try_from(value: AddressObjectRow) -> Result<Self, Self::Error> {
|
||||||
Ok(Self::from_vcf(value.id, value.vcf)?)
|
Ok(Self::from_vcf(value.id, value.vcf)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,7 +28,7 @@ async fn log_object_operation(
|
|||||||
addressbook_id: &str,
|
addressbook_id: &str,
|
||||||
object_id: &str,
|
object_id: &str,
|
||||||
operation: ChangeOperation,
|
operation: ChangeOperation,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), sqlx::Error> {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r#"
|
r#"
|
||||||
UPDATE addressbooks
|
UPDATE addressbooks
|
||||||
@@ -75,7 +74,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
)
|
)
|
||||||
.fetch_one(&self.db)
|
.fetch_one(&self.db)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?;
|
.map_err(crate::Error::from)?;
|
||||||
Ok(addressbook)
|
Ok(addressbook)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,7 +92,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
)
|
)
|
||||||
.fetch_all(&self.db)
|
.fetch_all(&self.db)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?;
|
.map_err(crate::Error::from)?;
|
||||||
Ok(addressbooks)
|
Ok(addressbooks)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,7 +115,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
)
|
)
|
||||||
.execute(&self.db)
|
.execute(&self.db)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?;
|
.map_err(crate::Error::from)?;
|
||||||
if result.rows_affected() == 0 {
|
if result.rows_affected() == 0 {
|
||||||
return Err(rustical_store::Error::NotFound);
|
return Err(rustical_store::Error::NotFound);
|
||||||
}
|
}
|
||||||
@@ -138,7 +137,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
)
|
)
|
||||||
.execute(&self.db)
|
.execute(&self.db)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?;
|
.map_err(crate::Error::from)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +155,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
principal, addressbook_id
|
principal, addressbook_id
|
||||||
)
|
)
|
||||||
.execute(&self.db)
|
.execute(&self.db)
|
||||||
.await.map_err(Error::SqlxError)?;
|
.await.map_err(crate::Error::from)?;
|
||||||
}
|
}
|
||||||
false => {
|
false => {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
@@ -166,7 +165,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
)
|
)
|
||||||
.execute(&self.db)
|
.execute(&self.db)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?;
|
.map_err(crate::Error::from)?;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -185,7 +184,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
)
|
)
|
||||||
.execute(&self.db)
|
.execute(&self.db)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?;
|
.map_err(crate::Error::from)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,7 +209,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
synctoken
|
synctoken
|
||||||
)
|
)
|
||||||
.fetch_all(&self.db)
|
.fetch_all(&self.db)
|
||||||
.await.map_err(Error::SqlxError)?;
|
.await.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
let mut objects = vec![];
|
let mut objects = vec![];
|
||||||
let mut deleted_objects = vec![];
|
let mut deleted_objects = vec![];
|
||||||
@@ -244,7 +243,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
addressbook_id
|
addressbook_id
|
||||||
)
|
)
|
||||||
.fetch_all(&self.db)
|
.fetch_all(&self.db)
|
||||||
.await.map_err(Error::SqlxError)?
|
.await.map_err(crate::Error::from)?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|row| row.try_into().map_err(rustical_store::Error::from))
|
.map(|row| row.try_into().map_err(rustical_store::Error::from))
|
||||||
.collect()
|
.collect()
|
||||||
@@ -266,7 +265,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
)
|
)
|
||||||
.fetch_one(&self.db)
|
.fetch_one(&self.db)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?
|
.map_err(crate::Error::from)?
|
||||||
.try_into()?)
|
.try_into()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -279,7 +278,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
// TODO: implement
|
// TODO: implement
|
||||||
overwrite: bool,
|
overwrite: bool,
|
||||||
) -> Result<(), rustical_store::Error> {
|
) -> Result<(), rustical_store::Error> {
|
||||||
let mut tx = self.db.begin().await.map_err(Error::SqlxError)?;
|
let mut tx = self.db.begin().await.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
let (object_id, vcf) = (object.get_id(), object.get_vcf());
|
let (object_id, vcf) = (object.get_id(), object.get_vcf());
|
||||||
|
|
||||||
@@ -292,7 +291,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
)
|
)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?;
|
.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
log_object_operation(
|
log_object_operation(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
@@ -302,9 +301,9 @@ impl AddressbookStore for SqliteStore {
|
|||||||
ChangeOperation::Add,
|
ChangeOperation::Add,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(rustical_store::Error::from)?;
|
.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
tx.commit().await.map_err(Error::SqlxError)?;
|
tx.commit().await.map_err(crate::Error::from)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,7 +315,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
object_id: &str,
|
object_id: &str,
|
||||||
use_trashbin: bool,
|
use_trashbin: bool,
|
||||||
) -> Result<(), rustical_store::Error> {
|
) -> Result<(), rustical_store::Error> {
|
||||||
let mut tx = self.db.begin().await.map_err(Error::SqlxError)?;
|
let mut tx = self.db.begin().await.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
match use_trashbin {
|
match use_trashbin {
|
||||||
true => {
|
true => {
|
||||||
@@ -327,7 +326,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
object_id
|
object_id
|
||||||
)
|
)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await.map_err(Error::SqlxError)?;
|
.await.map_err(crate::Error::from)?;
|
||||||
}
|
}
|
||||||
false => {
|
false => {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
@@ -337,7 +336,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
)
|
)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?;
|
.map_err(crate::Error::from)?;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
log_object_operation(
|
log_object_operation(
|
||||||
@@ -348,8 +347,8 @@ impl AddressbookStore for SqliteStore {
|
|||||||
ChangeOperation::Delete,
|
ChangeOperation::Delete,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(rustical_store::Error::from)?;
|
.map_err(crate::Error::from)?;
|
||||||
tx.commit().await.map_err(Error::SqlxError)?;
|
tx.commit().await.map_err(crate::Error::from)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -360,7 +359,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
addressbook_id: &str,
|
addressbook_id: &str,
|
||||||
object_id: &str,
|
object_id: &str,
|
||||||
) -> Result<(), rustical_store::Error> {
|
) -> Result<(), rustical_store::Error> {
|
||||||
let mut tx = self.db.begin().await.map_err(Error::SqlxError)?;
|
let mut tx = self.db.begin().await.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r#"UPDATE addressobjects SET deleted_at = NULL, updated_at = datetime() WHERE (principal, addressbook_id, id) = (?, ?, ?)"#,
|
r#"UPDATE addressobjects SET deleted_at = NULL, updated_at = datetime() WHERE (principal, addressbook_id, id) = (?, ?, ?)"#,
|
||||||
@@ -369,7 +368,7 @@ impl AddressbookStore for SqliteStore {
|
|||||||
object_id
|
object_id
|
||||||
)
|
)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await.map_err(Error::SqlxError)?;
|
.await.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
log_object_operation(
|
log_object_operation(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
@@ -379,8 +378,8 @@ impl AddressbookStore for SqliteStore {
|
|||||||
ChangeOperation::Add,
|
ChangeOperation::Add,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(rustical_store::Error::from)?;
|
.map_err(crate::Error::from)?;
|
||||||
tx.commit().await.map_err(Error::SqlxError)?;
|
tx.commit().await.map_err(crate::Error::from)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
use super::{ChangeOperation, SqliteStore};
|
use super::{ChangeOperation, SqliteStore};
|
||||||
use crate::Error;
|
|
||||||
use anyhow::Result;
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use rustical_store::model::object::CalendarObject;
|
use rustical_store::model::object::CalendarObject;
|
||||||
use rustical_store::model::Calendar;
|
use rustical_store::model::Calendar;
|
||||||
use rustical_store::CalendarStore;
|
use rustical_store::CalendarStore;
|
||||||
|
use rustical_store::Error;
|
||||||
use sqlx::Sqlite;
|
use sqlx::Sqlite;
|
||||||
use sqlx::Transaction;
|
use sqlx::Transaction;
|
||||||
use tracing::instrument;
|
use tracing::instrument;
|
||||||
@@ -30,7 +29,7 @@ async fn log_object_operation(
|
|||||||
cal_id: &str,
|
cal_id: &str,
|
||||||
object_id: &str,
|
object_id: &str,
|
||||||
operation: ChangeOperation,
|
operation: ChangeOperation,
|
||||||
) -> Result<(), rustical_store::Error> {
|
) -> Result<(), Error> {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r#"
|
r#"
|
||||||
UPDATE calendars
|
UPDATE calendars
|
||||||
@@ -41,7 +40,7 @@ async fn log_object_operation(
|
|||||||
)
|
)
|
||||||
.execute(&mut **tx)
|
.execute(&mut **tx)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?;
|
.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r#"
|
r#"
|
||||||
@@ -56,18 +55,14 @@ async fn log_object_operation(
|
|||||||
)
|
)
|
||||||
.execute(&mut **tx)
|
.execute(&mut **tx)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?;
|
.map_err(crate::Error::from)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl CalendarStore for SqliteStore {
|
impl CalendarStore for SqliteStore {
|
||||||
#[instrument]
|
#[instrument]
|
||||||
async fn get_calendar(
|
async fn get_calendar(&self, principal: &str, id: &str) -> Result<Calendar, Error> {
|
||||||
&self,
|
|
||||||
principal: &str,
|
|
||||||
id: &str,
|
|
||||||
) -> Result<Calendar, rustical_store::Error> {
|
|
||||||
let cal = sqlx::query_as!(
|
let cal = sqlx::query_as!(
|
||||||
Calendar,
|
Calendar,
|
||||||
r#"SELECT principal, id, synctoken, "order", displayname, description, color, timezone, deleted_at
|
r#"SELECT principal, id, synctoken, "order", displayname, description, color, timezone, deleted_at
|
||||||
@@ -77,12 +72,12 @@ impl CalendarStore for SqliteStore {
|
|||||||
id
|
id
|
||||||
)
|
)
|
||||||
.fetch_one(&self.db)
|
.fetch_one(&self.db)
|
||||||
.await.map_err(Error::SqlxError)?;
|
.await.map_err(crate::Error::from)?;
|
||||||
Ok(cal)
|
Ok(cal)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
async fn get_calendars(&self, principal: &str) -> Result<Vec<Calendar>, rustical_store::Error> {
|
async fn get_calendars(&self, principal: &str) -> Result<Vec<Calendar>, Error> {
|
||||||
let cals = sqlx::query_as!(
|
let cals = sqlx::query_as!(
|
||||||
Calendar,
|
Calendar,
|
||||||
r#"SELECT principal, id, synctoken, displayname, "order", description, color, timezone, deleted_at
|
r#"SELECT principal, id, synctoken, displayname, "order", description, color, timezone, deleted_at
|
||||||
@@ -91,12 +86,12 @@ impl CalendarStore for SqliteStore {
|
|||||||
principal
|
principal
|
||||||
)
|
)
|
||||||
.fetch_all(&self.db)
|
.fetch_all(&self.db)
|
||||||
.await.map_err(Error::SqlxError)?;
|
.await.map_err(crate::Error::from)?;
|
||||||
Ok(cals)
|
Ok(cals)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
async fn insert_calendar(&self, calendar: Calendar) -> Result<(), rustical_store::Error> {
|
async fn insert_calendar(&self, calendar: Calendar) -> Result<(), Error> {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r#"INSERT INTO calendars (principal, id, displayname, description, "order", color, timezone)
|
r#"INSERT INTO calendars (principal, id, displayname, description, "order", color, timezone)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?)"#,
|
VALUES (?, ?, ?, ?, ?, ?, ?)"#,
|
||||||
@@ -109,7 +104,7 @@ impl CalendarStore for SqliteStore {
|
|||||||
calendar.timezone
|
calendar.timezone
|
||||||
)
|
)
|
||||||
.execute(&self.db)
|
.execute(&self.db)
|
||||||
.await.map_err(Error::SqlxError)?;
|
.await.map_err(crate::Error::from)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,7 +114,7 @@ impl CalendarStore for SqliteStore {
|
|||||||
principal: String,
|
principal: String,
|
||||||
id: String,
|
id: String,
|
||||||
calendar: Calendar,
|
calendar: Calendar,
|
||||||
) -> Result<(), rustical_store::Error> {
|
) -> Result<(), Error> {
|
||||||
let result = sqlx::query!(
|
let result = sqlx::query!(
|
||||||
r#"UPDATE calendars SET principal = ?, id = ?, displayname = ?, description = ?, "order" = ?, color = ?, timezone = ?
|
r#"UPDATE calendars SET principal = ?, id = ?, displayname = ?, description = ?, "order" = ?, color = ?, timezone = ?
|
||||||
WHERE (principal, id) = (?, ?)"#,
|
WHERE (principal, id) = (?, ?)"#,
|
||||||
@@ -132,7 +127,7 @@ impl CalendarStore for SqliteStore {
|
|||||||
calendar.timezone,
|
calendar.timezone,
|
||||||
principal,
|
principal,
|
||||||
id
|
id
|
||||||
).execute(&self.db).await.map_err(Error::SqlxError)?;
|
).execute(&self.db).await.map_err(crate::Error::from)?;
|
||||||
if result.rows_affected() == 0 {
|
if result.rows_affected() == 0 {
|
||||||
return Err(rustical_store::Error::NotFound);
|
return Err(rustical_store::Error::NotFound);
|
||||||
}
|
}
|
||||||
@@ -146,7 +141,7 @@ impl CalendarStore for SqliteStore {
|
|||||||
principal: &str,
|
principal: &str,
|
||||||
id: &str,
|
id: &str,
|
||||||
use_trashbin: bool,
|
use_trashbin: bool,
|
||||||
) -> Result<(), rustical_store::Error> {
|
) -> Result<(), Error> {
|
||||||
match use_trashbin {
|
match use_trashbin {
|
||||||
true => {
|
true => {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
@@ -154,7 +149,7 @@ impl CalendarStore for SqliteStore {
|
|||||||
principal, id
|
principal, id
|
||||||
)
|
)
|
||||||
.execute(&self.db)
|
.execute(&self.db)
|
||||||
.await.map_err(Error::SqlxError)?;
|
.await.map_err(crate::Error::from)?;
|
||||||
}
|
}
|
||||||
false => {
|
false => {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
@@ -164,18 +159,14 @@ impl CalendarStore for SqliteStore {
|
|||||||
)
|
)
|
||||||
.execute(&self.db)
|
.execute(&self.db)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?;
|
.map_err(crate::Error::from)?;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
async fn restore_calendar(
|
async fn restore_calendar(&self, principal: &str, id: &str) -> Result<(), Error> {
|
||||||
&self,
|
|
||||||
principal: &str,
|
|
||||||
id: &str,
|
|
||||||
) -> Result<(), rustical_store::Error> {
|
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r"UPDATE calendars SET deleted_at = NULL WHERE (principal, id) = (?, ?)",
|
r"UPDATE calendars SET deleted_at = NULL WHERE (principal, id) = (?, ?)",
|
||||||
principal,
|
principal,
|
||||||
@@ -183,7 +174,7 @@ impl CalendarStore for SqliteStore {
|
|||||||
)
|
)
|
||||||
.execute(&self.db)
|
.execute(&self.db)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?;
|
.map_err(crate::Error::from)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,7 +183,7 @@ impl CalendarStore for SqliteStore {
|
|||||||
&self,
|
&self,
|
||||||
principal: &str,
|
principal: &str,
|
||||||
cal_id: &str,
|
cal_id: &str,
|
||||||
) -> Result<Vec<CalendarObject>, rustical_store::Error> {
|
) -> Result<Vec<CalendarObject>, Error> {
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
CalendarObjectRow,
|
CalendarObjectRow,
|
||||||
"SELECT id, ics FROM calendarobjects WHERE principal = ? AND cal_id = ? AND deleted_at IS NULL",
|
"SELECT id, ics FROM calendarobjects WHERE principal = ? AND cal_id = ? AND deleted_at IS NULL",
|
||||||
@@ -200,7 +191,7 @@ impl CalendarStore for SqliteStore {
|
|||||||
cal_id
|
cal_id
|
||||||
)
|
)
|
||||||
.fetch_all(&self.db)
|
.fetch_all(&self.db)
|
||||||
.await.map_err(Error::SqlxError)?
|
.await.map_err(crate::Error::from)?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|row| row.try_into().map_err(rustical_store::Error::from))
|
.map(|row| row.try_into().map_err(rustical_store::Error::from))
|
||||||
.collect()
|
.collect()
|
||||||
@@ -212,7 +203,7 @@ impl CalendarStore for SqliteStore {
|
|||||||
principal: &str,
|
principal: &str,
|
||||||
cal_id: &str,
|
cal_id: &str,
|
||||||
object_id: &str,
|
object_id: &str,
|
||||||
) -> Result<CalendarObject, rustical_store::Error> {
|
) -> Result<CalendarObject, Error> {
|
||||||
Ok(sqlx::query_as!(
|
Ok(sqlx::query_as!(
|
||||||
CalendarObjectRow,
|
CalendarObjectRow,
|
||||||
"SELECT id, ics FROM calendarobjects WHERE (principal, cal_id, id) = (?, ?, ?)",
|
"SELECT id, ics FROM calendarobjects WHERE (principal, cal_id, id) = (?, ?, ?)",
|
||||||
@@ -222,7 +213,7 @@ impl CalendarStore for SqliteStore {
|
|||||||
)
|
)
|
||||||
.fetch_one(&self.db)
|
.fetch_one(&self.db)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?
|
.map_err(crate::Error::from)?
|
||||||
.try_into()?)
|
.try_into()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,8 +225,8 @@ impl CalendarStore for SqliteStore {
|
|||||||
object: CalendarObject,
|
object: CalendarObject,
|
||||||
// TODO: implement
|
// TODO: implement
|
||||||
overwrite: bool,
|
overwrite: bool,
|
||||||
) -> Result<(), rustical_store::Error> {
|
) -> Result<(), Error> {
|
||||||
let mut tx = self.db.begin().await.map_err(Error::SqlxError)?;
|
let mut tx = self.db.begin().await.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
let (object_id, ics) = (object.get_id(), object.get_ics());
|
let (object_id, ics) = (object.get_id(), object.get_ics());
|
||||||
|
|
||||||
@@ -248,7 +239,7 @@ impl CalendarStore for SqliteStore {
|
|||||||
)
|
)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?;
|
.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
log_object_operation(
|
log_object_operation(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
@@ -259,7 +250,7 @@ impl CalendarStore for SqliteStore {
|
|||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
tx.commit().await.map_err(Error::SqlxError)?;
|
tx.commit().await.map_err(crate::Error::from)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,8 +261,8 @@ impl CalendarStore for SqliteStore {
|
|||||||
cal_id: &str,
|
cal_id: &str,
|
||||||
id: &str,
|
id: &str,
|
||||||
use_trashbin: bool,
|
use_trashbin: bool,
|
||||||
) -> Result<(), rustical_store::Error> {
|
) -> Result<(), Error> {
|
||||||
let mut tx = self.db.begin().await.map_err(Error::SqlxError)?;
|
let mut tx = self.db.begin().await.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
match use_trashbin {
|
match use_trashbin {
|
||||||
true => {
|
true => {
|
||||||
@@ -282,7 +273,7 @@ impl CalendarStore for SqliteStore {
|
|||||||
id
|
id
|
||||||
)
|
)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await.map_err(Error::SqlxError)?;
|
.await.map_err(crate::Error::from)?;
|
||||||
}
|
}
|
||||||
false => {
|
false => {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
@@ -292,11 +283,11 @@ impl CalendarStore for SqliteStore {
|
|||||||
)
|
)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::SqlxError)?;
|
.map_err(crate::Error::from)?;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
log_object_operation(&mut tx, principal, cal_id, id, ChangeOperation::Delete).await?;
|
log_object_operation(&mut tx, principal, cal_id, id, ChangeOperation::Delete).await?;
|
||||||
tx.commit().await.map_err(Error::SqlxError)?;
|
tx.commit().await.map_err(crate::Error::from)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -306,8 +297,8 @@ impl CalendarStore for SqliteStore {
|
|||||||
principal: &str,
|
principal: &str,
|
||||||
cal_id: &str,
|
cal_id: &str,
|
||||||
object_id: &str,
|
object_id: &str,
|
||||||
) -> Result<(), rustical_store::Error> {
|
) -> Result<(), Error> {
|
||||||
let mut tx = self.db.begin().await.map_err(Error::SqlxError)?;
|
let mut tx = self.db.begin().await.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r#"UPDATE calendarobjects SET deleted_at = NULL, updated_at = datetime() WHERE (principal, cal_id, id) = (?, ?, ?)"#,
|
r#"UPDATE calendarobjects SET deleted_at = NULL, updated_at = datetime() WHERE (principal, cal_id, id) = (?, ?, ?)"#,
|
||||||
@@ -316,10 +307,10 @@ impl CalendarStore for SqliteStore {
|
|||||||
object_id
|
object_id
|
||||||
)
|
)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await.map_err(Error::SqlxError)?;
|
.await.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
log_object_operation(&mut tx, principal, cal_id, object_id, ChangeOperation::Add).await?;
|
log_object_operation(&mut tx, principal, cal_id, object_id, ChangeOperation::Add).await?;
|
||||||
tx.commit().await.map_err(Error::SqlxError)?;
|
tx.commit().await.map_err(crate::Error::from)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -329,7 +320,7 @@ impl CalendarStore for SqliteStore {
|
|||||||
principal: &str,
|
principal: &str,
|
||||||
cal_id: &str,
|
cal_id: &str,
|
||||||
synctoken: i64,
|
synctoken: i64,
|
||||||
) -> Result<(Vec<CalendarObject>, Vec<String>, i64), rustical_store::Error> {
|
) -> Result<(Vec<CalendarObject>, Vec<String>, i64), Error> {
|
||||||
struct Row {
|
struct Row {
|
||||||
object_id: String,
|
object_id: String,
|
||||||
synctoken: i64,
|
synctoken: i64,
|
||||||
@@ -344,7 +335,7 @@ impl CalendarStore for SqliteStore {
|
|||||||
synctoken
|
synctoken
|
||||||
)
|
)
|
||||||
.fetch_all(&self.db)
|
.fetch_all(&self.db)
|
||||||
.await.map_err(Error::SqlxError)?;
|
.await.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
let mut objects = vec![];
|
let mut objects = vec![];
|
||||||
let mut deleted_objects = vec![];
|
let mut deleted_objects = vec![];
|
||||||
|
|||||||
@@ -1,51 +1,32 @@
|
|||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error("Not found")]
|
|
||||||
NotFound,
|
|
||||||
|
|
||||||
#[error("Resource already exists and overwrite=false")]
|
|
||||||
AlreadyExists,
|
|
||||||
|
|
||||||
#[error("Invalid ics/vcf input: {0}")]
|
|
||||||
InvalidData(String),
|
|
||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
SqlxError(sqlx::Error),
|
SqlxError(sqlx::Error),
|
||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Other(#[from] anyhow::Error),
|
StoreError(rustical_store::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<sqlx::Error> for Error {
|
impl From<sqlx::Error> for Error {
|
||||||
fn from(value: sqlx::Error) -> Self {
|
fn from(value: sqlx::Error) -> Self {
|
||||||
match value {
|
match value {
|
||||||
sqlx::Error::RowNotFound => Error::NotFound,
|
sqlx::Error::RowNotFound => Error::StoreError(rustical_store::Error::NotFound),
|
||||||
err => Error::SqlxError(err),
|
err => Error::SqlxError(err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: clean up error types
|
|
||||||
impl From<Error> for rustical_store::Error {
|
impl From<Error> for rustical_store::Error {
|
||||||
fn from(value: Error) -> Self {
|
fn from(value: Error) -> Self {
|
||||||
match value {
|
match value {
|
||||||
Error::NotFound => Self::NotFound,
|
|
||||||
Error::AlreadyExists => Self::AlreadyExists,
|
|
||||||
Error::InvalidData(b) => Self::InvalidData(b),
|
|
||||||
Error::SqlxError(err) => Self::Other(err.into()),
|
Error::SqlxError(err) => Self::Other(err.into()),
|
||||||
Error::Other(err) => Self::Other(err),
|
Error::StoreError(err) => err,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<rustical_store::Error> for Error {
|
impl From<rustical_store::Error> for Error {
|
||||||
fn from(value: rustical_store::Error) -> Self {
|
fn from(value: rustical_store::Error) -> Self {
|
||||||
match value {
|
Self::StoreError(value)
|
||||||
rustical_store::Error::NotFound => Self::NotFound,
|
|
||||||
rustical_store::Error::AlreadyExists => Self::AlreadyExists,
|
|
||||||
rustical_store::Error::InvalidData(b) => Self::InvalidData(b),
|
|
||||||
rustical_store::Error::Other(err) => Self::Other(err),
|
|
||||||
rustical_store::Error::ParserError(err) => Self::Other(err.into()),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ impl SqliteStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_db_pool(db_url: &str, migrate: bool) -> anyhow::Result<Pool<Sqlite>> {
|
pub async fn create_db_pool(db_url: &str, migrate: bool) -> Result<Pool<Sqlite>, sqlx::Error> {
|
||||||
let db = SqlitePool::connect_with(
|
let db = SqlitePool::connect_with(
|
||||||
SqliteConnectOptions::new()
|
SqliteConnectOptions::new()
|
||||||
.filename(db_url)
|
.filename(db_url)
|
||||||
@@ -40,7 +40,7 @@ pub async fn create_db_pool(db_url: &str, migrate: bool) -> anyhow::Result<Pool<
|
|||||||
Ok(db)
|
Ok(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_test_store() -> anyhow::Result<SqliteStore> {
|
pub async fn create_test_store() -> Result<SqliteStore, sqlx::Error> {
|
||||||
let db = SqlitePool::connect("sqlite::memory:").await?;
|
let db = SqlitePool::connect("sqlite::memory:").await?;
|
||||||
sqlx::migrate!("./migrations").run(&db).await?;
|
sqlx::migrate!("./migrations").run(&db).await?;
|
||||||
Ok(SqliteStore::new(db))
|
Ok(SqliteStore::new(db))
|
||||||
|
|||||||
Reference in New Issue
Block a user