aboutsummaryrefslogtreecommitdiffstats
path: root/jid
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@blos.sm>2025-02-18 01:01:17 +0000
committerLibravatar cel 🌸 <cel@blos.sm>2025-02-18 01:01:17 +0000
commit68a7d136705133dc5d3a5d43b9ff4da28eeb6d5b (patch)
treee7ff07721156fa0d4110c2c88b0ad8ef86b54c92 /jid
parent0d9e3d27e9b81411b4d4c53e1b1a1c29087d66f3 (diff)
downloadluz-68a7d136705133dc5d3a5d43b9ff4da28eeb6d5b.tar.gz
luz-68a7d136705133dc5d3a5d43b9ff4da28eeb6d5b.tar.bz2
luz-68a7d136705133dc5d3a5d43b9ff4da28eeb6d5b.zip
database work
Diffstat (limited to 'jid')
-rw-r--r--jid/Cargo.toml1
-rw-r--r--jid/src/lib.rs46
2 files changed, 45 insertions, 2 deletions
diff --git a/jid/Cargo.toml b/jid/Cargo.toml
index 15049c9..0d817c9 100644
--- a/jid/Cargo.toml
+++ b/jid/Cargo.toml
@@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021"
[dependencies]
+sqlx = { version = "0.8.3", features = ["sqlite"] }
diff --git a/jid/src/lib.rs b/jid/src/lib.rs
index ff1d82b..5b648d9 100644
--- a/jid/src/lib.rs
+++ b/jid/src/lib.rs
@@ -1,6 +1,8 @@
-use std::str::FromStr;
+use std::{error::Error, fmt::Display, str::FromStr};
-#[derive(PartialEq, Debug, Clone)]
+use sqlx::Sqlite;
+
+#[derive(PartialEq, Debug, Clone, sqlx::Type, sqlx::Encode)]
pub struct JID {
// TODO: validate localpart (length, char]
pub localpart: Option<String>,
@@ -8,6 +10,33 @@ pub struct JID {
pub resourcepart: Option<String>,
}
+// TODO: feature gate
+impl sqlx::Type<Sqlite> for JID {
+ fn type_info() -> <Sqlite as sqlx::Database>::TypeInfo {
+ <&str as sqlx::Type<Sqlite>>::type_info()
+ }
+}
+
+impl sqlx::Decode<'_, Sqlite> for JID {
+ fn decode(
+ value: <Sqlite as sqlx::Database>::ValueRef<'_>,
+ ) -> Result<Self, sqlx::error::BoxDynError> {
+ let value = <&str as sqlx::Decode<Sqlite>>::decode(value)?;
+
+ Ok(value.parse()?)
+ }
+}
+
+impl sqlx::Encode<'_, Sqlite> for JID {
+ fn encode_by_ref(
+ &self,
+ buf: &mut <Sqlite as sqlx::Database>::ArgumentBuffer<'_>,
+ ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
+ let jid = self.to_string();
+ <String as sqlx::Encode<Sqlite>>::encode(jid, buf)
+ }
+}
+
pub enum JIDError {
NoResourcePart,
ParseError(ParseError),
@@ -19,6 +48,19 @@ pub enum ParseError {
Malformed(String),
}
+impl Display for ParseError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ ParseError::Empty => f.write_str("JID parse error: Empty"),
+ ParseError::Malformed(j) => {
+ f.write_str(format!("JID parse error: malformed; got '{}'", j).as_str())
+ }
+ }
+ }
+}
+
+impl Error for ParseError {}
+
impl JID {
pub fn new(
localpart: Option<String>,