#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]
use core::fmt;
macro_rules! string_newtype {
($(#[$meta:meta])* $name:ident) => {
$(#[$meta])*
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct $name(String);
impl $name {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl AsRef<str> for $name {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl From<String> for $name {
fn from(value: String) -> Self {
Self::new(value)
}
}
impl From<&str> for $name {
fn from(value: &str) -> Self {
Self::new(value)
}
}
impl fmt::Display for $name {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
};
}
string_newtype! {
KeyspaceName
}
string_newtype! {
ColumnFamilyName
}
string_newtype! {
WideTableName
}
string_newtype! {
PartitionKey
}
string_newtype! {
ClusteringKey
}
string_newtype! {
ColumnName
}
string_newtype! {
ColumnValue
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ColumnFamily {
name: ColumnFamilyName,
columns: Vec<ColumnName>,
}
impl ColumnFamily {
pub fn new(name: ColumnFamilyName) -> Self {
Self {
name,
columns: Vec::new(),
}
}
pub fn with_column(mut self, column: ColumnName) -> Self {
self.columns.push(column);
self
}
pub const fn name(&self) -> &ColumnFamilyName {
&self.name
}
pub fn columns(&self) -> &[ColumnName] {
&self.columns
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct WideColumnRow {
partition_key: PartitionKey,
clustering_key: Option<ClusteringKey>,
columns: Vec<(ColumnName, ColumnValue)>,
}
impl WideColumnRow {
pub fn new(partition_key: PartitionKey) -> Self {
Self {
partition_key,
clustering_key: None,
columns: Vec::new(),
}
}
pub fn with_clustering_key(mut self, clustering_key: ClusteringKey) -> Self {
self.clustering_key = Some(clustering_key);
self
}
pub fn with_column(mut self, name: ColumnName, value: ColumnValue) -> Self {
self.columns.push((name, value));
self
}
pub const fn partition_key(&self) -> &PartitionKey {
&self.partition_key
}
pub const fn clustering_key(&self) -> Option<&ClusteringKey> {
self.clustering_key.as_ref()
}
pub fn columns(&self) -> &[(ColumnName, ColumnValue)] {
&self.columns
}
}
#[cfg(test)]
mod tests {
use super::{
ClusteringKey, ColumnFamily, ColumnFamilyName, ColumnName, ColumnValue, KeyspaceName,
PartitionKey, WideColumnRow, WideTableName,
};
#[test]
fn constructs_wide_column_labels() {
assert_eq!(KeyspaceName::new("app").to_string(), "app");
assert_eq!(WideTableName::new("events").as_ref(), "events");
assert_eq!(ColumnFamilyName::new("profile").as_str(), "profile");
}
#[test]
fn builds_families_and_rows() {
let family = ColumnFamily::new(ColumnFamilyName::new("profile"))
.with_column(ColumnName::new("display_name"));
let row = WideColumnRow::new(PartitionKey::new("customer_123"))
.with_clustering_key(ClusteringKey::new("2026-05"))
.with_column(ColumnName::new("display_name"), ColumnValue::new("Joshua"));
assert_eq!(family.columns().len(), 1);
assert_eq!(row.partition_key().as_str(), "customer_123");
assert_eq!(row.columns().len(), 1);
}
}