nitr-cli 0.0.0-beta.7

The Nitr command-line server binary.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// SPDX-License-Identifier: MIT OR Apache-2.0
// This file is part of Nitr.
// See https://nitrweb.com/ for more information
// Copyright (C) 2024-present Jose Quintana <joseluisq.net>

//! The machine-readable `nitr.*` API description and its generators.
//!
//! One source of truth (`nitr-api.toml`) produces the LuaCATS type
//! definitions (editor completion via the Lua Language Server) and the
//! reference page. Tests assert that every registered `nitr.*` entry
//! appears in the description and that the generated files are current —
//! hand-written stubs drift within one release, and stale autocomplete is
//! worse than none.

use serde::Deserialize;

/// The description source, compiled into the binary.
pub const SOURCE: &str = include_str!("nitr-api.toml");

#[derive(Deserialize)]
pub struct Api {
    #[serde(default, rename = "class")]
    pub classes: Vec<Class>,
    #[serde(default, rename = "fn")]
    pub fns: Vec<FnEntry>,
    #[serde(default, rename = "table")]
    pub tables: Vec<TableEntry>,
}

#[derive(Deserialize)]
pub struct Class {
    pub name: String,
    pub desc: String,
    #[serde(default)]
    pub fields: Vec<Field>,
}

#[derive(Deserialize)]
pub struct Field {
    pub name: String,
    #[serde(rename = "type")]
    pub ty: String,
    pub desc: String,
}

#[derive(Deserialize)]
pub struct FnEntry {
    pub name: String,
    #[serde(default)]
    pub feature: String,
    pub desc: String,
    #[serde(default)]
    pub params: Vec<Param>,
    #[serde(default)]
    pub returns: Vec<Ret>,
    /// Present when the function is also a namespace (`nitr.json`,
    /// `nitr.csrf`): a callable table with these members.
    #[serde(default)]
    pub methods: Vec<Member>,
}

#[derive(Deserialize)]
pub struct TableEntry {
    pub name: String,
    #[serde(default)]
    pub feature: String,
    pub desc: String,
    /// Colon-style members (`nitr.db:query`).
    #[serde(default)]
    pub methods: Vec<Member>,
    /// Dot-style members (`nitr.time.now`).
    #[serde(default)]
    pub functions: Vec<Member>,
    /// Plain data members (`nitr.crypto.max_password_bytes`): constants a
    /// script reads rather than calls.
    #[serde(default)]
    pub fields: Vec<Field>,
}

#[derive(Deserialize)]
pub struct Member {
    pub name: String,
    /// A dot-style member on an otherwise callable entry.
    #[serde(default)]
    pub dot: bool,
    #[serde(default)]
    pub params: Vec<Param>,
    #[serde(default)]
    pub returns: Vec<Ret>,
    pub desc: String,
}

#[derive(Deserialize)]
pub struct Param {
    pub name: String,
    #[serde(rename = "type")]
    pub ty: String,
    #[serde(default)]
    pub desc: String,
}

#[derive(Deserialize)]
pub struct Ret {
    #[serde(rename = "type")]
    pub ty: String,
    #[serde(default)]
    pub desc: String,
}

pub fn parse() -> anyhow::Result<Api> {
    toml::from_str(SOURCE).map_err(|err| anyhow::anyhow!("invalid nitr-api.toml: {err}"))
}

impl Api {
    /// Every dotted path the description covers, for the completeness test.
    pub fn known_paths(&self) -> std::collections::BTreeSet<String> {
        let mut out = std::collections::BTreeSet::new();
        for f in &self.fns {
            out.insert(f.name.clone());
            for m in &f.methods {
                out.insert(format!("{}.{}", f.name, m.name));
            }
        }
        for t in &self.tables {
            out.insert(t.name.clone());
            for m in t.methods.iter().chain(&t.functions) {
                out.insert(format!("{}.{}", t.name, m.name));
            }
            for field in &t.fields {
                out.insert(format!("{}.{}", t.name, field.name));
            }
        }
        out
    }
}

/// A `---@param` line: a trailing `?` on the type marks it optional.
fn param_line(p: &Param) -> String {
    let (name, ty) = match p.ty.strip_suffix('?') {
        Some(ty) if p.name != "..." => (format!("{}?", p.name), ty),
        Some(ty) => (p.name.clone(), ty),
        None => (p.name.clone(), p.ty.as_str()),
    };
    match p.desc.is_empty() {
        true => format!("---@param {name} {ty}"),
        false => format!("---@param {name} {ty} {}", p.desc),
    }
}

fn arg_list(params: &[Param]) -> String {
    params
        .iter()
        .map(|p| p.name.as_str())
        .collect::<Vec<_>>()
        .join(", ")
}

/// Emits one annotated `function ... end` stub.
fn emit_fn(out: &mut String, target: &str, member: &Member, sep: char) {
    out.push_str(&format!("---{}\n", member.desc));
    for p in &member.params {
        out.push_str(&param_line(p));
        out.push('\n');
    }
    for r in &member.returns {
        match r.desc.is_empty() {
            true => out.push_str(&format!("---@return {}\n", r.ty)),
            false => out.push_str(&format!("---@return {} _ {}\n", r.ty, r.desc)),
        }
    }
    out.push_str(&format!(
        "function {target}{sep}{}({}) end\n\n",
        member.name,
        arg_list(&member.params)
    ));
}

/// A `fun(...)` type for `---@overload`.
fn fun_type(params: &[Param], returns: &[Ret]) -> String {
    let params = params
        .iter()
        .map(|p| format!("{}: {}", p.name, p.ty))
        .collect::<Vec<_>>()
        .join(", ");
    let rets = returns
        .iter()
        .map(|r| r.ty.clone())
        .collect::<Vec<_>>()
        .join(", ");
    match rets.is_empty() {
        true => format!("fun({params})"),
        false => format!("fun({params}): {rets}"),
    }
}

fn feature_note(feature: &str) -> String {
    match feature {
        "" => String::new(),
        "test" => " (available in `nitr test` files)".into(),
        "cfg" => " (set when a `config_script` is configured)".into(),
        f => format!(" (std feature: `{f}`)"),
    }
}

/// Generates the LuaCATS definitions file (`resources/nitr-types.lua`).
pub fn emit_types(api: &Api) -> String {
    let mut out = String::from(
        "---@meta nitr\n\
         -- GENERATED by `nitr` from nitr-api.toml — do not edit by hand.\n\
         -- Editor completion for the whole `nitr.*` surface: keep this file\n\
         -- next to your scripts (the Lua Language Server picks it up).\n\n\
         ---@class nitr\nnitr = {}\n\n",
    );

    for class in &api.classes {
        let local = class.name.rsplit('.').next().unwrap_or(&class.name);
        out.push_str(&format!("---{}\n---@class {}\n", class.desc, class.name));
        for field in &class.fields {
            out.push_str(&format!(
                "---@field {} {} {}\n",
                field.name, field.ty, field.desc
            ));
        }
        out.push_str(&format!("local {local} = {{}}\n\n"));
        for f in api.fns.iter().filter(|f| {
            f.name
                .strip_prefix(class.name.as_str())
                .is_some_and(|rest| rest.starts_with(':'))
        }) {
            let member = Member {
                name: f.name.split(':').next_back().unwrap_or_default().into(),
                dot: false,
                params: f.params.iter().map(clone_param).collect(),
                returns: f.returns.iter().map(clone_ret).collect(),
                desc: f.desc.clone(),
            };
            emit_fn(&mut out, local, &member, ':');
        }
    }

    for f in api.fns.iter().filter(|f| !f.name.contains(':')) {
        let desc = format!("{}{}", f.desc, feature_note(&f.feature));
        if f.methods.is_empty() {
            let member = Member {
                name: f.name.rsplit('.').next().unwrap_or(&f.name).into(),
                dot: false,
                params: f.params.iter().map(clone_param).collect(),
                returns: f.returns.iter().map(clone_ret).collect(),
                desc,
            };
            emit_fn(&mut out, "nitr", &member, '.');
        } else {
            // Callable namespace: a class carrying an `@overload` for the
            // call form, then its members.
            out.push_str(&format!(
                "---{desc}\n---@class {}\n---@overload {}\n{} = {{}}\n\n",
                f.name,
                fun_type(&f.params, &f.returns),
                f.name
            ));
            for m in &f.methods {
                emit_fn(&mut out, &f.name, m, if m.dot { '.' } else { ':' });
            }
        }
    }

    for t in &api.tables {
        out.push_str(&format!(
            "---{}{}\n{} = {{}}\n\n",
            t.desc,
            feature_note(&t.feature),
            t.name
        ));
        // Data members before the callables, the way a reader meets them.
        // The placeholder value is never the point — `---@type` is what
        // the language server reads; the real value lives server-side.
        for field in &t.fields {
            out.push_str(&format!(
                "---{}\n---@type {}\n{}.{} = nil\n\n",
                field.desc, field.ty, t.name, field.name
            ));
        }
        for m in &t.methods {
            emit_fn(&mut out, &t.name, m, ':');
        }
        for m in &t.functions {
            emit_fn(&mut out, &t.name, m, '.');
        }
    }

    out
}

fn clone_param(p: &Param) -> Param {
    Param {
        name: p.name.clone(),
        ty: p.ty.clone(),
        desc: p.desc.clone(),
    }
}

fn clone_ret(r: &Ret) -> Ret {
    Ret {
        ty: r.ty.clone(),
        desc: r.desc.clone(),
    }
}

/// One signature line for the reference page.
fn signature(name: &str, params: &[Param], returns: &[Ret]) -> String {
    let params = params
        .iter()
        .map(|p| p.name.clone())
        .collect::<Vec<_>>()
        .join(", ");
    let rets = returns
        .iter()
        .map(|r| r.ty.clone())
        .collect::<Vec<_>>()
        .join(", ");
    match rets.is_empty() {
        true => format!("{name}({params})"),
        false => format!("{name}({params}) -> {rets}"),
    }
}

/// Generates the Markdown reference page (`resources/nitr-api.md`).
pub fn emit_docs(api: &Api) -> String {
    let mut out = String::from(
        "# The `nitr.*` API\n\n\
         <!-- GENERATED by `nitr` from nitr-api.toml — do not edit by hand. -->\n\n\
         The whole surface Nitr exposes to Lua, one namespace. Entries marked\n\
         with a *std feature* need that name in `[std] features`.\n\n",
    );

    out.push_str("## Functions and modules\n\n");
    for f in api.fns.iter().filter(|f| !f.name.contains(':')) {
        out.push_str(&format!(
            "### `{}`{}\n\n{}\n\n",
            signature(&f.name, &f.params, &f.returns),
            feature_note(&f.feature),
            f.desc
        ));
        for m in &f.methods {
            let sep = if m.dot { "." } else { ":" };
            out.push_str(&format!(
                "- `{}` — {}\n",
                signature(&format!("{}{sep}{}", f.name, m.name), &m.params, &m.returns),
                m.desc
            ));
        }
        if !f.methods.is_empty() {
            out.push('\n');
        }
    }
    for t in &api.tables {
        out.push_str(&format!(
            "### `{}`{}\n\n{}\n\n",
            t.name,
            feature_note(&t.feature),
            t.desc
        ));
        for m in &t.methods {
            out.push_str(&format!(
                "- `{}` — {}\n",
                signature(&format!("{}:{}", t.name, m.name), &m.params, &m.returns),
                m.desc
            ));
        }
        for m in &t.functions {
            out.push_str(&format!(
                "- `{}` — {}\n",
                signature(&format!("{}.{}", t.name, m.name), &m.params, &m.returns),
                m.desc
            ));
        }
        for field in &t.fields {
            out.push_str(&format!(
                "- `{}.{}: {}` — {}\n",
                t.name, field.name, field.ty, field.desc
            ));
        }
        if !t.methods.is_empty() || !t.functions.is_empty() || !t.fields.is_empty() {
            out.push('\n');
        }
    }

    out.push_str("## Types\n\n");
    for class in &api.classes {
        out.push_str(&format!("### `{}`\n\n{}\n\n", class.name, class.desc));
        for field in &class.fields {
            out.push_str(&format!(
                "- `{}: {}` — {}\n",
                field.name, field.ty, field.desc
            ));
        }
        let methods: Vec<&FnEntry> = api
            .fns
            .iter()
            .filter(|f| {
                f.name
                    .strip_prefix(class.name.as_str())
                    .is_some_and(|rest| rest.starts_with(':'))
            })
            .collect();
        for f in methods {
            let name = f.name.split(':').next_back().unwrap_or_default();
            out.push_str(&format!(
                "- `{}` — {}\n",
                signature(&format!(":{name}"), &f.params, &f.returns),
                f.desc
            ));
        }
        out.push('\n');
    }

    out
}