use crate::{AppError, AppResult, Application, PreparedApplication};
use appcore_contracts::{
ApplicationId, ApplicationManifestV1, DeploymentManifestV1, InstallationId, NetworkConfig,
ProviderConfig, ProviderId, RuntimeMode, RuntimeRequirements, ServiceId,
};
use appcore_core::AppId;
use appcore_core::NodeId;
use appcore_log::{ConfiguredLogger, LogBuilder, LoggerConfig};
use std::time::{SystemTime, UNIX_EPOCH};
const DEFAULT_APPLICATION_VERSION: &str = "0.1.0";
const DEFAULT_RUNTIME_VERSION: &str = "1.0.0";
const DEFAULT_PROTOCOL_VERSION: &str = "1";
const DEFAULT_VENDOR: &str = "local";
const DEFAULT_STORAGE_PROVIDER: &str = "file";
const DEFAULT_TRANSPORT_PROVIDER: &str = "http";
pub struct App {
id: AppId,
application_manifest: ApplicationManifestV1,
deployment_manifest: DeploymentManifestV1,
logger: ConfiguredLogger,
logger_component: String,
}
impl std::fmt::Debug for App {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("App")
.field("id", &self.id)
.field("application_manifest", &self.application_manifest)
.field("deployment_manifest", &self.deployment_manifest)
.finish_non_exhaustive()
}
}
impl App {
pub fn new(name: &str) -> AppResult<Self> {
let id = AppId::new(name)?;
let application_manifest = default_application_manifest(name)?;
let deployment_manifest = default_deployment_manifest(name)?;
Self::from_manifests(id, application_manifest, deployment_manifest)
}
pub fn application_manifest(mut self, manifest: ApplicationManifestV1) -> AppResult<Self> {
manifest.validate()?;
self.application_manifest = manifest;
self.ensure_manifest_identity()?;
Ok(self)
}
pub fn deployment_manifest(mut self, manifest: DeploymentManifestV1) -> AppResult<Self> {
manifest.validate()?;
self.deployment_manifest = manifest;
self.ensure_manifest_identity()?;
Ok(self)
}
pub fn effective_application_manifest(&self) -> &ApplicationManifestV1 {
&self.application_manifest
}
pub fn effective_deployment_manifest(&self) -> &DeploymentManifestV1 {
&self.deployment_manifest
}
pub fn id(&self) -> &AppId {
&self.id
}
pub fn logging(mut self, config: LoggerConfig) -> AppResult<Self> {
self.logger = config.build()?;
Ok(self)
}
pub fn log(&self, message: &str) {
self.logger().info(message);
}
pub fn logger(&self) -> LogBuilder<'_> {
self.logger
.dispatcher()
.event(now_ms(), self.logger_component.as_str())
}
pub fn prepare<A>(&self, application: &A, node_id: NodeId) -> AppResult<PreparedApplication>
where
A: Application + ?Sized,
{
PreparedApplication::new(
self.application_manifest.clone(),
&self.deployment_manifest,
application,
node_id,
)
}
#[cfg(feature = "deployment")]
pub fn prepare_with_deployment<A>(
&self,
application: &A,
node_id: NodeId,
deployment: &crate::DeploymentContext,
) -> AppResult<PreparedApplication>
where
A: Application + ?Sized,
{
application.configure(deployment)?;
self.prepare(application, node_id)
}
pub fn dump_crash_log(&self) -> AppResult<usize> {
Ok(self.logger.dump_crash()?)
}
pub fn run<F>(self, configure: F) -> AppResult<()>
where
F: FnOnce(&App) -> AppResult<()>,
{
configure(&self)
}
fn from_manifests(
id: AppId,
application_manifest: ApplicationManifestV1,
deployment_manifest: DeploymentManifestV1,
) -> AppResult<Self> {
let logger_component = format!("application.{}", id.as_str());
let app = Self {
id,
application_manifest,
deployment_manifest,
logger: LoggerConfig::default().build()?,
logger_component,
};
app.ensure_manifest_identity()?;
Ok(app)
}
fn ensure_manifest_identity(&self) -> AppResult<()> {
if self.application_manifest.application_id() != self.deployment_manifest.application_id()
|| self.application_manifest.application_id().as_str() != self.id.as_str()
{
return Err(AppError::ManifestIdentityMismatch);
}
Ok(())
}
}
pub fn run<F>(name: &str, configure: F) -> AppResult<()>
where
F: FnOnce(&App) -> AppResult<()>,
{
App::new(name)?.run(configure)
}
fn default_application_manifest(name: &str) -> AppResult<ApplicationManifestV1> {
Ok(ApplicationManifestV1::new(
ApplicationId::new(name)?,
DEFAULT_APPLICATION_VERSION,
name,
DEFAULT_VENDOR,
ServiceId::new(format!("{name}.local"))?,
RuntimeRequirements::new(DEFAULT_RUNTIME_VERSION, DEFAULT_PROTOCOL_VERSION)?,
)?)
}
fn default_deployment_manifest(name: &str) -> AppResult<DeploymentManifestV1> {
Ok(DeploymentManifestV1::builder(
InstallationId::new(format!("{name}-local"))?,
ApplicationId::new(name)?,
RuntimeMode::Standalone,
ProviderConfig::new(ProviderId::new(DEFAULT_STORAGE_PROVIDER)?),
NetworkConfig::new(
ProviderId::new(DEFAULT_TRANSPORT_PROVIDER)?,
ProviderId::new(DEFAULT_TRANSPORT_PROVIDER)?,
),
)
.build()?)
}
fn now_ms() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|duration| duration.as_millis().try_into().unwrap_or(u64::MAX))
.unwrap_or_default()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn zero_config_uses_validated_standalone_manifests() {
let app = App::new("hello-sdk").expect("defaults are valid");
assert_eq!(
app.effective_deployment_manifest().mode(),
RuntimeMode::Standalone
);
app.effective_application_manifest()
.validate()
.expect("application manifest validates");
app.effective_deployment_manifest()
.validate()
.expect("deployment manifest validates");
}
#[test]
fn application_accepts_disabled_logging_without_a_sink() {
let app = App::new("quiet-sdk")
.unwrap()
.logging(LoggerConfig {
output: appcore_log::LogOutputMode::Disabled,
..LoggerConfig::default()
})
.unwrap();
app.log("this performs no sink work");
assert!(app.logger.dump_crash().is_ok_and(|written| written == 0));
}
#[test]
fn callback_receives_validated_identity() {
run("hello-sdk", |app| {
assert_eq!(app.id().as_str(), "hello-sdk");
Ok(())
})
.expect("run succeeds");
}
#[test]
fn invalid_identity_is_returned() {
assert!(run("", |_| Ok(())).is_err());
}
#[test]
fn explicit_manifest_identity_mismatch_is_not_masked_by_defaults() {
let other = App::new("other-sdk").expect("other defaults");
let error = App::new("hello-sdk")
.expect("hello defaults")
.application_manifest(other.effective_application_manifest().clone())
.expect_err("explicit application identity must win and fail");
assert_eq!(error, AppError::ManifestIdentityMismatch);
}
#[test]
fn explicit_deployment_replaces_the_standalone_default() {
let deployment = DeploymentManifestV1::builder(
InstallationId::new("hello-sdk-explicit").expect("installation identity"),
ApplicationId::new("hello-sdk").expect("application identity"),
RuntimeMode::Standalone,
ProviderConfig::new(ProviderId::new("memory").expect("storage provider")),
NetworkConfig::new(
ProviderId::new("loopback").expect("peer transport"),
ProviderId::new("loopback").expect("command transport"),
),
)
.build()
.expect("explicit deployment");
let app = App::new("hello-sdk")
.expect("target defaults")
.deployment_manifest(deployment)
.expect("matching explicit deployment");
assert_eq!(
app.effective_deployment_manifest()
.installation_id()
.as_str(),
"hello-sdk-explicit"
);
assert_eq!(
app.effective_deployment_manifest().mode(),
RuntimeMode::Standalone
);
}
}