rspyts-cli 3.0.3

Compiler and build orchestrator for rspyts
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! Cross-language validation of linked names, namespaces, and dependencies.
//!
//! Validation runs before rendering so generators can assume that every
//! public name is legal and unique in both hosts. Python namespace dependency
//! cycles are rejected because generated modules use eager imports.

use std::collections::{BTreeMap, BTreeSet};

use super::*;

// Contract-wide validation -------------------------------------------------

/// Validate that a discovered contract can be represented by both generators.
pub(crate) fn validate_contract(project: &Project, manifest: &Manifest) -> Result<()> {
    if manifest.package_name != project.package_name
        || manifest.package_version != project.package_version
    {
        bail!("the discovered package does not match Cargo metadata");
    }
    if !is_python_identifier(&manifest.module_name)
        || matches!(manifest.module_name.as_str(), "api" | "models" | "runtime")
        || is_python_package_attribute(&manifest.module_name)
    {
        bail!(
            "Python native module name `{}` is invalid or reserved for generated package loading",
            manifest.module_name
        );
    }
    validate_namespaces(manifest)?;
    validate_model_namespace_cycles(manifest)?;
    Ok(())
}

/// Validate namespace derivation and public-name uniqueness for both hosts.
pub(crate) fn validate_namespaces(manifest: &Manifest) -> Result<()> {
    let namespace_map = namespaces(manifest);
    let python_namespace_paths = namespace_map
        .keys()
        .map(Namespace::python_segments)
        .collect::<Vec<_>>();
    for (owner, rust_module) in export_origins(manifest) {
        let namespace = manifest.namespace(owner, rust_module);
        if let Some(package) = &namespace.package {
            let segment = package.replace('-', "_");
            if !is_python_identifier(&segment) || is_python_package_attribute(&segment) {
                bail!(
                    "Cargo package `{owner}` derives the invalid Python namespace segment `{segment}`; rename the Cargo package"
                );
            }
        }
        for segment in rust_module.split("::").skip(1) {
            if !is_python_identifier(segment) || is_python_package_attribute(segment) {
                bail!(
                    "Rust module `{rust_module}` in Cargo package `{owner}` contains the invalid Python namespace segment `{segment}`; rename the Rust module"
                );
            }
        }
    }
    let mut python_paths = BTreeMap::<Vec<String>, Namespace>::new();
    for namespace in namespace_map.keys() {
        let path = namespace.python_segments();
        if let Some(existing) = python_paths.insert(path.clone(), namespace.clone())
            && existing != *namespace
        {
            bail!(
                "Rust namespaces `{}` and `{}` both derive the Python path `{}`; rename one Cargo package",
                display_namespace(&existing),
                display_namespace(namespace),
                path.join(".")
            );
        }
    }
    for (namespace, items) in namespace_map {
        validate_generated_type_names(&items)?;
        let namespace_path = namespace.python_segments();
        let child_package_names = python_namespace_paths
            .iter()
            .filter(|path| {
                path.len() == namespace_path.len() + 1
                    && path.starts_with(namespace_path.as_slice())
            })
            .filter_map(|path| path.last().map(String::as_str))
            .collect::<BTreeSet<_>>();
        let mut python_names = items
            .types
            .iter()
            .map(|item| item.name.clone())
            .chain(items.errors.iter().map(|item| item.name.clone()))
            .chain(items.resources.iter().map(|item| item.name.clone()))
            .chain(items.functions.iter().map(|item| item.rust_name.clone()))
            .chain(items.constants.iter().map(|item| item.host_name.clone()))
            .collect::<Vec<_>>();
        for item in &items.types {
            if let rspyts::ir::TypeShape::TaggedEnum { variants, .. } = &item.shape {
                python_names.extend(
                    variants
                        .iter()
                        .map(|variant| tagged_variant_name(&item.name, &variant.rust_name)),
                );
            }
        }
        let mut buffers = BTreeSet::new();
        for reference in namespace_refs(&items) {
            crate::contract::collect_buffers_resolved(reference, manifest, &mut buffers)?;
        }
        if let Some(name) = child_package_names.iter().find(|name| {
            **name == "models"
                || **name == "api"
                || (namespace == Namespace::root()
                    && (**name == "runtime" || **name == manifest.module_name.as_str()))
        }) {
            bail!(
                "Python namespace segment `{name}` conflicts with a generated module in namespace `{}`",
                display_namespace(&namespace)
            );
        }
        python_names.extend(
            buffers
                .into_iter()
                .map(|element| python::buffer_name(element).to_owned()),
        );
        if let Some(name) = python_names.iter().find(|name| {
            matches!(
                name.as_str(),
                "__all__" | "__dir__" | "__getattr__" | "api" | "models"
            ) || name.starts_with("_rspyts_models_")
                || is_python_package_attribute(name)
                || (namespace == Namespace::root()
                    && (name.as_str() == "runtime"
                        || name.as_str() == manifest.module_name.as_str()))
                || child_package_names.contains(name.as_str())
        }) {
            bail!(
                "Python export name `{name}` is reserved for generated package loading in namespace `{}`",
                display_namespace(&namespace)
            );
        }
        if let Some(name) = python_names.iter().find(|name| !is_python_identifier(name)) {
            bail!("Python export name `{name}` is not a valid identifier");
        }
        unique_public_names("Python", python_names.into_iter())
            .with_context(|| format!("in namespace `{}`", display_namespace(&namespace)))?;

        let mut typescript_names = items
            .types
            .iter()
            .map(|item| item.name.clone())
            .chain(items.errors.iter().map(|item| item.name.clone()))
            .chain(items.resources.iter().map(|item| item.name.clone()))
            .chain(items.functions.iter().map(|item| item.host_name.clone()))
            .chain(items.constants.iter().map(|item| item.host_name.clone()))
            .collect::<Vec<_>>();
        for item in &items.types {
            if let rspyts::ir::TypeShape::TaggedEnum { variants, .. } = &item.shape {
                typescript_names.extend(
                    variants
                        .iter()
                        .map(|variant| tagged_variant_name(&item.name, &variant.rust_name)),
                );
            }
        }
        if let Some(name) = typescript_names
            .iter()
            .find(|name| !is_typescript_identifier(name))
        {
            bail!("TypeScript export name `{name}` is not a valid identifier");
        }
        unique_public_names("TypeScript", typescript_names.into_iter())
            .with_context(|| format!("in namespace `{}`", display_namespace(&namespace)))?;
    }
    Ok(())
}

/// Reject model members that collide after Python identifier normalization.
fn validate_generated_type_names(items: &crate::contract::NamespaceItems<'_>) -> Result<()> {
    for definition in &items.types {
        match &definition.shape {
            rspyts::ir::TypeShape::Struct { fields } => {
                validate_python_members(
                    &definition.name,
                    fields
                        .iter()
                        .map(|field| crate::python::safe_python_name(&field.rust_name)),
                )?;
            }
            rspyts::ir::TypeShape::StringEnum { variants } => {
                validate_python_members(
                    &definition.name,
                    variants
                        .iter()
                        .map(|variant| crate::python::safe_python_name(&variant.rust_name)),
                )?;
            }
            rspyts::ir::TypeShape::TaggedEnum { tag, variants } => {
                for variant in variants {
                    validate_python_members(
                        &format!("{}::{}", definition.name, variant.rust_name),
                        std::iter::once(crate::python::safe_python_name(tag)).chain(
                            variant
                                .fields
                                .iter()
                                .map(|field| crate::python::safe_python_name(&field.rust_name)),
                        ),
                    )?;
                }
            }
            rspyts::ir::TypeShape::Alias { .. } => {}
        }
    }
    Ok(())
}

/// Validate one generated Python class's member namespace.
fn validate_python_members(owner: &str, names: impl Iterator<Item = String>) -> Result<()> {
    let mut seen = BTreeSet::new();
    for name in names {
        if name == "model_config" || name.starts_with("__pydantic_") {
            bail!("Python member name `{name}` in `{owner}` is reserved by generated models");
        }
        if !seen.insert(name.clone()) {
            bail!(
                "Python member names in `{owner}` collide after identifier normalization at `{name}`"
            );
        }
    }
    Ok(())
}

// Namespace graph ----------------------------------------------------------

/// Return the distinct Cargo-package and Rust-module origins in a manifest.
pub(crate) fn export_origins(manifest: &Manifest) -> Vec<(&rspyts::ir::CargoPackageId, &str)> {
    let mut origins = manifest
        .types
        .iter()
        .map(|item| (&item.owner, item.rust_module.as_str()))
        .chain(
            manifest
                .errors
                .iter()
                .map(|item| (&item.owner, item.rust_module.as_str())),
        )
        .chain(
            manifest
                .functions
                .iter()
                .map(|item| (&item.owner, item.rust_module.as_str())),
        )
        .chain(
            manifest
                .resources
                .iter()
                .map(|item| (&item.owner, item.rust_module.as_str())),
        )
        .chain(
            manifest
                .constants
                .iter()
                .map(|item| (&item.owner, item.rust_module.as_str())),
        )
        .collect::<Vec<_>>();
    origins.sort();
    origins.dedup();
    origins
}

/// Reject cross-namespace model cycles that Python cannot import eagerly.
pub(crate) fn validate_model_namespace_cycles(manifest: &Manifest) -> Result<()> {
    let mut graph = BTreeMap::<Namespace, BTreeSet<Namespace>>::new();
    for definition in &manifest.types {
        let source = manifest.namespace(&definition.owner, &definition.rust_module);
        graph.entry(source.clone()).or_default();
        for reference in type_refs(definition) {
            let mut identities = Vec::new();
            named_identities(reference, &mut identities);
            for identity in identities {
                let target = type_namespace(identity, manifest)?;
                if target != source {
                    graph.entry(source.clone()).or_default().insert(target);
                }
            }
        }
    }
    let mut complete = BTreeSet::new();
    let mut active = BTreeSet::new();
    let mut stack = Vec::new();
    for namespace in graph.keys() {
        if let Some(cycle) =
            namespace_cycle(namespace, &graph, &mut active, &mut complete, &mut stack)
        {
            let path = cycle
                .iter()
                .map(display_namespace)
                .collect::<Vec<_>>()
                .join(" -> ");
            bail!(
                "Python model namespaces form a dependency cycle: {path}; move the declarations into one Rust module or remove the cyclic type reference"
            );
        }
    }
    Ok(())
}

/// Find one cycle reachable from `namespace` using depth-first traversal.
pub(crate) fn namespace_cycle(
    namespace: &Namespace,
    graph: &BTreeMap<Namespace, BTreeSet<Namespace>>,
    active: &mut BTreeSet<Namespace>,
    complete: &mut BTreeSet<Namespace>,
    stack: &mut Vec<Namespace>,
) -> Option<Vec<Namespace>> {
    if complete.contains(namespace) {
        return None;
    }
    if active.contains(namespace) {
        let start = stack.iter().position(|item| item == namespace).unwrap_or(0);
        let mut cycle = stack[start..].to_vec();
        cycle.push(namespace.clone());
        return Some(cycle);
    }
    active.insert(namespace.clone());
    stack.push(namespace.clone());
    if let Some(targets) = graph.get(namespace) {
        for target in targets {
            if let Some(cycle) = namespace_cycle(target, graph, active, complete, stack) {
                return Some(cycle);
            }
        }
    }
    stack.pop();
    active.remove(namespace);
    complete.insert(namespace.clone());
    None
}

/// Render a namespace for diagnostics, naming the empty namespace explicitly.
pub(crate) fn display_namespace(namespace: &Namespace) -> String {
    let namespace = namespace.display();
    if namespace.is_empty() {
        "<root>".to_owned()
    } else {
        namespace
    }
}

// Public-name and package validation ---------------------------------------

/// Require every generated name in one host scope to be unique.
pub(crate) fn unique_public_names<S: AsRef<str>>(
    host: &str,
    names: impl Iterator<Item = S>,
) -> Result<()> {
    let mut seen = BTreeSet::new();
    for name in names {
        let name = name.as_ref();
        if !seen.insert(name.to_owned()) {
            bail!("duplicate {host} export name `{name}`");
        }
    }
    Ok(())
}

/// Validate a dot-separated Python import package.
pub(crate) fn validate_python_package(value: &str) -> Result<()> {
    if value.is_empty()
        || value
            .split('.')
            .any(|part| !is_python_identifier(part) || is_python_package_attribute(part))
    {
        bail!("Python package `{value}` must contain dot-separated identifiers");
    }
    Ok(())
}

/// Validate an unscoped or npm-scoped TypeScript package name.
pub(crate) fn validate_typescript_package(value: &str) -> Result<()> {
    let name = value.strip_prefix('@').unwrap_or(value);
    let parts = name.split('/').collect::<Vec<_>>();
    let expected = if value.starts_with('@') { 2 } else { 1 };
    if value.is_empty()
        || parts.len() != expected
        || parts.iter().any(|part| {
            part.is_empty()
                || !part.chars().all(|character| {
                    character.is_ascii_lowercase()
                        || character.is_ascii_digit()
                        || matches!(character, '-' | '_' | '.')
                })
        })
    {
        bail!("invalid TypeScript package `{value}`");
    }
    Ok(())
}

/// Validate the portable application-name grammar used by generated symbols.
pub(crate) fn validate_application_name(value: &str) -> Result<()> {
    let mut characters = value.chars();
    if !characters
        .next()
        .is_some_and(|character| character.is_ascii_lowercase())
        || !characters.all(|character| {
            character.is_ascii_lowercase() || character.is_ascii_digit() || character == '-'
        })
        || value.ends_with('-')
        || value.contains("--")
    {
        bail!(
            "application name `{value}` must use lower-case letters, numbers, and single hyphens"
        );
    }
    Ok(())
}

/// Return whether `value` follows rspyts's portable ASCII identifier grammar.
pub(crate) fn is_identifier(value: &str) -> bool {
    let mut chars = value.chars();
    chars
        .next()
        .is_some_and(|character| character.is_ascii_alphabetic() || character == '_')
        && chars.all(|character| character.is_ascii_alphanumeric() || character == '_')
}

/// Return whether `value` is a usable, non-keyword Python identifier.
pub(crate) fn is_python_identifier(value: &str) -> bool {
    is_identifier(value)
        && !matches!(
            value,
            "False"
                | "None"
                | "True"
                | "and"
                | "as"
                | "assert"
                | "async"
                | "await"
                | "break"
                | "class"
                | "continue"
                | "def"
                | "del"
                | "elif"
                | "else"
                | "except"
                | "finally"
                | "for"
                | "from"
                | "global"
                | "if"
                | "import"
                | "in"
                | "is"
                | "lambda"
                | "nonlocal"
                | "not"
                | "or"
                | "pass"
                | "raise"
                | "return"
                | "try"
                | "while"
                | "with"
                | "yield"
        )
}

/// Return whether `value` is a usable, non-keyword TypeScript identifier.
pub(crate) fn is_typescript_identifier(value: &str) -> bool {
    is_identifier(value)
        && !matches!(
            value,
            "break"
                | "case"
                | "catch"
                | "class"
                | "const"
                | "continue"
                | "debugger"
                | "default"
                | "delete"
                | "do"
                | "else"
                | "enum"
                | "export"
                | "extends"
                | "false"
                | "finally"
                | "for"
                | "function"
                | "if"
                | "import"
                | "in"
                | "instanceof"
                | "new"
                | "null"
                | "return"
                | "super"
                | "switch"
                | "this"
                | "throw"
                | "true"
                | "try"
                | "typeof"
                | "var"
                | "void"
                | "while"
                | "with"
                | "as"
                | "implements"
                | "interface"
                | "let"
                | "package"
                | "private"
                | "protected"
                | "public"
                | "static"
                | "yield"
                | "any"
                | "boolean"
                | "constructor"
                | "declare"
                | "get"
                | "module"
                | "namespace"
                | "never"
                | "number"
                | "readonly"
                | "require"
                | "set"
                | "string"
                | "symbol"
                | "type"
                | "undefined"
                | "unknown"
        )
}

/// Return whether Python reserves `value` as a package protocol attribute.
pub(crate) fn is_python_package_attribute(value: &str) -> bool {
    value != "__version__" && value.starts_with("__") && value.ends_with("__")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn generated_member_validation_rejects_collisions_and_reserved_names() {
        assert_eq!(crate::python::safe_python_name("event-type"), "event_type");
        assert_eq!(crate::python::safe_python_name("class"), "class_value");
        assert!(
            validate_python_members(
                "Model",
                ["class_value".to_owned(), "class_value".to_owned()].into_iter(),
            )
            .is_err()
        );
        assert!(validate_python_members("Model", ["model_config".to_owned()].into_iter()).is_err());
    }

    #[test]
    fn host_identifier_validation_rejects_language_keywords() {
        assert!(!is_python_identifier("class"));
        assert!(!is_typescript_identifier("type"));
        assert!(is_python_identifier("Model"));
        assert!(is_typescript_identifier("Model"));
    }
}