Skip to content

Commit cbb61a6

Browse files
authored
feat: add docs (#2)
1 parent c263cf1 commit cbb61a6

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

src/config.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1+
//! Configuration types for the registry server.
2+
13
use std::path::PathBuf;
24

5+
/// Storage backend for registry data.
36
#[derive(Debug, Clone)]
47
pub enum StorageBackend {
8+
/// In-memory storage (data lost when server stops).
59
Memory,
10+
/// Temporary directory storage (cleaned up automatically).
611
TempDir,
12+
/// Persistent directory storage at a specific path.
713
Directory(PathBuf),
814
}
915

16+
/// Configuration for the registry server.
1017
#[derive(Debug, Clone)]
1118
pub struct RegistryConfig {
19+
/// Storage backend to use.
1220
pub storage: StorageBackend,
21+
/// Port to bind to (None for random port).
1322
pub port: Option<u16>,
23+
/// Host address to bind to.
1424
pub host: String,
1525
}
1626

1727
impl RegistryConfig {
28+
/// Creates a new configuration with the specified storage backend.
1829
pub fn new(storage: StorageBackend) -> Self {
1930
Self {
2031
storage,
@@ -23,23 +34,28 @@ impl RegistryConfig {
2334
}
2435
}
2536

37+
/// Creates a configuration with in-memory storage.
2638
pub fn memory() -> Self {
2739
Self::new(StorageBackend::Memory)
2840
}
2941

42+
/// Creates a configuration with temporary directory storage.
3043
pub fn temp_dir() -> Self {
3144
Self::new(StorageBackend::TempDir)
3245
}
3346

47+
/// Creates a configuration with directory storage at the specified path.
3448
pub fn directory(path: PathBuf) -> Self {
3549
Self::new(StorageBackend::Directory(path))
3650
}
3751

52+
/// Sets a specific port for the server to bind to.
3853
pub fn with_port(mut self, port: u16) -> Self {
3954
self.port = Some(port);
4055
self
4156
}
4257

58+
/// Sets the host address for the server to bind to.
4359
pub fn with_host(mut self, host: impl Into<String>) -> Self {
4460
self.host = host.into();
4561
self

src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
//! Error types for the registry.
2+
13
use thiserror::Error;
24

5+
/// Result type alias for registry operations.
36
pub type Result<T> = std::result::Result<T, RegistryError>;
47

8+
/// Errors that can occur during registry operations.
59
#[derive(Error, Debug)]
610
pub enum RegistryError {
711
#[error("IO error: {0}")]

src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
//! A minimal, OCI-compliant container registry for testing and development.
2+
//!
3+
//! This crate provides a simple way to spin up a local container registry for testing
4+
//! Docker/OCI container workflows without external dependencies.
5+
//!
6+
//! # Examples
7+
//!
8+
//! ```no_run
9+
//! use registry_testkit::{RegistryServer, RegistryConfig};
10+
//!
11+
//! #[tokio::main]
12+
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
13+
//! let config = RegistryConfig::memory();
14+
//! let server = RegistryServer::new(config).await?;
15+
//! println!("Registry running at {}", server.url());
16+
//! Ok(())
17+
//! }
18+
//! ```
19+
120
pub mod config;
221
pub mod error;
322
pub mod server;

src/server.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! OCI-compliant registry server implementation.
2+
13
use crate::config::RegistryConfig;
24
use crate::error::Result;
35
use crate::storage::{create_storage, ManifestEntry, Storage};
@@ -38,12 +40,29 @@ struct UploadParams {
3840
digest: Option<String>,
3941
}
4042

43+
/// The main registry server.
44+
///
45+
/// Implements an OCI-compliant container registry that can be used for
46+
/// testing Docker/container workflows.
4147
pub struct RegistryServer {
4248
addr: SocketAddr,
4349
_handle: tokio::task::JoinHandle<()>,
4450
}
4551

4652
impl RegistryServer {
53+
/// Creates and starts a new registry server with the given configuration.
54+
///
55+
/// # Examples
56+
///
57+
/// ```no_run
58+
/// use registry_testkit::{RegistryServer, RegistryConfig};
59+
///
60+
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
61+
/// let config = RegistryConfig::memory();
62+
/// let server = RegistryServer::new(config).await?;
63+
/// # Ok(())
64+
/// # }
65+
/// ```
4766
pub async fn new(config: RegistryConfig) -> Result<Self> {
4867
let storage = create_storage(&config.storage).await?;
4968

@@ -87,14 +106,28 @@ impl RegistryServer {
87106
})
88107
}
89108

109+
/// Returns the socket address the server is bound to.
90110
pub fn addr(&self) -> SocketAddr {
91111
self.addr
92112
}
93113

114+
/// Returns the full URL of the registry server.
115+
///
116+
/// # Examples
117+
///
118+
/// ```no_run
119+
/// # use registry_testkit::{RegistryServer, RegistryConfig};
120+
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
121+
/// # let server = RegistryServer::new(RegistryConfig::memory()).await?;
122+
/// println!("Registry URL: {}", server.url());
123+
/// # Ok(())
124+
/// # }
125+
/// ```
94126
pub fn url(&self) -> String {
95127
format!("http://{}", self.addr)
96128
}
97129

130+
/// Returns the port number the server is listening on.
98131
pub fn port(&self) -> u16 {
99132
self.addr.port()
100133
}

src/storage.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Storage backends for registry data.
2+
13
use crate::config::StorageBackend;
24
use crate::error::{RegistryError, Result};
35
use async_trait::async_trait;
@@ -7,23 +9,35 @@ use std::sync::Arc;
79
use tokio::fs;
810
use tokio::sync::RwLock;
911

12+
/// Container image manifest with metadata.
1013
#[derive(Clone)]
1114
pub struct ManifestEntry {
15+
/// Raw manifest data.
1216
pub data: Vec<u8>,
17+
/// Content type of the manifest.
1318
pub content_type: String,
1419
}
1520

21+
/// Trait for storage backends handling registry data.
1622
#[async_trait]
1723
pub trait Storage: Send + Sync {
24+
/// Stores a manifest with the given key.
1825
async fn store_manifest(&self, key: String, entry: ManifestEntry) -> Result<()>;
26+
/// Retrieves a manifest by key.
1927
async fn get_manifest(&self, key: &str) -> Result<Option<ManifestEntry>>;
28+
/// Stores a blob with the given digest.
2029
async fn store_blob(&self, digest: String, data: Vec<u8>) -> Result<()>;
30+
/// Retrieves a blob by digest.
2131
async fn get_blob(&self, digest: &str) -> Result<Option<Vec<u8>>>;
32+
/// Creates a new upload session with the given UUID.
2233
async fn create_upload(&self, uuid: String) -> Result<()>;
34+
/// Appends data to an existing upload session.
2335
async fn append_upload(&self, uuid: &str, data: &[u8]) -> Result<()>;
36+
/// Finalizes an upload session and returns the complete data.
2437
async fn finish_upload(&self, uuid: &str) -> Result<Option<Vec<u8>>>;
2538
}
2639

40+
/// In-memory storage implementation.
2741
#[derive(Default)]
2842
pub struct MemoryStorage {
2943
manifests: Arc<RwLock<HashMap<String, ManifestEntry>>>,
@@ -32,6 +46,7 @@ pub struct MemoryStorage {
3246
}
3347

3448
impl MemoryStorage {
49+
/// Creates a new in-memory storage backend.
3550
pub fn new() -> Self {
3651
Self::default()
3752
}
@@ -76,12 +91,14 @@ impl Storage for MemoryStorage {
7691
}
7792
}
7893

94+
/// Disk-based storage implementation.
7995
pub struct DiskStorage {
8096
base_path: PathBuf,
8197
_temp_dir: Option<tempfile::TempDir>,
8298
}
8399

84100
impl DiskStorage {
101+
/// Creates a new disk storage backend at the specified path.
85102
pub async fn new(path: PathBuf) -> Result<Self> {
86103
fs::create_dir_all(&path).await?;
87104
fs::create_dir_all(path.join("manifests")).await?;
@@ -94,6 +111,7 @@ impl DiskStorage {
94111
})
95112
}
96113

114+
/// Creates a new temporary disk storage backend.
97115
pub async fn temp() -> Result<Self> {
98116
let temp_dir = tempfile::tempdir()?;
99117
let path = temp_dir.path().to_path_buf();
@@ -211,6 +229,7 @@ impl Storage for DiskStorage {
211229
}
212230
}
213231

232+
/// Creates a storage backend from the given configuration.
214233
pub async fn create_storage(backend: &StorageBackend) -> Result<Arc<dyn Storage>> {
215234
match backend {
216235
StorageBackend::Memory => Ok(Arc::new(MemoryStorage::new())),

0 commit comments

Comments
 (0)