geth/crates/geth-db/src/lib.rs

51 lines
1.4 KiB
Rust
Raw Normal View History

2026-05-15 15:08:20 +02:00
use geth_types::{DbId, ResourceId};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DbResource {
pub id: DbId,
pub resource: ResourceId,
2026-05-16 21:13:33 +02:00
pub name: String,
2026-05-15 15:08:20 +02:00
pub path: String,
2026-05-16 21:13:33 +02:00
pub path_exists: bool,
pub size_bytes: Option<u64>,
pub schema_metadata: String,
2026-05-15 15:08:20 +02:00
pub sync_status: String,
}
2026-05-16 21:13:33 +02:00
#[derive(Debug, thiserror::Error)]
pub enum DbError {
#[error("invalid db resource name: {0}")]
InvalidName(String),
}
pub fn validate_db_name(name: &str) -> Result<(), DbError> {
if name.is_empty()
|| !name
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-'))
{
return Err(DbError::InvalidName(name.to_owned()));
}
Ok(())
}
2026-05-15 15:08:20 +02:00
#[must_use]
pub fn crsqlite_sync_roadmap() -> &'static str {
"future db sync reads crsql_changes, exchanges changes over Iroh, and applies through crsql_changes"
}
2026-05-16 21:13:33 +02:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn db_name_validation_rejects_paths_and_empty_names() {
assert!(validate_db_name("notes").is_ok());
assert!(validate_db_name("notes.v1").is_ok());
assert!(validate_db_name("").is_err());
assert!(validate_db_name("../notes").is_err());
assert!(validate_db_name("notes/main").is_err());
assert!(validate_db_name("notes main").is_err());
}
}