Skip to content

Commit 786e803

Browse files
committed
Move summarize function to runtime config crate
Signed-off-by: Ryan Levick <[email protected]>
1 parent 7149cf0 commit 786e803

File tree

9 files changed

+46
-119
lines changed

9 files changed

+46
-119
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/runtime-config/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ rust-version.workspace = true
1010

1111
[dependencies]
1212
anyhow = { workspace = true }
13+
spin-common = { path = "../common" }
1314
spin-factor-key-value = { path = "../factor-key-value" }
1415
spin-key-value-azure = { path = "../key-value-azure" }
1516
spin-key-value-redis = { path = "../key-value-redis" }

crates/runtime-config/src/lib.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::path::{Path, PathBuf};
22

33
use anyhow::Context as _;
4+
use spin_common::ui::quoted_path;
45
use spin_factor_key_value::runtime_config::spin::{self as key_value};
56
use spin_factor_key_value::{DefaultLabelResolver as _, KeyValueFactor};
67
use spin_factor_llm::{spin as llm, LlmFactor};
@@ -19,9 +20,9 @@ use spin_factors::{
1920
runtime_config::toml::TomlKeyTracker, FactorRuntimeConfigSource, RuntimeConfigSourceFinalizer,
2021
};
2122
use spin_key_value_spin::{SpinKeyValueRuntimeConfig, SpinKeyValueStore};
22-
use spin_runtime_factors::TriggerFactorsRuntimeConfig;
2323
use spin_sqlite as sqlite;
2424
use spin_trigger::cli::UserProvidedPath;
25+
use toml::Value;
2526

2627
/// The default state directory for the trigger.
2728
pub const DEFAULT_STATE_DIR: &str = ".spin";
@@ -48,6 +49,41 @@ pub struct ResolvedRuntimeConfig<T> {
4849
pub toml: toml::Table,
4950
}
5051

52+
impl<T> ResolvedRuntimeConfig<T> {
53+
pub fn summarize(&self, runtime_config_path: Option<&Path>) {
54+
let summarize_labeled_typed_tables = |key| {
55+
let mut summaries = vec![];
56+
if let Some(tables) = self.toml.get(key).and_then(Value::as_table) {
57+
for (label, config) in tables {
58+
if let Some(ty) = config.get("type").and_then(Value::as_str) {
59+
summaries.push(format!("[{key}.{label}: {ty}]"))
60+
}
61+
}
62+
}
63+
summaries
64+
};
65+
66+
let mut summaries = vec![];
67+
// [key_value_store.<label>: <type>]
68+
summaries.extend(summarize_labeled_typed_tables("key_value_store"));
69+
// [sqlite_database.<label>: <type>]
70+
summaries.extend(summarize_labeled_typed_tables("sqlite_database"));
71+
// [llm_compute: <type>]
72+
if let Some(table) = self.toml.get("llm_compute").and_then(Value::as_table) {
73+
if let Some(ty) = table.get("type").and_then(Value::as_str) {
74+
summaries.push(format!("[llm_compute: {ty}"));
75+
}
76+
}
77+
if !summaries.is_empty() {
78+
let summaries = summaries.join(", ");
79+
let from_path = runtime_config_path
80+
.map(|path| format!("from {}", quoted_path(path)))
81+
.unwrap_or_default();
82+
println!("Using runtime config {summaries} {from_path}");
83+
}
84+
}
85+
}
86+
5187
impl<T> ResolvedRuntimeConfig<T>
5288
where
5389
T: for<'a, 'b> TryFrom<TomlRuntimeConfigSource<'a, 'b>>,
@@ -95,8 +131,6 @@ where
95131
let runtime_config_dir = runtime_config_path
96132
.and_then(Path::parent)
97133
.map(ToOwned::to_owned);
98-
let toml_resolver =
99-
TomlResolver::new(&toml, local_app_dir, provided_state_dir, provided_log_dir);
100134
let tls_resolver = runtime_config_dir.clone().map(SpinTlsRuntimeConfig::new);
101135
let key_value_config_resolver =
102136
key_value_config_resolver(runtime_config_dir, toml_resolver.state_dir()?);

crates/runtime-factors/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ spin-factor-wasi = { path = "../factor-wasi" }
3131
spin-factors = { path = "../factors" }
3232
spin-factors-executor = { path = "../factors-executor" }
3333
spin-runtime-config = { path = "../runtime-config" }
34-
spin-trigger = { path = "../trigger" }
3534
spin-telemetry = { path = "../telemetry" }
35+
spin-trigger = { path = "../trigger" }
3636
terminal = { path = "../terminal" }
37-
toml = "0.8"
3837
tracing = { workspace = true }
3938

4039
[lints]

crates/runtime-factors/src/build.rs

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
use std::path::{Path, PathBuf};
1+
use std::path::PathBuf;
22

33
use super::{TriggerAppArgs, TriggerFactors, TriggerFactorsRuntimeConfig};
44

55
use anyhow::Context as _;
6-
use spin_common::ui::quoted_path;
76
use spin_factors_executor::FactorsExecutor;
87
use spin_runtime_config::ResolvedRuntimeConfig;
98
use spin_trigger::cli::{
109
FactorsConfig, InitialKvSetterHook, KeyValueDefaultStoreSummaryHook, RuntimeFactorsBuilder,
1110
SqlStatementExecutorHook, SqliteDefaultStoreSummaryHook, StdioLoggingExecutorHooks,
1211
};
13-
use toml::Value;
1412

1513
/// A [`RuntimeFactorsBuilder`] for [`TriggerFactors`].
1614
pub struct FactorsBuilder;
@@ -34,7 +32,7 @@ impl RuntimeFactorsBuilder for FactorsBuilder {
3432
use_gpu,
3533
)?;
3634

37-
summarize_runtime_config(&runtime_config, config.runtime_config_file.as_deref());
35+
runtime_config.summarize(config.runtime_config_file.as_deref());
3836

3937
let factors = TriggerFactors::new(
4038
runtime_config.state_dir(),
@@ -67,40 +65,3 @@ impl RuntimeFactorsBuilder for FactorsBuilder {
6765
Ok(())
6866
}
6967
}
70-
71-
fn summarize_runtime_config<T>(
72-
runtime_config: &ResolvedRuntimeConfig<T>,
73-
runtime_config_path: Option<&Path>,
74-
) {
75-
let toml = &runtime_config.toml;
76-
let summarize_labeled_typed_tables = |key| {
77-
let mut summaries = vec![];
78-
if let Some(tables) = toml.get(key).and_then(Value::as_table) {
79-
for (label, config) in tables {
80-
if let Some(ty) = config.get("type").and_then(Value::as_str) {
81-
summaries.push(format!("[{key}.{label}: {ty}]"))
82-
}
83-
}
84-
}
85-
summaries
86-
};
87-
88-
let mut summaries = vec![];
89-
// [key_value_store.<label>: <type>]
90-
summaries.extend(summarize_labeled_typed_tables("key_value_store"));
91-
// [sqlite_database.<label>: <type>]
92-
summaries.extend(summarize_labeled_typed_tables("sqlite_database"));
93-
// [llm_compute: <type>]
94-
if let Some(table) = toml.get("llm_compute").and_then(Value::as_table) {
95-
if let Some(ty) = table.get("type").and_then(Value::as_str) {
96-
summaries.push(format!("[llm_compute: {ty}"));
97-
}
98-
}
99-
if !summaries.is_empty() {
100-
let summaries = summaries.join(", ");
101-
let from_path = runtime_config_path
102-
.map(|path| format!("from {}", quoted_path(path)))
103-
.unwrap_or_default();
104-
println!("Using runtime config {summaries} {from_path}");
105-
}
106-
}

crates/trigger-http/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ serde = { version = "1.0", features = ["derive"] }
2626
serde_json = "1"
2727
spin-app = { path = "../app" }
2828
spin-core = { path = "../core" }
29-
spin-factors = { path = "../factors" }
3029
spin-factor-outbound-http = { path = "../factor-outbound-http" }
3130
spin-factor-wasi = { path = "../factor-wasi" }
31+
spin-factors = { path = "../factors" }
3232
spin-http = { path = "../http" }
3333
spin-factor-outbound-networking = { path = "../factor-outbound-networking" }
3434
spin-telemetry = { path = "../telemetry" }

crates/trigger-redis/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ async-trait = "0.1"
1313
futures = "0.3"
1414
redis = { version = "0.26.1", features = ["tokio-comp"] }
1515
serde = "1.0.188"
16-
spin-factors = { path = "../factors" }
1716
spin-factor-variables = { path = "../factor-variables" }
17+
spin-factors = { path = "../factors" }
1818
spin-telemetry = { path = "../telemetry" }
1919
spin-trigger = { path = "../trigger" }
2020
spin-world = { path = "../world" }

examples/spin-timer/Cargo.lock

Lines changed: 1 addition & 69 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/spin-timer/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ anyhow = "1.0.68"
99
clap = { version = "3.1.15", features = ["derive", "env"] }
1010
futures = "0.3.25"
1111
serde = "1.0.188"
12+
spin-factors = { path = "../../crates/factors" }
1213
spin-runtime-factors = { path = "../../crates/runtime-factors" }
1314
spin-trigger = { path = "../../crates/trigger" }
14-
spin-factors = { path = "../../crates/factors" }
1515
tokio = { version = "1.11", features = ["full"] }
1616
tokio-scoped = "0.2.0"
1717
wasmtime = "22.0.0"

0 commit comments

Comments
 (0)