diff --git a/crates/reddwarf-apiserver/src/handlers/common.rs b/crates/reddwarf-apiserver/src/handlers/common.rs index 7e44426..f1bd47a 100644 --- a/crates/reddwarf-apiserver/src/handlers/common.rs +++ b/crates/reddwarf-apiserver/src/handlers/common.rs @@ -1,16 +1,13 @@ use crate::{ApiError, AppState, Result}; use reddwarf_core::{Resource, ResourceKey}; -use reddwarf_storage::{KeyEncoder, KVStore}; +use reddwarf_storage::{KVStore, KeyEncoder}; use reddwarf_versioning::{Change, CommitBuilder}; use serde::Serialize; use tracing::{debug, info}; use uuid::Uuid; /// Get a resource from storage -pub async fn get_resource( - state: &AppState, - key: &ResourceKey, -) -> Result { +pub async fn get_resource(state: &AppState, key: &ResourceKey) -> Result { debug!("Getting resource: {}", key); let storage_key = KeyEncoder::encode_resource_key(key); @@ -25,10 +22,7 @@ pub async fn get_resource( } /// Create a resource in storage -pub async fn create_resource( - state: &AppState, - mut resource: T, -) -> Result { +pub async fn create_resource(state: &AppState, mut resource: T) -> Result { let key = resource .resource_key() .map_err(|e| ApiError::BadRequest(e.to_string()))?; @@ -51,7 +45,10 @@ pub async fn create_resource( let data = serde_json::to_vec(&resource)?; // Create commit - let change = Change::create(storage_key.clone(), String::from_utf8_lossy(&data).to_string()); + let change = Change::create( + storage_key.clone(), + String::from_utf8_lossy(&data).to_string(), + ); let commit = state .version_store @@ -73,10 +70,7 @@ pub async fn create_resource( } /// Update a resource in storage -pub async fn update_resource( - state: &AppState, - mut resource: T, -) -> Result { +pub async fn update_resource(state: &AppState, mut resource: T) -> Result { let key = resource .resource_key() .map_err(|e| ApiError::BadRequest(e.to_string()))?; @@ -115,17 +109,17 @@ pub async fn update_resource( resource.set_resource_version(reddwarf_core::ResourceVersion::new(commit.id().to_string())); // Update in storage - state.storage.as_ref().put(storage_key.as_bytes(), &new_data)?; + state + .storage + .as_ref() + .put(storage_key.as_bytes(), &new_data)?; info!("Updated resource: {} with version {}", key, commit.id()); Ok(resource) } /// Delete a resource from storage -pub async fn delete_resource( - state: &AppState, - key: &ResourceKey, -) -> Result<()> { +pub async fn delete_resource(state: &AppState, key: &ResourceKey) -> Result<()> { info!("Deleting resource: {}", key); let storage_key = KeyEncoder::encode_resource_key(key); @@ -138,7 +132,10 @@ pub async fn delete_resource( .ok_or_else(|| ApiError::NotFound(format!("Resource not found: {}", key)))?; // Create commit - let change = Change::delete(storage_key.clone(), String::from_utf8_lossy(&prev_data).to_string()); + let change = Change::delete( + storage_key.clone(), + String::from_utf8_lossy(&prev_data).to_string(), + ); let commit = state .version_store @@ -157,10 +154,7 @@ pub async fn delete_resource( } /// List resources with optional filtering -pub async fn list_resources( - state: &AppState, - prefix: &str, -) -> Result> { +pub async fn list_resources(state: &AppState, prefix: &str) -> Result> { debug!("Listing resources with prefix: {}", prefix); let results = state.storage.as_ref().scan(prefix.as_bytes())?; diff --git a/crates/reddwarf-apiserver/src/handlers/mod.rs b/crates/reddwarf-apiserver/src/handlers/mod.rs index 3302bf2..7e83978 100644 --- a/crates/reddwarf-apiserver/src/handlers/mod.rs +++ b/crates/reddwarf-apiserver/src/handlers/mod.rs @@ -1,12 +1,12 @@ -pub mod pods; -pub mod nodes; -pub mod services; -pub mod namespaces; pub mod common; +pub mod namespaces; +pub mod nodes; +pub mod pods; +pub mod services; // Re-export handler functions -pub use pods::*; -pub use nodes::*; -pub use services::*; -pub use namespaces::*; pub use common::*; +pub use namespaces::*; +pub use nodes::*; +pub use pods::*; +pub use services::*; diff --git a/crates/reddwarf-apiserver/src/handlers/namespaces.rs b/crates/reddwarf-apiserver/src/handlers/namespaces.rs index 26f449c..123c9d6 100644 --- a/crates/reddwarf-apiserver/src/handlers/namespaces.rs +++ b/crates/reddwarf-apiserver/src/handlers/namespaces.rs @@ -1,4 +1,6 @@ -use crate::handlers::common::{create_resource, delete_resource, get_resource, list_resources, update_resource, ListResponse}; +use crate::handlers::common::{ + create_resource, delete_resource, get_resource, list_resources, update_resource, ListResponse, +}; use crate::response::{status_deleted, ApiResponse}; use crate::validation::validate_resource; use crate::{AppState, Result}; @@ -24,9 +26,7 @@ pub async fn get_namespace( } /// GET /api/v1/namespaces -pub async fn list_namespaces( - State(state): State>, -) -> Result { +pub async fn list_namespaces(State(state): State>) -> Result { let prefix = KeyEncoder::encode_prefix("v1", "Namespace", None); let namespaces: Vec = list_resources(&state, &prefix).await?; diff --git a/crates/reddwarf-apiserver/src/handlers/nodes.rs b/crates/reddwarf-apiserver/src/handlers/nodes.rs index 32e66c5..775cf18 100644 --- a/crates/reddwarf-apiserver/src/handlers/nodes.rs +++ b/crates/reddwarf-apiserver/src/handlers/nodes.rs @@ -1,4 +1,6 @@ -use crate::handlers::common::{create_resource, delete_resource, get_resource, list_resources, update_resource, ListResponse}; +use crate::handlers::common::{ + create_resource, delete_resource, get_resource, list_resources, update_resource, ListResponse, +}; use crate::response::{status_deleted, ApiResponse}; use crate::validation::validate_resource; use crate::{AppState, Result}; @@ -24,9 +26,7 @@ pub async fn get_node( } /// GET /api/v1/nodes -pub async fn list_nodes( - State(state): State>, -) -> Result { +pub async fn list_nodes(State(state): State>) -> Result { let prefix = KeyEncoder::encode_prefix("v1", "Node", None); let nodes: Vec = list_resources(&state, &prefix).await?; diff --git a/crates/reddwarf-apiserver/src/handlers/pods.rs b/crates/reddwarf-apiserver/src/handlers/pods.rs index cc36f62..c3a88c3 100644 --- a/crates/reddwarf-apiserver/src/handlers/pods.rs +++ b/crates/reddwarf-apiserver/src/handlers/pods.rs @@ -1,4 +1,6 @@ -use crate::handlers::common::{create_resource, delete_resource, get_resource, list_resources, update_resource, ListResponse}; +use crate::handlers::common::{ + create_resource, delete_resource, get_resource, list_resources, update_resource, ListResponse, +}; use crate::response::{status_deleted, ApiResponse}; use crate::validation::validate_resource; use crate::{AppState, Result}; diff --git a/crates/reddwarf-apiserver/src/handlers/services.rs b/crates/reddwarf-apiserver/src/handlers/services.rs index b02248b..1241f33 100644 --- a/crates/reddwarf-apiserver/src/handlers/services.rs +++ b/crates/reddwarf-apiserver/src/handlers/services.rs @@ -1,4 +1,6 @@ -use crate::handlers::common::{create_resource, delete_resource, get_resource, list_resources, update_resource, ListResponse}; +use crate::handlers::common::{ + create_resource, delete_resource, get_resource, list_resources, update_resource, ListResponse, +}; use crate::response::{status_deleted, ApiResponse}; use crate::validation::validate_resource; use crate::{AppState, Result}; diff --git a/crates/reddwarf-apiserver/src/lib.rs b/crates/reddwarf-apiserver/src/lib.rs index c851692..4bc4a95 100644 --- a/crates/reddwarf-apiserver/src/lib.rs +++ b/crates/reddwarf-apiserver/src/lib.rs @@ -8,10 +8,10 @@ //! - WATCH mechanism for streaming updates pub mod error; -pub mod server; pub mod handlers; -pub mod state; pub mod response; +pub mod server; +pub mod state; pub mod validation; pub mod watch; diff --git a/crates/reddwarf-apiserver/src/server.rs b/crates/reddwarf-apiserver/src/server.rs index 3dc0213..71f671f 100644 --- a/crates/reddwarf-apiserver/src/server.rs +++ b/crates/reddwarf-apiserver/src/server.rs @@ -68,9 +68,7 @@ impl ApiServer { ) .route( "/api/v1/namespaces/{namespace}/services/{name}", - get(get_service) - .put(replace_service) - .delete(delete_service), + get(get_service).put(replace_service).delete(delete_service), ) // Namespaces .route( diff --git a/crates/reddwarf-core/src/error.rs b/crates/reddwarf-core/src/error.rs index 71df199..06bd1f2 100644 --- a/crates/reddwarf-core/src/error.rs +++ b/crates/reddwarf-core/src/error.rs @@ -31,10 +31,7 @@ pub enum ReddwarfError { /// Invalid resource #[error("Invalid resource: {reason}")] - #[diagnostic( - code(reddwarf::invalid_resource), - help("{suggestion}") - )] + #[diagnostic(code(reddwarf::invalid_resource), help("{suggestion}"))] InvalidResource { #[allow(unused)] reason: String, @@ -44,10 +41,7 @@ pub enum ReddwarfError { /// Validation failed #[error("Validation failed for {resource_type}: {details}")] - #[diagnostic( - code(reddwarf::validation_failed), - help("{help_text}") - )] + #[diagnostic(code(reddwarf::validation_failed), help("{help_text}"))] ValidationFailed { #[allow(unused)] resource_type: String, @@ -246,9 +240,7 @@ impl ReddwarfError { /// Create an InvalidKind error pub fn invalid_kind(kind: impl Into) -> Self { - Self::InvalidKind { - kind: kind.into(), - } + Self::InvalidKind { kind: kind.into() } } } diff --git a/crates/reddwarf-core/src/lib.rs b/crates/reddwarf-core/src/lib.rs index a9b7a06..4e5d8e0 100644 --- a/crates/reddwarf-core/src/lib.rs +++ b/crates/reddwarf-core/src/lib.rs @@ -12,12 +12,12 @@ pub mod types; // Re-export commonly used types pub use error::{ReddwarfError, Result}; -pub use resources::{Resource, ResourceError, is_valid_name}; +pub use resources::{is_valid_name, Resource, ResourceError}; pub use types::{GroupVersionKind, ResourceKey, ResourceVersion}; // Re-export k8s-openapi types for convenience pub use k8s_openapi; -pub use k8s_openapi::api::core::v1::{Pod, Node, Service, Namespace}; +pub use k8s_openapi::api::core::v1::{Namespace, Node, Pod, Service}; pub use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta; /// Serialize a resource to JSON diff --git a/crates/reddwarf-core/src/resources/mod.rs b/crates/reddwarf-core/src/resources/mod.rs index 88f0665..bd7a170 100644 --- a/crates/reddwarf-core/src/resources/mod.rs +++ b/crates/reddwarf-core/src/resources/mod.rs @@ -39,7 +39,9 @@ pub trait Resource: Serialize + for<'de> Deserialize<'de> + Send + Sync { /// Get the ResourceKey fn resource_key(&self) -> Result { let metadata = self.metadata(); - let name = metadata.name.as_ref() + let name = metadata + .name + .as_ref() .ok_or_else(|| ResourceError::MissingField("metadata.name".to_string()))?; let namespace = metadata.namespace.clone().unwrap_or_default(); @@ -114,13 +116,13 @@ pub fn is_valid_name(name: &str) -> bool { return false; } - chars.iter().all(|c| { - c.is_ascii_lowercase() || c.is_ascii_digit() || *c == '-' || *c == '.' - }) + chars + .iter() + .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || *c == '-' || *c == '.') } // Implement Resource trait for common k8s-openapi types -use k8s_openapi::api::core::v1::{Pod, Node, Service, Namespace}; +use k8s_openapi::api::core::v1::{Namespace, Node, Pod, Service}; impl Resource for Pod { fn api_version(&self) -> String { @@ -147,7 +149,7 @@ impl Resource for Pod { if let Some(spec) = &self.spec { if spec.containers.is_empty() { return Err(ResourceError::ValidationFailed( - "Pod must have at least one container".to_string() + "Pod must have at least one container".to_string(), )); } } else { diff --git a/crates/reddwarf-core/src/types.rs b/crates/reddwarf-core/src/types.rs index 511bfee..02a6408 100644 --- a/crates/reddwarf-core/src/types.rs +++ b/crates/reddwarf-core/src/types.rs @@ -14,7 +14,11 @@ pub struct GroupVersionKind { impl GroupVersionKind { /// Create a new GVK - pub fn new(group: impl Into, version: impl Into, kind: impl Into) -> Self { + pub fn new( + group: impl Into, + version: impl Into, + kind: impl Into, + ) -> Self { Self { group: group.into(), version: version.into(), @@ -90,7 +94,11 @@ pub struct ResourceKey { impl ResourceKey { /// Create a new ResourceKey - pub fn new(gvk: GroupVersionKind, namespace: impl Into, name: impl Into) -> Self { + pub fn new( + gvk: GroupVersionKind, + namespace: impl Into, + name: impl Into, + ) -> Self { Self { gvk, namespace: namespace.into(), @@ -117,7 +125,10 @@ impl ResourceKey { pub fn storage_key(&self) -> String { let api_version = self.gvk.api_version(); if self.is_namespaced() { - format!("{}/{}/{}/{}", api_version, self.gvk.kind, self.namespace, self.name) + format!( + "{}/{}/{}/{}", + api_version, self.gvk.kind, self.namespace, self.name + ) } else { format!("{}/{}/{}", api_version, self.gvk.kind, self.name) } @@ -129,7 +140,10 @@ impl ResourceKey { let resource = self.gvk.resource_name(); if self.is_namespaced() { - format!("/{}/namespaces/{}/{}/{}", base, self.namespace, resource, self.name) + format!( + "/{}/namespaces/{}/{}/{}", + base, self.namespace, resource, self.name + ) } else { format!("/{}/{}/{}", base, resource, self.name) } diff --git a/crates/reddwarf-scheduler/src/error.rs b/crates/reddwarf-scheduler/src/error.rs index f257192..28a2026 100644 --- a/crates/reddwarf-scheduler/src/error.rs +++ b/crates/reddwarf-scheduler/src/error.rs @@ -13,21 +13,12 @@ pub enum SchedulerError { code(scheduler::no_suitable_nodes), help("Check node resources, taints, and pod requirements") )] - NoSuitableNodes { - pod_name: String, - reason: String, - }, + NoSuitableNodes { pod_name: String, reason: String }, /// Scheduling failed #[error("Scheduling failed: {message}")] - #[diagnostic( - code(scheduler::scheduling_failed), - help("{suggestion}") - )] - SchedulingFailed { - message: String, - suggestion: String, - }, + #[diagnostic(code(scheduler::scheduling_failed), help("{suggestion}"))] + SchedulingFailed { message: String, suggestion: String }, /// Storage error #[error("Storage error: {0}")] @@ -39,10 +30,7 @@ pub enum SchedulerError { /// Core error #[error("Core error: {0}")] - #[diagnostic( - code(scheduler::core_error), - help("This is an internal error") - )] + #[diagnostic(code(scheduler::core_error), help("This is an internal error"))] CoreError(#[from] reddwarf_core::ReddwarfError), /// Internal error @@ -51,9 +39,7 @@ pub enum SchedulerError { code(scheduler::internal_error), help("This is likely a bug. Please report it") )] - InternalError { - message: String, - }, + InternalError { message: String }, } /// Result type for scheduler operations diff --git a/crates/reddwarf-scheduler/src/filter.rs b/crates/reddwarf-scheduler/src/filter.rs index 244832a..2bac511 100644 --- a/crates/reddwarf-scheduler/src/filter.rs +++ b/crates/reddwarf-scheduler/src/filter.rs @@ -193,7 +193,8 @@ impl FilterPredicate for TaintToleration { let toleration_effect = toleration.effect.as_ref(); if toleration_key.as_ref() == Some(&taint_key) - && (toleration_effect.is_none() || toleration_effect.as_ref() == Some(&taint_effect)) + && (toleration_effect.is_none() + || toleration_effect.as_ref() == Some(&taint_effect)) { tolerated = true; break; @@ -203,7 +204,10 @@ impl FilterPredicate for TaintToleration { if !tolerated { return FilterResult::fail( node_name, - format!("Pod does not tolerate taint: {}={}", taint_key, taint_effect), + format!( + "Pod does not tolerate taint: {}={}", + taint_key, taint_effect + ), ); } } diff --git a/crates/reddwarf-scheduler/src/lib.rs b/crates/reddwarf-scheduler/src/lib.rs index e1b2961..51ae750 100644 --- a/crates/reddwarf-scheduler/src/lib.rs +++ b/crates/reddwarf-scheduler/src/lib.rs @@ -7,12 +7,12 @@ //! - Pod binding to nodes pub mod error; -pub mod types; pub mod filter; -pub mod score; pub mod scheduler; +pub mod score; +pub mod types; // Re-export commonly used types -pub use error::{SchedulerError, Result}; +pub use error::{Result, SchedulerError}; pub use scheduler::Scheduler; -pub use types::{SchedulingContext, FilterResult, ScoreResult}; +pub use types::{FilterResult, SchedulingContext, ScoreResult}; diff --git a/crates/reddwarf-scheduler/src/scheduler.rs b/crates/reddwarf-scheduler/src/scheduler.rs index 1af9a71..20086ef 100644 --- a/crates/reddwarf-scheduler/src/scheduler.rs +++ b/crates/reddwarf-scheduler/src/scheduler.rs @@ -3,7 +3,7 @@ use crate::score::{calculate_weighted_score, default_scores, ScoreFunction}; use crate::types::SchedulingContext; use crate::{Result, SchedulerError}; use reddwarf_core::{Node, Pod}; -use reddwarf_storage::{KeyEncoder, KVStore, RedbBackend}; +use reddwarf_storage::{KVStore, KeyEncoder, RedbBackend}; use std::sync::Arc; use std::time::Duration; use tokio::time::sleep; @@ -110,8 +110,9 @@ impl Scheduler { let mut unscheduled = Vec::new(); for (_key, data) in results.iter() { - let pod: Pod = serde_json::from_slice(data) - .map_err(|e| SchedulerError::internal_error(format!("Failed to deserialize pod: {}", e)))?; + let pod: Pod = serde_json::from_slice(data).map_err(|e| { + SchedulerError::internal_error(format!("Failed to deserialize pod: {}", e)) + })?; // Check if pod is unscheduled if let Some(spec) = &pod.spec { @@ -132,8 +133,9 @@ impl Scheduler { let mut nodes = Vec::new(); for (_key, data) in results.iter() { - let node: Node = serde_json::from_slice(data) - .map_err(|e| SchedulerError::internal_error(format!("Failed to deserialize node: {}", e)))?; + let node: Node = serde_json::from_slice(data).map_err(|e| { + SchedulerError::internal_error(format!("Failed to deserialize node: {}", e)) + })?; nodes.push(node); } @@ -268,8 +270,9 @@ impl Scheduler { ); let storage_key = KeyEncoder::encode_resource_key(&key); - let data = serde_json::to_vec(&pod) - .map_err(|e| SchedulerError::internal_error(format!("Failed to serialize pod: {}", e)))?; + let data = serde_json::to_vec(&pod).map_err(|e| { + SchedulerError::internal_error(format!("Failed to serialize pod: {}", e)) + })?; self.storage.as_ref().put(storage_key.as_bytes(), &data)?; diff --git a/crates/reddwarf-scheduler/src/score.rs b/crates/reddwarf-scheduler/src/score.rs index 34aa9fb..3f8c48e 100644 --- a/crates/reddwarf-scheduler/src/score.rs +++ b/crates/reddwarf-scheduler/src/score.rs @@ -176,10 +176,7 @@ impl ScoreFunction for BalancedAllocation { /// Get default scoring functions pub fn default_scores() -> Vec> { - vec![ - Box::new(LeastAllocated), - Box::new(BalancedAllocation), - ] + vec![Box::new(LeastAllocated), Box::new(BalancedAllocation)] } /// Calculate weighted score from multiple scoring functions diff --git a/crates/reddwarf-scheduler/src/types.rs b/crates/reddwarf-scheduler/src/types.rs index 3460a35..be517c2 100644 --- a/crates/reddwarf-scheduler/src/types.rs +++ b/crates/reddwarf-scheduler/src/types.rs @@ -104,7 +104,10 @@ impl ResourceQuantities { /// Get CPU and memory from a resource map (k8s-openapi format) pub fn from_k8s_resource_map( - resources: &std::collections::BTreeMap, + resources: &std::collections::BTreeMap< + String, + k8s_openapi::apimachinery::pkg::api::resource::Quantity, + >, ) -> Self { let cpu_millicores = resources .get("cpu") diff --git a/crates/reddwarf-storage/src/encoding.rs b/crates/reddwarf-storage/src/encoding.rs index 04501cc..94d0eae 100644 --- a/crates/reddwarf-storage/src/encoding.rs +++ b/crates/reddwarf-storage/src/encoding.rs @@ -104,7 +104,10 @@ impl IndexKey { name, } => { if let Some(ns) = namespace { - format!("label/{}/{}/{}/{}/{}/{}", key, value, api_version, kind, ns, name) + format!( + "label/{}/{}/{}/{}/{}/{}", + key, value, api_version, kind, ns, name + ) } else { format!("label/{}/{}/{}/{}/{}", key, value, api_version, kind, name) } @@ -118,9 +121,15 @@ impl IndexKey { name, } => { if let Some(ns) = namespace { - format!("field/{}/{}/{}/{}/{}/{}", field_path, value, api_version, kind, ns, name) + format!( + "field/{}/{}/{}/{}/{}/{}", + field_path, value, api_version, kind, ns, name + ) } else { - format!("field/{}/{}/{}/{}/{}", field_path, value, api_version, kind, name) + format!( + "field/{}/{}/{}/{}/{}", + field_path, value, api_version, kind, name + ) } } } @@ -165,7 +174,10 @@ mod tests { fn test_encode_resource_key() { let gvk = GroupVersionKind::from_api_version_kind("v1", "Pod"); let key = ResourceKey::new(gvk, "default", "nginx"); - assert_eq!(KeyEncoder::encode_resource_key(&key), "v1/Pod/default/nginx"); + assert_eq!( + KeyEncoder::encode_resource_key(&key), + "v1/Pod/default/nginx" + ); let gvk = GroupVersionKind::from_api_version_kind("v1", "Node"); let key = ResourceKey::cluster_scoped(gvk, "node-1"); diff --git a/crates/reddwarf-storage/src/error.rs b/crates/reddwarf-storage/src/error.rs index 4002604..0b2100d 100644 --- a/crates/reddwarf-storage/src/error.rs +++ b/crates/reddwarf-storage/src/error.rs @@ -13,9 +13,7 @@ pub enum StorageError { code(storage::key_not_found), help("Verify the key exists in the database") )] - KeyNotFound { - key: String, - }, + KeyNotFound { key: String }, /// Database error #[error("Database error: {message}")] @@ -35,9 +33,7 @@ pub enum StorageError { code(storage::transaction_error), help("Ensure the transaction is not already committed or aborted") )] - TransactionError { - message: String, - }, + TransactionError { message: String }, /// Serialization error #[error("Serialization error: {message}")] diff --git a/crates/reddwarf-storage/src/lib.rs b/crates/reddwarf-storage/src/lib.rs index c02acec..f8a7f53 100644 --- a/crates/reddwarf-storage/src/lib.rs +++ b/crates/reddwarf-storage/src/lib.rs @@ -6,13 +6,13 @@ //! - Key encoding and indexing //! - Transaction support +pub mod encoding; pub mod error; pub mod kv; pub mod redb_backend; -pub mod encoding; // Re-export commonly used types -pub use error::{StorageError, Result}; +pub use encoding::{IndexKey, KeyEncoder}; +pub use error::{Result, StorageError}; pub use kv::{KVStore, Transaction}; pub use redb_backend::RedbBackend; -pub use encoding::{KeyEncoder, IndexKey}; diff --git a/crates/reddwarf-storage/src/redb_backend.rs b/crates/reddwarf-storage/src/redb_backend.rs index 2d208ee..3a10f88 100644 --- a/crates/reddwarf-storage/src/redb_backend.rs +++ b/crates/reddwarf-storage/src/redb_backend.rs @@ -21,7 +21,10 @@ impl RedbBackend { info!("Opening redb database at: {}", path.as_ref().display()); let db = Database::create(path.as_ref()).map_err(|e| { - StorageError::database_error(format!("Failed to create database: {}", e), Some(Box::new(e))) + StorageError::database_error( + format!("Failed to create database: {}", e), + Some(Box::new(e)), + ) })?; // Create tables if they don't exist @@ -87,7 +90,10 @@ impl KVStore for RedbBackend { } fn scan(&self, prefix: &[u8]) -> Result> { - debug!("Scanning with prefix: {:?}", String::from_utf8_lossy(prefix)); + debug!( + "Scanning with prefix: {:?}", + String::from_utf8_lossy(prefix) + ); let read_txn = self.db.begin_read()?; let table = read_txn.open_table(RESOURCES_TABLE)?; diff --git a/crates/reddwarf-versioning/src/error.rs b/crates/reddwarf-versioning/src/error.rs index 947cce9..680f554 100644 --- a/crates/reddwarf-versioning/src/error.rs +++ b/crates/reddwarf-versioning/src/error.rs @@ -13,9 +13,7 @@ pub enum VersioningError { code(versioning::commit_not_found), help("Verify the commit ID is correct and exists in the repository") )] - CommitNotFound { - commit_id: String, - }, + CommitNotFound { commit_id: String }, /// Conflict detected #[error("Conflict detected: {message}")] @@ -30,14 +28,8 @@ pub enum VersioningError { /// Invalid operation #[error("Invalid operation: {message}")] - #[diagnostic( - code(versioning::invalid_operation), - help("{suggestion}") - )] - InvalidOperation { - message: String, - suggestion: String, - }, + #[diagnostic(code(versioning::invalid_operation), help("{suggestion}"))] + InvalidOperation { message: String, suggestion: String }, /// Storage error #[error("Storage error: {0}")] @@ -49,10 +41,7 @@ pub enum VersioningError { /// Core error #[error("Core error: {0}")] - #[diagnostic( - code(versioning::core_error), - help("This is an internal error") - )] + #[diagnostic(code(versioning::core_error), help("This is an internal error"))] CoreError(#[from] reddwarf_core::ReddwarfError), /// Internal error @@ -61,9 +50,7 @@ pub enum VersioningError { code(versioning::internal_error), help("This is likely a bug. Please report it with full error details") )] - InternalError { - message: String, - }, + InternalError { message: String }, } /// Result type for versioning operations diff --git a/crates/reddwarf-versioning/src/lib.rs b/crates/reddwarf-versioning/src/lib.rs index 12d9038..c94c23b 100644 --- a/crates/reddwarf-versioning/src/lib.rs +++ b/crates/reddwarf-versioning/src/lib.rs @@ -6,13 +6,13 @@ //! - Conflict detection and representation //! - DAG traversal for WATCH operations -pub mod error; -pub mod store; pub mod commit; pub mod conflict; +pub mod error; +pub mod store; // Re-export commonly used types -pub use error::{VersioningError, Result}; -pub use store::VersionStore; -pub use commit::{Commit, CommitBuilder, Change, ChangeType}; +pub use commit::{Change, ChangeType, Commit, CommitBuilder}; pub use conflict::{Conflict, ConflictSide}; +pub use error::{Result, VersioningError}; +pub use store::VersionStore; diff --git a/crates/reddwarf-versioning/src/store.rs b/crates/reddwarf-versioning/src/store.rs index ac37f51..29cd09d 100644 --- a/crates/reddwarf-versioning/src/store.rs +++ b/crates/reddwarf-versioning/src/store.rs @@ -37,11 +37,13 @@ impl VersionStore { debug!("Creating commit: {}", commit.id); // Serialize and store the commit - let commit_json = serde_json::to_string(&commit) - .map_err(|e| VersioningError::internal_error(format!("Failed to serialize commit: {}", e)))?; + let commit_json = serde_json::to_string(&commit).map_err(|e| { + VersioningError::internal_error(format!("Failed to serialize commit: {}", e)) + })?; let commit_key = format!("version:commit:{}", commit.id); - self.storage.put(commit_key.as_bytes(), commit_json.as_bytes())?; + self.storage + .put(commit_key.as_bytes(), commit_json.as_bytes())?; // Update HEAD self.set_head(commit.id.clone())?; @@ -60,8 +62,9 @@ impl VersionStore { .get(commit_key.as_bytes())? .ok_or_else(|| VersioningError::commit_not_found(commit_id))?; - let commit: Commit = serde_json::from_slice(&commit_bytes) - .map_err(|e| VersioningError::internal_error(format!("Failed to deserialize commit: {}", e)))?; + let commit: Commit = serde_json::from_slice(&commit_bytes).map_err(|e| { + VersioningError::internal_error(format!("Failed to deserialize commit: {}", e)) + })?; Ok(commit) } @@ -90,8 +93,9 @@ impl VersionStore { for key in keys { let commit_bytes = self.storage.get(&key)?.unwrap(); - let commit: Commit = serde_json::from_slice(&commit_bytes) - .map_err(|e| VersioningError::internal_error(format!("Failed to deserialize commit: {}", e)))?; + let commit: Commit = serde_json::from_slice(&commit_bytes).map_err(|e| { + VersioningError::internal_error(format!("Failed to deserialize commit: {}", e)) + })?; commits.push(commit); } @@ -100,7 +104,10 @@ impl VersionStore { /// Detect conflicts between two commits pub fn detect_conflicts(&self, commit_id1: &str, commit_id2: &str) -> Result> { - debug!("Detecting conflicts between {} and {}", commit_id1, commit_id2); + debug!( + "Detecting conflicts between {} and {}", + commit_id1, commit_id2 + ); let commit1 = self.get_commit(commit_id1)?; let commit2 = self.get_commit(commit_id2)?; @@ -148,7 +155,11 @@ impl VersionStore { } /// Find the common ancestor of two commits (simplified BFS) - pub fn find_common_ancestor(&self, commit_id1: &str, commit_id2: &str) -> Result> { + pub fn find_common_ancestor( + &self, + commit_id1: &str, + commit_id2: &str, + ) -> Result> { let _commit1 = self.get_commit(commit_id1)?; let _commit2 = self.get_commit(commit_id2)?; @@ -239,7 +250,11 @@ mod tests { // Create a commit let change = Change::create("v1/Pod/default/nginx".to_string(), "{}".to_string()); let commit = store - .create_commit(CommitBuilder::new().change(change).message("Initial commit".to_string())) + .create_commit( + CommitBuilder::new() + .change(change) + .message("Initial commit".to_string()), + ) .unwrap(); assert!(!commit.id.is_empty()); @@ -262,9 +277,16 @@ mod tests { let store = VersionStore::new(backend).unwrap(); // Create base commit - let change1 = Change::create("v1/Pod/default/nginx".to_string(), "{\"version\":0}".to_string()); + let change1 = Change::create( + "v1/Pod/default/nginx".to_string(), + "{\"version\":0}".to_string(), + ); let commit1 = store - .create_commit(CommitBuilder::new().change(change1).message("Base".to_string())) + .create_commit( + CommitBuilder::new() + .change(change1) + .message("Base".to_string()), + ) .unwrap(); // Create two diverging commits from the base