0.5.1-pre.006

This commit is contained in:
2026-08-10 02:44:24 +02:00
parent b6a286a4df
commit 34a670eaec
72 changed files with 5589 additions and 1932 deletions

View File

@@ -0,0 +1,373 @@
// file: ks-config/src/composition.rs
// version: 1
//! Binary composition configuration and resolution into the shared runtime profile contract.
const COMPOSITION_JSON_SCHEMA: &str =
include_str!("../../config/schemas/composition.config.schema.json");
/// Root composition document used by a binary to select shared configuration profiles.
#[derive(Clone, Debug, serde::Deserialize, Eq, PartialEq, serde::Serialize)]
pub struct CompositionConfigDocument {
/// Active binary composition profile name.
pub active_profile: std::string::String,
/// Shared configuration documents referenced by this composition.
pub sources: CompositionConfigSources,
/// Named binary composition profiles.
pub profiles: std::vec::Vec<CompositionProfileConfig>,
}
/// Paths of shared configuration documents consumed by one binary composition.
#[derive(Clone, Debug, serde::Deserialize, Eq, PartialEq, serde::Serialize)]
pub struct CompositionConfigSources {
/// Logging configuration document path relative to the workspace root unless absolute.
pub logging: std::string::String,
/// Transport configuration document path relative to the workspace root unless absolute.
pub transport: std::string::String,
/// Listener configuration document path relative to the workspace root unless absolute.
pub listeners: std::string::String,
}
/// Named composition profile referencing shared configuration profiles.
#[derive(Clone, Debug, serde::Deserialize, Eq, PartialEq, serde::Serialize)]
pub struct CompositionProfileConfig {
/// Composition profile code.
pub name: std::string::String,
/// Application metadata retained until the remaining configuration split is completed.
pub app: crate::AppSectionConfig,
/// Logging profile selected from the referenced logging document.
pub logging_profile: std::string::String,
/// Transport profile selected from the referenced transport document.
pub transport_profile: std::string::String,
/// Listener profile selected from the referenced listener document.
pub listeners_profile: std::string::String,
/// Database configuration retained until the store split prerelease.
pub database: crate::DatabaseConfig,
/// Local data directories retained until their owning configuration files are split.
pub data: crate::DataConfig,
/// Wallet configuration retained until the wallet split prerelease.
pub wallet: crate::WalletConfig,
/// Execution policy retained until the execution split prerelease.
pub execution: crate::ExecutionConfig,
/// Desktop demo flags retained until the binary-specific split prerelease.
pub demo: crate::DemoConfig,
}
/// Returns the embedded binary composition JSON Schema text.
pub fn composition_json_schema_text() -> &'static str {
return COMPOSITION_JSON_SCHEMA;
}
/// Parses the embedded binary composition JSON Schema into a JSON value.
pub fn composition_json_schema_value() -> ks_core::Result<serde_json::Value> {
let schema_result = serde_json::from_str::<serde_json::Value>(COMPOSITION_JSON_SCHEMA);
return match schema_result {
std::result::Result::Ok(schema) => std::result::Result::Ok(schema),
std::result::Result::Err(error) => std::result::Result::Err(ks_core::Error::new(
"composition_config_schema_parse_failed",
error.to_string(),
)),
};
}
/// Validates raw binary composition JSON against its embedded schema.
pub fn validate_composition_json_schema(raw_json: &str) -> ks_core::Result<()> {
let schema = match composition_json_schema_value() {
std::result::Result::Ok(schema) => schema,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let instance = match serde_json::from_str::<serde_json::Value>(raw_json) {
std::result::Result::Ok(instance) => instance,
std::result::Result::Err(error) => {
return std::result::Result::Err(ks_core::Error::new(
"composition_config_json_parse_failed",
error.to_string(),
));
},
};
let validator = match jsonschema::validator_for(&schema) {
std::result::Result::Ok(validator) => validator,
std::result::Result::Err(error) => {
return std::result::Result::Err(ks_core::Error::new(
"composition_config_schema_compile_failed",
error.to_string(),
));
},
};
return match validator.validate(&instance) {
std::result::Result::Ok(()) => std::result::Result::Ok(()),
std::result::Result::Err(error) => std::result::Result::Err(ks_core::Error::new(
"composition_config_schema_validation_failed",
error.to_string(),
)),
};
}
/// Parses and validates one binary composition document.
pub fn parse_composition_json(raw_json: &str) -> ks_core::Result<CompositionConfigDocument> {
match validate_composition_json_schema(raw_json) {
std::result::Result::Ok(()) => (),
std::result::Result::Err(error) => return std::result::Result::Err(error),
}
let document = match serde_json::from_str::<CompositionConfigDocument>(raw_json) {
std::result::Result::Ok(document) => document,
std::result::Result::Err(error) => {
return std::result::Result::Err(ks_core::Error::new(
"composition_config_json_decode_failed",
error.to_string(),
));
},
};
return match validate_composition_document(&document) {
std::result::Result::Ok(()) => std::result::Result::Ok(document),
std::result::Result::Err(error) => std::result::Result::Err(error),
};
}
/// Reads and parses one binary composition document from a filesystem path.
pub fn read_composition_json_file(
path: &std::path::Path,
) -> ks_core::Result<CompositionConfigDocument> {
let raw_json = match std::fs::read_to_string(path) {
std::result::Result::Ok(content) => content,
std::result::Result::Err(error) => {
return std::result::Result::Err(ks_core::Error::new(
"composition_config_file_read_failed",
error.to_string(),
));
},
};
return parse_composition_json(&raw_json);
}
/// Loads workspace environment values, resolves placeholders and parses one binary composition document.
pub fn read_composition_json_file_with_environment(
path: &std::path::Path,
workspace_root: &std::path::Path,
) -> ks_core::Result<CompositionConfigDocument> {
match crate::load_workspace_environment(workspace_root) {
std::result::Result::Ok(_) => (),
std::result::Result::Err(error) => return std::result::Result::Err(error),
}
let raw_json = match std::fs::read_to_string(path) {
std::result::Result::Ok(content) => content,
std::result::Result::Err(error) => {
return std::result::Result::Err(ks_core::Error::new(
"composition_config_file_read_failed",
error.to_string(),
));
},
};
let resolved = crate::resolve_environment_placeholders(&raw_json);
return parse_composition_json(&resolved);
}
/// Returns one named binary composition profile.
pub fn composition_profile<'a>(
document: &'a CompositionConfigDocument,
profile_name: &str,
) -> ks_core::Result<&'a CompositionProfileConfig> {
for profile in &document.profiles {
if profile.name == profile_name {
return std::result::Result::Ok(profile);
}
}
return std::result::Result::Err(ks_core::Error::new(
"composition_profile_not_found",
profile_name.to_string(),
));
}
/// Returns the active binary composition profile.
pub fn active_composition_profile(
document: &CompositionConfigDocument,
) -> ks_core::Result<&CompositionProfileConfig> {
return composition_profile(document, document.active_profile.as_str());
}
/// Resolves a source path relative to the workspace root unless already absolute.
pub fn resolve_composition_source_path(
workspace_root: &std::path::Path,
configured_path: &str,
) -> std::path::PathBuf {
let path = std::path::PathBuf::from(configured_path);
if path.is_absolute() {
return path;
}
return workspace_root.join(path);
}
/// Resolves all shared transport/listener selections into the existing runtime application contract.
pub fn compose_app_config(
document: &CompositionConfigDocument,
transport: &crate::TransportConfigDocument,
listeners: &crate::ListenersConfigDocument,
) -> ks_core::Result<crate::AppConfig> {
let mut profiles = std::vec::Vec::<crate::ProfileConfig>::new();
for profile in &document.profiles {
let transport_profile =
match crate::transport_profile(transport, profile.transport_profile.as_str()) {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let listeners_profile =
match crate::listeners_profile(listeners, profile.listeners_profile.as_str()) {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
profiles.push(crate::ProfileConfig {
name: profile.name.clone(),
app: profile.app.clone(),
database: profile.database.clone(),
data: profile.data.clone(),
solana: crate::SolanaConfig {
http_endpoints: transport_profile.http_endpoints.clone(),
ws_endpoints: transport_profile.ws_endpoints.clone(),
listeners: crate::resolved_listener_config(listeners_profile),
},
wallet: profile.wallet.clone(),
execution: profile.execution.clone(),
demo: profile.demo.clone(),
});
}
let config = crate::AppConfig {
active_profile: document.active_profile.clone(),
profiles,
};
return match crate::validate_config(&config) {
std::result::Result::Ok(()) => std::result::Result::Ok(config),
std::result::Result::Err(error) => std::result::Result::Err(error),
};
}
/// Reads a binary composition and its referenced transport/listener documents into the resolved runtime contract.
pub fn read_composed_app_config_with_environment(
composition_path: &std::path::Path,
workspace_root: &std::path::Path,
) -> ks_core::Result<crate::ComposedAppConfig> {
let composition =
match read_composition_json_file_with_environment(composition_path, workspace_root) {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let transport_path =
resolve_composition_source_path(workspace_root, composition.sources.transport.as_str());
let listeners_path =
resolve_composition_source_path(workspace_root, composition.sources.listeners.as_str());
let transport =
match crate::read_transport_json_file_with_environment(&transport_path, workspace_root) {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let listeners =
match crate::read_listeners_json_file_with_environment(&listeners_path, workspace_root) {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let app_config = match compose_app_config(&composition, &transport, &listeners) {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
return std::result::Result::Ok(crate::ComposedAppConfig {
composition,
app_config,
transport_path,
listeners_path,
});
}
/// Resolved application configuration and the source documents needed by a binary.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ComposedAppConfig {
/// Parsed binary composition document.
pub composition: CompositionConfigDocument,
/// Resolved runtime application configuration.
pub app_config: crate::AppConfig,
/// Resolved transport source path.
pub transport_path: std::path::PathBuf,
/// Resolved listener source path.
pub listeners_path: std::path::PathBuf,
}
/// Validates a typed binary composition document before referenced documents are loaded.
pub fn validate_composition_document(document: &CompositionConfigDocument) -> ks_core::Result<()> {
match crate::require_non_empty(&document.active_profile, "active_profile") {
std::result::Result::Ok(()) => (),
std::result::Result::Err(error) => return std::result::Result::Err(error),
}
for source in [
&document.sources.logging,
&document.sources.transport,
&document.sources.listeners,
] {
match crate::require_non_empty(source, "composition.sources") {
std::result::Result::Ok(()) => (),
std::result::Result::Err(error) => return std::result::Result::Err(error),
}
}
if document.profiles.is_empty() {
return std::result::Result::Err(ks_core::Error::new(
"composition_profiles_empty",
"at least one composition profile is required",
));
}
let mut names = std::collections::BTreeSet::<std::string::String>::new();
for profile in &document.profiles {
match crate::require_non_empty(&profile.name, "composition.profile.name") {
std::result::Result::Ok(()) => (),
std::result::Result::Err(error) => return std::result::Result::Err(error),
}
for reference in
[&profile.logging_profile, &profile.transport_profile, &profile.listeners_profile]
{
match crate::require_non_empty(reference, "composition.profile.reference") {
std::result::Result::Ok(()) => (),
std::result::Result::Err(error) => return std::result::Result::Err(error),
}
}
if !names.insert(profile.name.clone()) {
return std::result::Result::Err(ks_core::Error::new(
"composition_profile_name_duplicate",
profile.name.clone(),
));
}
}
return match active_composition_profile(document) {
std::result::Result::Ok(_) => std::result::Result::Ok(()),
std::result::Result::Err(error) => std::result::Result::Err(error),
};
}
#[cfg(test)]
mod tests {
const DEFAULT_COMPOSITION: &str =
include_str!("../../config/kb-app-demo-desktop.default.config.json");
const EXAMPLE_COMPOSITION: &str =
include_str!("../../config/example.kb-app-demo-desktop.default.config.json");
const DEFAULT_TRANSPORT: &str = include_str!("../../config/transport.config.json");
const DEFAULT_LISTENERS: &str = include_str!("../../config/listeners.config.json");
#[test]
fn default_and_example_compositions_validate() {
assert!(super::parse_composition_json(DEFAULT_COMPOSITION).is_ok());
assert!(super::parse_composition_json(EXAMPLE_COMPOSITION).is_ok());
}
#[test]
fn default_composition_resolves_shared_transport_and_listener_profiles() {
let composition = match super::parse_composition_json(DEFAULT_COMPOSITION) {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => panic!("default composition must parse: {error}"),
};
let transport = match crate::parse_transport_json(DEFAULT_TRANSPORT) {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => panic!("default transport must parse: {error}"),
};
let listeners = match crate::parse_listeners_json(DEFAULT_LISTENERS) {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => panic!("default listeners must parse: {error}"),
};
let resolved = super::compose_app_config(&composition, &transport, &listeners);
assert!(resolved.is_ok());
}
}