logicaffeine_cli/lib.rs
1#![doc = include_str!("../README.md")]
2
3pub mod browser;
4pub mod cli;
5pub(crate) mod closure;
6pub(crate) mod commands;
7
8/// The content type largo serves a path as — the table `largo serve` and `largo build` both use.
9///
10/// Public so `mime_single_source_lock` asks the REAL table rather than a copy, and can hold it
11/// against `static.lg`'s `contentTypeFor`, which is what a DEPLOYED LOGOS program answers with.
12/// The two had already drifted (`text/css` against `text/css; charset=utf-8`, and six types
13/// `static.lg` did not know at all), which means a file typed correctly in development and
14/// wrongly in production.
15pub fn content_type_for(path: &str) -> &'static str {
16 commands::serve_web::content_type_for(path)
17}
18
19/// Which stylesheets a page links — the ONE rule `largo serve` and `largo build` both apply.
20///
21/// Public so `mime_single_source_lock` holds the real convention rather than a copy: it was
22/// written twice before, so a page could be styled in development and unstyled in production.
23pub fn stylesheet_links_for<'a>(paths: impl Iterator<Item = &'a str>) -> String {
24 commands::serve_web::stylesheet_links_for(paths)
25}
26
27/// The URLs a page answers — the ONE rule `largo serve` and `largo build` both follow.
28///
29/// Public so `mime_single_source_lock` holds the real namespace rather than a copy. Written
30/// twice, they had already diverged: the dev server answered `/index.html` and the built site
31/// did not publish it, so a link that worked all through development 404'd the moment the site
32/// shipped.
33pub fn page_html_paths(stem: &str) -> Vec<String> {
34 commands::web_bundle::page_html_paths(stem)
35}
36
37/// The document `largo serve` sends for a page — the same pre-rendered markup `largo build`
38/// ships, so what a developer looks at is what their visitors get.
39///
40/// Public so `mime_single_source_lock` holds dev and prod against each other through the REAL
41/// functions. They diverged before: the build pre-rendered the content and the dev server served
42/// the bare shell.
43pub fn dev_page_html(source: &str, stem: &str) -> String {
44 commands::web_bundle::prerendered_html_pub(source, stem)
45}
46
47/// A built site's planned assets as `(path, content type, bytes)` — the REAL table `largo build`
48/// bakes into the binary, exposed so `asset_cache_lock` holds the shipped names and headers rather
49/// than a copy of the rule.
50pub fn plan_site_assets(
51 pages_dir: &std::path::Path,
52 static_dir: &std::path::Path,
53) -> Result<Vec<(String, String, Vec<u8>)>, String> {
54 commands::web_bundle::plan_site(pages_dir, static_dir)
55 .map(|assets| {
56 assets
57 .into_iter()
58 .map(|a| (a.path, a.content_type, a.bytes))
59 .collect()
60 })
61 .map_err(|e| e.to_string())
62}
63
64/// Does a concrete request path match a page's declared `## Page` path? A `:name` segment
65/// matches any one segment — the same rule `web.lg`'s `matchRoute` applies inside the page,
66/// exposed so `page_pattern_routing_lock` holds the REAL matcher the router uses.
67pub fn page_path_matches(pattern: &str, request: &str) -> bool {
68 commands::web_bundle::page_path_matches(pattern, request)
69}
70
71/// The URL-and-filesystem-safe form of a page path (`/posts/:id` → `/posts/id`) — the shipped
72/// name, exposed so the lock holds the published bytes rather than a copy of the rule.
73pub fn page_asset_base(path: &str) -> String {
74 commands::web_bundle::page_asset_base(path)
75}
76
77/// gzip some bytes the way `largo build` does, or `None` when a compressed form is not
78/// worth shipping — exposed so `asset_compression_lock` measures the REAL compressor on a
79/// REAL asset rather than a copy of the rule.
80pub fn gzip_asset(bytes: &[u8]) -> Option<Vec<u8>> {
81 commands::web_bundle::gzip_asset(bytes)
82}
83
84/// The generated `logos_assets` module a built site bakes into its binary — the REAL table, so a
85/// lock holds the headers that actually ship rather than the rule that computes them.
86pub fn assets_module(
87 pages_dir: &std::path::Path,
88 static_dir: &std::path::Path,
89) -> Result<String, String> {
90 let patterns = commands::web_bundle::page_patterns(pages_dir);
91 commands::web_bundle::plan_site(pages_dir, static_dir)
92 .map(|assets| commands::web_bundle::site_assets_module(pages_dir, &assets, &patterns))
93 .map_err(|e| e.to_string())
94}
95
96/// The short content digest an asset's file name carries — see `web_bundle::content_hash`.
97pub fn content_hash(bytes: &[u8]) -> String {
98 commands::web_bundle::content_hash(bytes)
99}
100
101/// An asset's quoted `ETag`.
102pub fn etag_for(bytes: &[u8]) -> String {
103 commands::web_bundle::etag_for(bytes)
104}
105
106/// The `Cache-Control` a path is served with — immutable for a content-addressed name, revalidated
107/// for a name people link to.
108pub fn cache_control_for(path: &str) -> String {
109 commands::web_bundle::cache_control_for(path)
110}
111
112/// Answer a conditional request: `(status, body length)` for an ETag and a client's
113/// `If-None-Match`.
114pub fn conditional_response(etag: &str, if_none_match: Option<&str>, len: usize) -> (u16, usize) {
115 commands::web_bundle::conditional_response(etag, if_none_match, len)
116}
117
118/// A built page's document with its markup already in it — the static-render half of
119/// `largo build`. Public so `ssg_lock` exercises the REAL function the build calls, rather than a
120/// copy that could drift from it.
121pub fn prerender_page_html(source: &str, stem: &str) -> String {
122 commands::web_bundle::prerendered_html_pub(source, stem)
123}
124pub mod compile;
125pub mod dap;
126pub(crate) mod diagnostics;
127pub use diagnostics::parse_error_report;
128pub mod mcp;
129pub mod project;
130pub(crate) mod repl;
131pub mod ui;
132
133/// Re-export of the LOGOS interface types from the kernel crate.
134///
135/// Provides access to core types like `World` and `Entity` for
136/// integration with the LOGOS runtime.
137pub use logicaffeine_kernel::interface;
138
139/// Re-export of analysis utilities from the compile crate.
140///
141/// Useful for tooling that needs to analyze LOGOS source without
142/// performing a full build.
143pub use logicaffeine_compile::analysis;
144
145/// Entry point for the CLI.
146///
147/// Parses command-line arguments and executes the appropriate command.
148/// See [`cli::run_cli`] for details.
149pub use cli::run_cli;
150
151/// The `largo` process: installs the native tier, runs [`run_cli`] on a worker whose stack is sized
152/// from the AST depth limit, renders any error, and exits with its code. `src/main.rs` is exactly
153/// this call, and so is the test crate's `largo-under-test`, so the CLI suites spawn what ships.
154pub fn largo_main() {
155 // The copy-and-patch JIT becomes the process-wide native tier: every
156 // interpreted program the CLI runs gets hot-function and hot-loop
157 // tier-up. Only x86_64 System V (Linux + macOS) links it; on Windows,
158 // aarch64 and WASM the tier is absent and largo runs the bytecode VM.
159 // `LOGOS_NO_JIT=1` skips installation so programs run on the VM bytecode
160 // interpreter alone — a fallback when the JIT miscompiles a hot loop, and
161 // the knob the differential gate uses to compare AOT against the unjitted
162 // reference engine.
163 #[cfg(all(target_arch = "x86_64", not(target_os = "windows")))]
164 logicaffeine_jit::segv_trace_install();
165 #[cfg(all(target_arch = "x86_64", not(target_os = "windows")))]
166 if std::env::var_os("LOGOS_NO_JIT").is_none() {
167 logicaffeine_jit::install();
168 }
169
170 // Run the CLI on a worker whose stack is SIZED FROM the AST depth limit,
171 // so `LOGOS_MAX_AST_DEPTH` genuinely works: raising the limit raises the
172 // stack that honors it (the main thread's stack is fixed by `ulimit -s`
173 // at exec and can't follow the knob). 40 KiB/level covers the fattest
174 // measured walker frame with margin; the pages are virtual until
175 // touched, so a large reservation costs nothing on shallow programs.
176 let stack_bytes = logicaffeine_language::ast_depth::max_ast_depth()
177 .saturating_mul(40 * 1024)
178 .max(16 * 1024 * 1024);
179 let outcome = std::thread::Builder::new()
180 .name("largo".into())
181 .stack_size(stack_bytes)
182 .spawn(|| {
183 // Errors render inside the worker (their type isn't Send);
184 // structured CLI errors carry a hint and their own exit code,
185 // anything else renders as a bare `error:` line and exits 1.
186 match run_cli() {
187 Ok(()) => 0,
188 Err(e) => ui::render_error(e.as_ref()),
189 }
190 })
191 .expect("spawn the largo worker")
192 .join();
193
194 match outcome {
195 Ok(0) => {}
196 Ok(code) => std::process::exit(code),
197 // A panicked worker already printed its panic message.
198 Err(_) => std::process::exit(101),
199 }
200}