diplodocus 0.15.0

Build coherent documentation websites for polyglot software projects.
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
//! Versioned semantic keys for future static extractors.
//!
//! Keys encode canonical names and declaration roles, never presentation names,
//! routes, file paths, spans, discovery order, or overload ordinals. Extractors
//! reconcile stubs and implementations before registering one canonical item.
//! Re-export and Rd names bind to that item through [`IdentityRegistry`].
//!
//! Overload keys encode normalized signature structure, including parameter
//! names, conventions, annotations, defaults, and returns. Name resolution
//! targets and diagnostic source spellings are excluded. Producers must supply
//! canonical literal spellings and semantic language-specific children; opaque
//! source-only expressions cannot establish stable overload identity.

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

use serde::{Deserialize, Serialize};
use thiserror::Error;

use super::{ItemReference, ParameterKind, RGenericReference, Signature, SignatureExpression};

/// Python entity roles that can share the same canonical qualified name.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PythonIdentityKind {
    /// Module declaration.
    Module,
    /// Module-level callable family.
    Function,
    /// Method callable family.
    Method,
    /// Explicit constructor callable family.
    Constructor,
    /// Class declaration.
    Class,
    /// Property declaration, distinct from a method with the same name.
    Property,
    /// Constant declaration.
    Constant,
    /// Field declaration.
    Field,
    /// A distinct named type alias.
    TypeAlias,
}

impl PythonIdentityKind {
    fn tag(self) -> &'static str {
        match self {
            Self::Module => "module",
            Self::Function => "function",
            Self::Method => "method",
            Self::Constructor => "constructor",
            Self::Class => "class",
            Self::Property => "property",
            Self::Constant => "constant",
            Self::Field => "field",
            Self::TypeAlias => "type-alias",
        }
    }
}

/// An item-map key built under the `sid1` contract, before package qualification.
///
/// This is not a URL or a display string. The encoding is injective over the
/// supported normalized inputs, so it needs no hash-collision fallback.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct SemanticIdentity(String);

impl SemanticIdentity {
    /// Build a C++ declaration or callable-family key from its defining name.
    pub fn cpp(name: &str, kind: &str) -> Result<Self, IdentityError> {
        nonempty("canonical-name", name)?;
        nonempty("cpp-kind", kind)?;
        Ok(Self(format!("sid1:cpp:{}:{}", atom(kind), atom(name))))
    }

    /// Build a C++ overload key from normalized dispatch syntax.
    /// Parameter names, defaults, source offsets, and native IDs are excluded.
    pub fn cpp_overload(name: &str, dispatch: &SignatureExpression) -> Result<Self, IdentityError> {
        let mut key = format!("{}:overload:", Self::cpp(name, "callable")?.0);
        expression(&mut key, dispatch)?;
        Ok(Self(key))
    }
    /// Build a Rust declaration key from its defining path and declaration kind.
    pub fn rust(name: &str, kind: &str) -> Result<Self, IdentityError> {
        nonempty("canonical-name", name)?;
        nonempty("rust-kind", kind)?;
        Ok(Self(format!("sid1:rust:{}:{}", atom(kind), atom(name))))
    }

    /// Build a Rust member key under its type, trait, and implementation context.
    pub fn rust_member(
        owner: &str,
        context: &str,
        name: &str,
        kind: &str,
    ) -> Result<Self, IdentityError> {
        nonempty("owner", owner)?;
        nonempty("member-name", name)?;
        nonempty("rust-kind", kind)?;
        Ok(Self(format!(
            "sid1:rust:{}:{}:{}:{}",
            atom(kind),
            atom(owner),
            atom(context),
            atom(name)
        )))
    }
    /// Build a named Julia declaration key under the existing `sid1` contract.
    pub fn julia(name: &str, kind: JuliaIdentityKind) -> Result<Self, IdentityError> {
        nonempty("canonical-name", name)?;
        Ok(Self(format!("sid1:julia:{}:{}", kind.tag(), atom(name))))
    }

    /// Build an authored Julia method key from normalized positional dispatch.
    ///
    /// The producer excludes names, keywords, defaults, and return annotations,
    /// and normalizes bound type variables before calling this constructor.
    pub fn julia_method(name: &str, dispatch: &SignatureExpression) -> Result<Self, IdentityError> {
        nonempty("canonical-name", name)?;
        let mut key = format!("sid1:julia:method:{}:", atom(name));
        expression(&mut key, dispatch)?;
        Ok(Self(key))
    }
    /// Build the canonical Python declaration or callable-family key.
    ///
    /// Use the defining module and lexical containers, not a re-export name.
    /// Changing an ordinary callable's signature preserves its family identity.
    pub fn python(name: &str, kind: PythonIdentityKind) -> Result<Self, IdentityError> {
        nonempty("canonical-name", name)?;
        Ok(Self(format!("sid1:python:{}:{}", kind.tag(), atom(name))))
    }

    /// Build an individually addressable overload under a canonical family.
    ///
    /// Reordering declarations or changing source formatting does not change
    /// keys when the normalized signatures are unchanged. Equal normalized
    /// signatures intentionally have equal keys and must be reconciled or
    /// diagnosed as duplicates, never suffixed with a source-order counter.
    pub fn python_overload(
        name: &str,
        kind: PythonIdentityKind,
        signature: &Signature,
    ) -> Result<Self, IdentityError> {
        if !matches!(
            kind,
            PythonIdentityKind::Function
                | PythonIdentityKind::Method
                | PythonIdentityKind::Constructor
        ) {
            return Err(invalid(
                "python-kind",
                "overloads require a callable family",
            ));
        }
        let family = Self::python(name, kind)?;
        let Signature::Callable {
            parameters,
            returns,
        } = signature
        else {
            return Err(invalid(
                "signature",
                "overloads require a callable signature",
            ));
        };
        let mut key = format!("{}:overload:call(", family.0);
        for parameter in parameters {
            nonempty("parameter-name", &parameter.name)?;
            key.push_str("parameter(");
            key.push_str(&atom(&parameter.name));
            key.push(',');
            match &parameter.kind {
                ParameterKind::PositionalOnly => key.push_str("positional-only"),
                ParameterKind::PositionalOrKeyword => key.push_str("positional-or-keyword"),
                ParameterKind::KeywordOnly => key.push_str("keyword-only"),
                ParameterKind::VariadicPositional => key.push_str("variadic-positional"),
                ParameterKind::VariadicKeyword => key.push_str("variadic-keyword"),
                ParameterKind::LanguageSpecific { language, name } => {
                    write!(key, "language-specific({},{})", atom(language), atom(name)).unwrap();
                }
            }
            key.push(',');
            expression_option(&mut key, parameter.annotation.as_ref())?;
            key.push(',');
            expression_option(&mut key, parameter.default.as_ref())?;
            key.push_str(");");
        }
        key.push_str(")returns(");
        expression_option(&mut key, returns.as_ref())?;
        key.push(')');
        Ok(Self(key))
    }

    /// Build a maintained R function's key, including S3 constructor functions.
    pub fn r_function(name: &str) -> Result<Self, IdentityError> {
        Self::r_named(name, "function")
    }

    /// Build a package documentation topic key without a source declaration.
    pub fn r_package(name: &str) -> Result<Self, IdentityError> {
        Self::r_named(name, "package")
    }

    /// Build a maintained constant's key.
    pub fn r_constant(name: &str) -> Result<Self, IdentityError> {
        Self::r_named(name, "constant")
    }

    /// Build a standalone deprecated help topic key.
    pub fn r_deprecated_topic(name: &str) -> Result<Self, IdentityError> {
        Self::r_named(name, "deprecated-topic")
    }

    /// Build a dataset key from its documented object name.
    pub fn r_dataset(name: &str) -> Result<Self, IdentityError> {
        Self::r_named(name, "dataset")
    }

    /// Build an S3 generic's key; the generic is also its callable family.
    pub fn r_s3_generic(name: &str) -> Result<Self, IdentityError> {
        Self::r_named(name, "s3-generic")
    }

    /// Build an S3 method key from its binding, generic identity, and class.
    ///
    /// An explicitly registered method need not be named `generic.class`.
    /// Generic qualification prevents identical binding/class names in distinct
    /// generic families from colliding. External generics remain coordinates.
    pub fn r_s3_method(
        name: &str,
        generic: &RGenericReference,
        class: &str,
    ) -> Result<Self, IdentityError> {
        let mut identity = Self::r_named(name, "s3-method")?;
        nonempty("dispatch-class", class)?;
        let generic = match generic {
            RGenericReference::Workspace { item } => {
                nonempty("generic-package", &item.package)?;
                nonempty("generic-item", &item.item)?;
                format!("workspace({},{})", atom(&item.package), atom(&item.item))
            }
            RGenericReference::External { package, name } => {
                nonempty("generic-package", package)?;
                nonempty("generic-name", name)?;
                format!("external({},{})", atom(package), atom(name))
            }
        };
        write!(identity.0, ":{generic}:{}", atom(class)).unwrap();
        Ok(identity)
    }

    fn r_named(name: &str, kind: &str) -> Result<Self, IdentityError> {
        nonempty("canonical-name", name)?;
        Ok(Self(format!("sid1:r:{kind}:{}", atom(name))))
    }

    /// Borrow the package-local item-map key.
    pub fn item_id(&self) -> &str {
        &self.0
    }

    /// Qualify this key with a stable workspace package ID, never its URL slug.
    pub fn in_package(&self, package: &str) -> Result<ItemReference, IdentityError> {
        nonempty("package", package)?;
        Ok(ItemReference {
            package: package.into(),
            item: self.0.clone(),
        })
    }
}

/// Julia declaration roles with distinct canonical identities.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JuliaIdentityKind {
    /// Lexical module.
    Module,
    /// Generic function.
    Function,
    /// Struct, abstract type, or primitive type.
    Type,
    /// Struct field.
    Field,
    /// Maintained constant.
    Constant,
    /// Maintained macro.
    Macro,
}

impl JuliaIdentityKind {
    fn tag(self) -> &'static str {
        match self {
            Self::Module => "module",
            Self::Function => "function",
            Self::Type => "type",
            Self::Field => "field",
            Self::Constant => "constant",
            Self::Macro => "macro",
        }
    }
}

/// Duplicate and ambiguous identities with stable, portable diagnostic data.
///
/// Producers may attach source evidence when translating these errors into the
/// common diagnostic model. No parser-native text or runtime paths are needed.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Error)]
#[serde(tag = "code", rename_all = "kebab-case", deny_unknown_fields)]
pub enum IdentityError {
    /// A required semantic input cannot establish a stable identity.
    #[error("invalid semantic identity field {field}: {reason}")]
    InvalidIdentity {
        /// Stable field identifier, not the rejected raw value.
        field: String,
        /// Fixed explanation of the failed contract.
        reason: String,
    },
    /// A canonical key was registered twice instead of being reconciled first.
    #[error("duplicate canonical item {item:?}")]
    #[serde(rename = "duplicate-item-identity")]
    DuplicateIdentity {
        /// Duplicated package-qualified identity.
        item: ItemReference,
    },
    /// A lookup name denotes more than one distinct semantic item.
    #[error("conflicting item alias {package}::{alias}")]
    #[serde(rename = "conflicting-item-alias")]
    ConflictingAlias {
        /// Package containing the lookup name.
        package: String,
        /// Ambiguous name.
        alias: String,
        /// All candidates, sorted independently of registration order.
        candidates: BTreeSet<ItemReference>,
    },
    /// An alias points outside the package registry or to an unregistered item.
    #[error("unknown canonical target for alias {alias}: {target:?}")]
    UnknownAliasTarget {
        /// Unbound lookup name.
        alias: String,
        /// Claimed canonical target.
        target: ItemReference,
    },
}

impl IdentityError {
    /// Stable code for translation into the shared diagnostic layer.
    pub fn code(&self) -> &'static str {
        match self {
            Self::InvalidIdentity { .. } => "invalid-identity",
            Self::DuplicateIdentity { .. } => "duplicate-item-identity",
            Self::ConflictingAlias { .. } => "conflicting-item-alias",
            Self::UnknownAliasTarget { .. } => "unknown-alias-target",
        }
    }
}

/// Package-local construction guard, not an extractor or workspace resolver.
///
/// Register canonical entities after source/stub reconciliation. Registering an
/// existing key fails without mutation. Alias binding is idempotent for the same
/// target and never registers an entity. Conflicting bindings retain *all*
/// candidates so subsequent resolution fails instead of choosing the first one.
#[derive(Debug, Clone)]
pub struct IdentityRegistry {
    package: String,
    items: BTreeSet<String>,
    aliases: BTreeMap<String, BTreeSet<ItemReference>>,
}

impl IdentityRegistry {
    /// Start an empty registry under a stable workspace package ID.
    pub fn new(package: &str) -> Result<Self, IdentityError> {
        nonempty("package", package)?;
        Ok(Self {
            package: package.into(),
            items: BTreeSet::new(),
            aliases: BTreeMap::new(),
        })
    }

    /// Register one already-reconciled canonical entity.
    pub fn register(
        &mut self,
        identity: &SemanticIdentity,
    ) -> Result<ItemReference, IdentityError> {
        let item = identity.in_package(&self.package)?;
        if !self.items.insert(item.item.clone()) {
            return Err(IdentityError::DuplicateIdentity { item });
        }
        Ok(item)
    }

    /// Bind a re-export, Rd alias, or canonical lookup spelling to an item.
    ///
    /// Bind public names to a family, not to each individual overload. An
    /// unknown target is rejected without changing the alias table.
    pub fn bind_alias(&mut self, alias: &str, target: &ItemReference) -> Result<(), IdentityError> {
        nonempty("alias", alias)?;
        if target.package != self.package || !self.items.contains(&target.item) {
            return Err(IdentityError::UnknownAliasTarget {
                alias: alias.into(),
                target: target.clone(),
            });
        }
        self.aliases
            .entry(alias.into())
            .or_default()
            .insert(target.clone());
        self.resolve_alias(alias).map(|_| ())
    }

    /// Resolve an unambiguous lookup name; unknown names return `None`.
    pub fn resolve_alias(&self, alias: &str) -> Result<Option<&ItemReference>, IdentityError> {
        let Some(candidates) = self.aliases.get(alias) else {
            return Ok(None);
        };
        if candidates.len() > 1 {
            return Err(IdentityError::ConflictingAlias {
                package: self.package.clone(),
                alias: alias.into(),
                candidates: candidates.clone(),
            });
        }
        Ok(candidates.first())
    }

    /// Number of canonical entities, independent of aliases.
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Whether any canonical entities have been registered.
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }
}

fn invalid(field: &str, reason: &str) -> IdentityError {
    IdentityError::InvalidIdentity {
        field: field.into(),
        reason: reason.into(),
    }
}

fn nonempty(field: &str, value: &str) -> Result<(), IdentityError> {
    if value.is_empty() || value.chars().all(char::is_whitespace) {
        Err(invalid(field, "a nonempty semantic value is required"))
    } else {
        Ok(())
    }
}

// Delimiters and '%' cannot occur in atoms, including non-ASCII UTF-8 bytes.
fn atom(value: &str) -> String {
    let mut encoded = String::new();
    for byte in value.bytes() {
        if byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'.' | b'-') {
            encoded.push(char::from(byte));
        } else {
            write!(encoded, "%{byte:02X}").unwrap();
        }
    }
    encoded
}

fn expression_option(
    key: &mut String,
    value: Option<&SignatureExpression>,
) -> Result<(), IdentityError> {
    if let Some(value) = value {
        key.push_str("some(");
        expression(key, value)?;
        key.push(')');
    } else {
        key.push_str("none");
    }
    Ok(())
}

fn expression(key: &mut String, value: &SignatureExpression) -> Result<(), IdentityError> {
    match value {
        SignatureExpression::Name { name, .. } => {
            write!(key, "name({})", atom(name)).unwrap();
        }
        SignatureExpression::Literal { text } => {
            write!(key, "literal({})", atom(text)).unwrap();
        }
        SignatureExpression::Apply {
            constructor,
            arguments,
        } => {
            key.push_str("apply(");
            expression(key, constructor)?;
            key.push(';');
            expressions(key, arguments)?;
            key.push(')');
        }
        SignatureExpression::Sequence { items } => {
            key.push_str("sequence(");
            expressions(key, items)?;
            key.push(')');
        }
        SignatureExpression::LanguageSpecific {
            language,
            name,
            children,
            source,
        } => {
            if children.is_empty() && source.is_some() {
                return Err(invalid(
                    "signature-expression",
                    "opaque source-only syntax cannot establish overload identity",
                ));
            }
            write!(key, "language-specific({},{},", atom(language), atom(name)).unwrap();
            expressions(key, children)?;
            key.push(')');
        }
    }
    Ok(())
}

fn expressions(key: &mut String, values: &[SignatureExpression]) -> Result<(), IdentityError> {
    for value in values {
        expression(key, value)?;
        key.push(';');
    }
    Ok(())
}