Singleton pattern for single-row configuration tables
| Tier | foundation |
| Role | unclassified (baselined) |
| Path | crates/foundation/singleton |
| Edition | 2021 |
| Targets | foundation_singleton |
| Public items | 0 across 0 modules |
| Tests | 28 |
What it is for
# foundation-singleton
Singleton pattern for single-row configuration tables.
Singletons ensure exactly one row exists per type, with:
- Safe concurrent access via transaction handling
- Corruption detection (multiple rows)
- Deletion prevention
- Optional environment variable fallback
Example
use foundation_singleton::{Singleton, SingletonError};
struct SiteSettings {
id: i32,
site_name: String,
maintenance_mode: bool,
}
impl Singleton for SiteSettings {
fn id(&self) -> i32 {
self.id
}
}
let settings = SiteSettings {
id: 1,
site_name: "My Site".to_string(),
maintenance_mode: false,
};
// Singleton ID is always 1
assert_eq!(settings.id(), 1);
assert!(settings.is_valid_singleton());
// Deletion is not allowed
assert!(settings.delete().is_err());
Capabilities
No public items.
How to use it
From this crate's own rustdoc:
use foundation_singleton::{Singleton, SingletonError};
struct SiteSettings {
id: i32,
site_name: String,
maintenance_mode: bool,
}
impl Singleton for SiteSettings {
fn id(&self) -> i32 {
self.id
}
}
let settings = SiteSettings {
id: 1,
site_name: "My Site".to_string(),
maintenance_mode: false,
};
// Singleton ID is always 1
assert_eq!(settings.id(), 1);
assert!(settings.is_valid_singleton());
// Deletion is not allowed
assert!(settings.delete().is_err());
Module structure
No public modules: the crate root is its whole surface.
Public surface
No public items.
Re-exports. Exported here, defined elsewhere.
| Export | Defined in |
|---|---|
{EnvFallback,Singleton} | traits::{EnvFallback,Singleton} |
{SingletonError,ValueSource} | error::{SingletonError,ValueSource} |
Boundary
Depends on no other workspace tier.
Shares tier foundation with 27 other crates: foundation-audit-log, foundation-basemodels, foundation-bounded-io, foundation-conversation-closure, foundation-crypto-sign, foundation-decisioning, foundation-encounter-vocabulary, foundation-fs-metadata, … (27 total).
_What this crate deliberately does NOT own is a judgment. No committed registry records one for it, so none is stated here._
Where it sits
| Tier (ontology) | foundation |
| Architectural role (taxonomy) | unclassified (baselined) |
| Location | crates/foundation/singleton |
| Vocabulary in force (lexicon) | current |
Dependencies
Runtime, in this workspace.
| Crate | Tier | Optional | Only on |
|---|---|---|---|
| `foundation-basemodels` | foundation | no | always |
Runtime, from outside the workspace.
| Crate | Requirement | Features | Optional | Only on |
|---|---|---|---|---|
sqlx | ^0.8 | runtime-tokio, postgres, chrono, uuid, json | no | always |
thiserror | ^2 | — | no | always |
tokio | ^1 | full | no | always |
Development, from outside the workspace.
| Crate | Requirement | Features | Optional | Only on |
|---|---|---|---|---|
tokio-test | ^0.4 | — | no | always |
Build. None.
Depended on by. 1 workspace crate.
Signal flow — what reaches this crate, and what it reaches.
flowchart LR n_application_singleton["application-singleton"] -->|uses| SELF SELF["foundation-singleton"] SELF -->|runtime| n_foundation_basemodels["foundation-basemodels"] classDef self fill:#1f883d,stroke:#1f883d,color:#fff; class SELF self;
Feature flags
No Cargo features are defined: every capability is unconditional, so no consumer can receive a half-wired crate.
Targets
| Kind | Name | Source |
|---|---|---|
| lib | foundation_singleton | `src/lib.rs` |
Error model
No public error type was detected: no public item declares a type named *Error, and no public signature returns one.
Operational characteristics
| Property | Evidence |
|---|---|
| async public surface | none detected |
| async runtime | yes |
| database access | yes |
| network I/O | none detected |
| unsafe code | none detected |
| environment variables | none detected |
No unsafe block, unsafe fn, unsafe impl or unsafe trait was found by the parser anywhere in this crate's source.
Configuration
No environment variable is read with a literal name anywhere in this crate. A variable whose key is computed at run time cannot be listed here, and is not claimed to be absent.
Related capabilities
1 workspace crate depends on this one: application-singleton.
Verification
| Kind | Count |
|---|---|
| Unit tests | 28 |
| Integration tests | 0 |
| Examples | 0 |
| Doctests | 1 |
What the tests establish, by name:
test_delete_error_is_deletion_not_allowed—src/lib.rstest_delete_error_message—src/lib.rstest_delete_returns_error—src/lib.rstest_env_fallback_db_takes_precedence_over_env—src/lib.rstest_env_fallback_returns_db_value_when_present—src/lib.rstest_env_fallback_returns_default_when_empty—src/lib.rstest_env_fallback_returns_env_var_when_db_empty—src/lib.rstest_fields_persist_correctly—src/lib.rstest_get_env_var_name—src/lib.rstest_get_value_source_database—src/lib.rstest_get_value_source_default—src/lib.rstest_get_value_source_environment—src/lib.rstest_has_value_false_when_empty—src/lib.rstest_has_value_true_for_db—src/lib.rstest_has_value_true_for_env—src/lib.rstest_is_valid_singleton_false_for_negative—src/lib.rstest_is_valid_singleton_false_for_wrong_id—src/lib.rstest_is_valid_singleton_false_for_zero—src/lib.rstest_is_valid_singleton_true_for_id_1—src/lib.rstest_multiple_instances_have_same_singleton_id—src/lib.rstest_singleton_error_display_corrupted—src/lib.rstest_singleton_error_display_deletion—src/lib.rstest_singleton_id_is_one—src/lib.rstest_singleton_id_returns_correct_value—src/lib.rstest_singleton_id_with_custom_id—src/lib.rstest_value_source_clone—src/lib.rstest_value_source_debug—src/lib.rstest_value_source_equality—src/lib.rs
Documentation coverage
| Measure | Documented | Total |
|---|---|---|
| Public items with rustdoc | 0 | 0 |
Public modules with a //! block | 0 | 0 |
Metrics
| Metric | Value |
|---|---|
| Rust source files | 3 |
| Source lines | 581 |
| Code lines | 315 |
| Public API items | 0 |
| Public modules | 0 |
| Tests | 28 |
| Examples | 0 |
| Cargo features | 0 |
| Direct runtime dependencies | 4 |
| Workspace reverse dependencies | 1 |
pie showData
title Rust source composition
"Code" : 315
"Blank or comment" : 266
Generation
Rendered by tools-corpus corpus readme from repository evidence alone, renderer schema 2, lexicon current. No model, network service or database was consulted. Regenerate with tools-corpus corpus readme --write; verify with --check.