use serde::{Deserialize, Serialize};
use crate::error::SkybaseError;
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateRecordRequest<'a, T: Serialize> {
pub repo: &'a str,
pub collection: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
pub rkey: Option<&'a str>,
pub validate: bool,
pub record: &'a T,
#[serde(skip_serializing_if = "Option::is_none")]
pub swap_commit: Option<&'a str>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CreateRecordResult {
pub uri: String,
pub cid: String,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PutRecordRequest<'a, T: Serialize> {
pub repo: &'a str,
pub collection: &'a str,
pub rkey: &'a str,
pub validate: bool,
pub record: &'a T,
#[serde(skip_serializing_if = "Option::is_none")]
pub swap_record: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub swap_commit: Option<&'a str>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PutRecordResult {
pub uri: String,
pub cid: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RecordView {
pub uri: String,
#[serde(default)]
pub cid: String,
pub value: serde_json::Value,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DeleteRecordRequest<'a> {
pub repo: &'a str,
pub collection: &'a str,
pub rkey: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
pub swap_record: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub swap_commit: Option<&'a str>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct XrpcErrorResponse {
#[serde(default)]
pub error: Option<String>,
#[serde(default)]
pub message: Option<String>,
}
#[must_use]
pub fn format_at_uri(did: &str, collection: &str, rkey: &str) -> String {
format!("at://{did}/{collection}/{rkey}")
}
#[must_use]
pub fn split_at_uri(uri: &str) -> Option<(&str, &str, &str)> {
let stripped = uri.strip_prefix("at://")?;
let mut parts = stripped.split('/');
let authority = parts.next()?;
let collection = parts.next()?;
let rkey = parts.next()?;
if parts.next().is_some() || authority.is_empty() || collection.is_empty() || rkey.is_empty() {
return None;
}
Some((authority, collection, rkey))
}
#[must_use]
pub fn extract_did_from_at_uri(uri: &str) -> Option<&str> {
let stripped = uri.strip_prefix("at://")?;
let did = stripped.split('/').next()?;
if did.starts_with("did:") {
Some(did)
} else {
None
}
}
#[must_use]
pub fn extract_did_for_collection<'a>(uri: &'a str, expected_collection: &str) -> Option<&'a str> {
let (did, collection, rkey) = split_at_uri(uri)?;
if !did.starts_with("did:") || collection != expected_collection || rkey.is_empty() {
return None;
}
Some(did)
}
#[must_use]
pub fn parse_at_post_uri(uri: &str) -> Option<(&str, &str)> {
let (author, collection, rkey) = split_at_uri(uri)?;
if author.is_empty() || collection != "app.bsky.feed.post" || rkey.is_empty() {
return None;
}
Some((author, rkey))
}
pub fn validate_rkey(rkey: &str) -> Result<(), SkybaseError> {
if rkey.is_empty() || rkey.len() > 512 || rkey == "." || rkey == ".." {
return Err(SkybaseError::Repo(format!(
"Invalid rkey '{rkey}': length must be 1..=512 and not '.' or '..'"
)));
}
for b in rkey.bytes() {
match b {
b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'.' | b'_' | b'~' | b'-' => {}
_ => {
return Err(SkybaseError::Repo(format!(
"Invalid rkey '{rkey}': illegal character '{}'",
b as char
)));
}
}
}
Ok(())
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, missing_docs)]
mod tests {
use super::*;
#[test]
fn test_format_at_uri() {
let uri = format_at_uri("did:plc:12345", "app.bsky.feed.post", "rkey_abc");
assert_eq!(uri, "at://did:plc:12345/app.bsky.feed.post/rkey_abc");
assert_eq!(
split_at_uri(&uri),
Some(("did:plc:12345", "app.bsky.feed.post", "rkey_abc"))
);
assert_eq!(extract_did_from_at_uri(&uri), Some("did:plc:12345"));
assert_eq!(
extract_did_for_collection(&uri, "app.bsky.feed.post"),
Some("did:plc:12345")
);
assert_eq!(
extract_did_for_collection(&uri, "app.bsky.graph.list"),
None
);
assert_eq!(parse_at_post_uri(&uri), Some(("did:plc:12345", "rkey_abc")));
}
#[test]
fn test_validate_rkey_valid() {
assert!(validate_rkey("valid-key").is_ok());
assert!(validate_rkey("3k2abc123").is_ok());
assert!(validate_rkey("user.profile_data~test-01").is_ok());
assert!(validate_rkey("a").is_ok());
let max_len_key = "a".repeat(512);
assert!(validate_rkey(&max_len_key).is_ok());
}
#[test]
fn test_validate_rkey_invalid() {
assert!(validate_rkey("").is_err());
assert!(validate_rkey(".").is_err());
assert!(validate_rkey("..").is_err());
assert!(validate_rkey("has space").is_err());
assert!(validate_rkey("slash/in/key").is_err());
assert!(validate_rkey("hash#tag").is_err());
assert!(validate_rkey("question?mark").is_err());
assert!(validate_rkey("null\0byte").is_err());
let too_long = "a".repeat(513);
assert!(validate_rkey(&too_long).is_err());
}
#[test]
fn test_create_record_request_serialization() {
let req = CreateRecordRequest {
repo: "did:plc:alice",
collection: "app.bsky.feed.post",
rkey: Some("post1"),
validate: true,
record: &serde_json::json!({ "text": "Hello" }),
swap_commit: None,
};
let json_val = serde_json::to_value(&req).expect("serialization failed");
assert_eq!(json_val["repo"], "did:plc:alice");
assert_eq!(json_val["collection"], "app.bsky.feed.post");
assert_eq!(json_val["rkey"], "post1");
assert_eq!(json_val["validate"], true);
assert_eq!(json_val["record"]["text"], "Hello");
assert!(json_val.get("swapCommit").is_none());
}
#[test]
fn test_delete_record_request_serialization() {
let req = DeleteRecordRequest {
repo: "did:plc:bob",
collection: "app.bsky.feed.like",
rkey: "like_123",
swap_record: Some("cid_record"),
swap_commit: None,
};
let json_val = serde_json::to_value(&req).expect("serialization failed");
assert_eq!(json_val["repo"], "did:plc:bob");
assert_eq!(json_val["collection"], "app.bsky.feed.like");
assert_eq!(json_val["rkey"], "like_123");
assert_eq!(json_val["swapRecord"], "cid_record");
assert!(json_val.get("swapCommit").is_none());
}
#[test]
fn test_xrpc_error_response_deserialization() {
let json_str = r#"{"error": "use_dpop_nonce", "message": "Proof requires nonce"}"#;
let parsed: XrpcErrorResponse =
serde_json::from_str(json_str).expect("deserialize xrpc error");
assert_eq!(parsed.error.as_deref(), Some("use_dpop_nonce"));
assert_eq!(parsed.message.as_deref(), Some("Proof requires nonce"));
let empty_json = "{}";
let empty_parsed: XrpcErrorResponse =
serde_json::from_str(empty_json).expect("deserialize empty xrpc error");
assert!(empty_parsed.error.is_none());
assert!(empty_parsed.message.is_none());
}
}