mod admit;
mod assets;
mod cli;
mod download;
mod engine;
mod error;
mod evaluate;
mod export;
mod flowlog;
mod http;
mod jobs;
mod limits;
mod model_plan;
mod queue;
mod results;
mod run;
mod security;
mod server;
mod state;
pub(crate) use cli::ServeArgs;
pub(crate) use run::run as serve_main;
#[cfg(test)]
mod tests;
#[cfg(test)]
mod dependency_boundary {
use std::path::{Path, PathBuf};
const ONLY_HTTP_MODULE: &str = "http.rs";
#[test]
fn the_http_dependency_lives_only_in_the_http_module() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
let directory = root.join("src/bin/serve");
let mut sources = Vec::new();
collect_sources(&directory, &mut sources);
assert!(
sources.len() >= 14,
"expected the M1 modules, found {sources:?}"
);
let needle = ["tiny", "_http"].concat();
for path in &sources {
let text = std::fs::read_to_string(path).expect("a serve source file must be readable");
let code = strip_line_comments(&text);
if path
.file_name()
.is_some_and(|name| name == ONLY_HTTP_MODULE)
{
assert!(
code.contains(&needle),
"the HTTP module must be the one that uses the HTTP library"
);
continue;
}
assert!(
!code.contains(&needle),
"only serve/{ONLY_HTTP_MODULE} may reference the HTTP library: {} does",
path.display()
);
}
let mut library_sources = Vec::new();
collect_sources(&root.join("src"), &mut library_sources);
for path in &library_sources {
if path.starts_with(root.join("src/bin")) {
continue;
}
let text = std::fs::read_to_string(path).expect("a library source must be readable");
assert!(
!strip_line_comments(&text).contains(&needle),
"the library must stay HTTP-free: {} references it",
path.display()
);
}
}
#[test]
fn the_http_dependency_is_optional_and_outside_the_default_feature() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
let manifest = std::fs::read_to_string(root.join("Cargo.toml"))
.expect("the crate manifest must be readable");
let name = ["tiny", "_http"].concat();
let mut in_dependencies = false;
let mut dependency = None;
let mut default_features = None;
let mut serve_feature = None;
for line in manifest.lines() {
let trimmed = line.trim();
if trimmed.starts_with('[') {
in_dependencies = trimmed == "[dependencies]";
continue;
}
if in_dependencies && !trimmed.starts_with('#') && trimmed.starts_with(&name) {
dependency = Some(trimmed.to_string());
}
if let Some(value) = trimmed.strip_prefix("default =") {
default_features = Some(value.to_string());
}
if let Some(value) = trimmed.strip_prefix("serve =") {
serve_feature = Some(value.to_string());
}
}
let dependency = dependency.expect("the HTTP library must be a dependency");
assert!(
dependency.contains("optional = true"),
"the HTTP library must be optional: {dependency}"
);
let default_features = default_features.expect("a `default` feature list must exist");
assert!(
!default_features.contains(&name),
"the HTTP library must not be in `default`: {default_features}"
);
let serve_feature = serve_feature.expect("a `serve` feature must exist");
assert!(
serve_feature.contains(&format!("dep:{name}")),
"only the `serve` feature may enable the HTTP library: {serve_feature}"
);
}
fn strip_line_comments(text: &str) -> String {
text.lines()
.map(|line| match line.find("//") {
Some(index) => &line[..index],
None => line,
})
.collect::<Vec<_>>()
.join("\n")
}
fn collect_sources(directory: &Path, out: &mut Vec<PathBuf>) {
for entry in std::fs::read_dir(directory).expect("the directory must exist") {
let path = entry.expect("readable entry").path();
if path.is_dir() {
collect_sources(&path, out);
} else if path.extension().is_some_and(|extension| extension == "rs") {
out.push(path);
}
}
}
}