distributed_cli 4.0.0

The `distributed` CLI for Distributed applications: contracts check/accept, scaffold projects, describe manifests, compile clients, and render schema artifacts. Also a library so other CLIs (e.g. hops) can mount its commands.
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
//! Pure, framework-neutral compiler for Distributed client artifacts.
//!
//! This module deliberately owns no filesystem or glob behavior. Callers load
//! one role/application-selected manifest and the colocated GraphQL documents,
//! then decide how to write or check the returned files.

mod command_manifest;
mod graphql;
mod manifest;
mod projection_delta;
mod render;

#[cfg(test)]
mod command_manifest_tests;

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

use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;

use graphql::{compile_document, CompiledOperation};
use manifest::ClientManifest;
use render::render_project;

/// Complete input to one deterministic client compilation.
#[derive(Clone, Debug)]
pub struct ClientCompileInput {
    /// The role/application-selected Distributed client manifest.
    pub manifest: JsonValue,
    /// Exact authorization surface expected by the caller.
    pub selector: ClientSurfaceSelector,
    /// GraphQL sources. Filesystem traversal and glob expansion stay in the CLI.
    pub documents: Vec<ClientDocument>,
    /// Explicit fallback for `@load` documents outside the conventional route
    /// location. Equivalent CLI syntax is `--route Operation=/route-id`.
    pub route_registrations: Vec<ClientRouteRegistration>,
}

impl ClientCompileInput {
    pub fn new(
        manifest: JsonValue,
        selector: ClientSurfaceSelector,
        documents: Vec<ClientDocument>,
    ) -> Self {
        Self {
            manifest,
            selector,
            documents,
            route_registrations: Vec::new(),
        }
    }

    pub fn with_route_registrations(
        mut self,
        route_registrations: Vec<ClientRouteRegistration>,
    ) -> Self {
        self.route_registrations = route_registrations;
        self
    }
}

/// Explicit client authorization surface. This value only verifies manifest
/// provenance; it can never relabel a broader manifest.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ClientSurfaceSelector {
    Role { name: String },
    Application {
        name: String,
        eligible_roles: Vec<String>,
        schema_roles: Vec<String>,
    },
}

impl ClientSurfaceSelector {
    pub fn role(name: impl Into<String>) -> Self {
        Self::Role { name: name.into() }
    }

    pub fn application(
        name: impl Into<String>,
        eligible_roles: impl IntoIterator<Item = impl Into<String>>,
        schema_roles: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        let mut eligible_roles = eligible_roles.into_iter().map(Into::into).collect::<Vec<_>>();
        let mut schema_roles = schema_roles.into_iter().map(Into::into).collect::<Vec<_>>();
        eligible_roles.sort();
        eligible_roles.dedup();
        schema_roles.sort();
        schema_roles.dedup();
        Self::Application {
            name: name.into(),
            eligible_roles,
            schema_roles,
        }
    }
}

/// One already-loaded GraphQL source.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClientDocument {
    pub path: String,
    pub source: String,
}

impl ClientDocument {
    pub fn new(path: impl Into<String>, source: impl Into<String>) -> Self {
        Self {
            path: path.into(),
            source: source.into(),
        }
    }
}

/// Explicit `@load` route fallback, keyed by the GraphQL operation name.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClientRouteRegistration {
    pub operation: String,
    pub route: String,
}

impl ClientRouteRegistration {
    pub fn new(operation: impl Into<String>, route: impl Into<String>) -> Self {
        Self {
            operation: operation.into(),
            route: route.into(),
        }
    }
}

/// Pure compiler output. `files` is sorted by portable relative path.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GeneratedClientProject {
    pub files: Vec<GeneratedClientFile>,
    pub operations: Vec<GeneratedOperationSummary>,
    pub routes: Vec<GeneratedRoutePlan>,
    pub schema_fingerprint: String,
    pub protocol_fingerprint: String,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GeneratedClientFile {
    pub path: String,
    pub contents: String,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct GeneratedOperationSummary {
    pub name: String,
    pub source_path: String,
    pub module_path: String,
    pub export_name: String,
    pub operation_hash: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub live_operation_hash: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct GeneratedRoutePlan {
    pub operation: String,
    pub route: String,
    pub source_path: String,
    pub discovery: ClientRouteDiscovery,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ClientRouteDiscovery {
    Convention,
    Explicit,
}

/// Stable, source-located error returned for every unsupported or invalid
/// construct. The first compiler slice never silently weakens an operation.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClientCompileError {
    pub code: &'static str,
    pub message: String,
    pub source: Option<ClientSourceLocation>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClientSourceLocation {
    pub path: String,
    pub line: usize,
    pub column: usize,
}

impl ClientCompileError {
    pub(crate) fn manifest(code: &'static str, message: impl Into<String>) -> Self {
        Self {
            code,
            message: message.into(),
            source: None,
        }
    }

    pub(crate) fn source(
        code: &'static str,
        message: impl Into<String>,
        path: &str,
        line: usize,
        column: usize,
    ) -> Self {
        Self {
            code,
            message: message.into(),
            source: Some(ClientSourceLocation {
                path: path.to_string(),
                line,
                column,
            }),
        }
    }
}

impl fmt::Display for ClientCompileError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(source) = &self.source {
            write!(
                formatter,
                "{}:{}:{}: {} [{}]",
                source.path, source.line, source.column, self.message, self.code
            )
        } else {
            write!(formatter, "{} [{}]", self.message, self.code)
        }
    }
}

impl std::error::Error for ClientCompileError {}

/// Compile one selected manifest and its colocated GraphQL sources.
pub fn compile_client(
    mut input: ClientCompileInput,
) -> Result<GeneratedClientProject, ClientCompileError> {
    let manifest = ClientManifest::parse(input.manifest, &input.selector)?;

    if input.documents.is_empty() {
        return Err(ClientCompileError::manifest(
            "client.documents.empty",
            "client compilation requires at least one GraphQL document",
        ));
    }

    for document in &mut input.documents {
        document.path = normalize_source_path(&document.path)?;
    }
    input.documents.sort_by(|left, right| {
        left.path
            .cmp(&right.path)
            .then_with(|| left.source.cmp(&right.source))
    });
    for pair in input.documents.windows(2) {
        if pair[0].path == pair[1].path {
            return Err(ClientCompileError::manifest(
                "client.documents.duplicate_path",
                format!("duplicate GraphQL document path `{}`", pair[0].path),
            ));
        }
    }

    let registrations = validate_route_registrations(input.route_registrations)?;
    let mut used_registrations = BTreeSet::new();
    let mut operations = Vec::with_capacity(input.documents.len());
    for document in &input.documents {
        let operation = compile_document(document, &manifest, &registrations)?;
        if operation.route.as_ref().is_some_and(|route| {
            route.discovery == ClientRouteDiscovery::Explicit
                && used_registrations.insert(operation.name.clone())
        }) {
            // Insert performed by the predicate.
        }
        operations.push(operation);
    }

    operations.sort_by(|left, right| {
        left.name
            .cmp(&right.name)
            .then_with(|| left.source_path.cmp(&right.source_path))
    });
    let mut names = BTreeMap::<&str, &str>::new();
    let mut modules = BTreeMap::<&str, &str>::new();
    for operation in &operations {
        if let Some(previous_path) =
            names.insert(operation.name.as_str(), operation.source_path.as_str())
        {
            return Err(ClientCompileError::manifest(
                "client.operation.duplicate_name",
                format!(
                    "duplicate GraphQL operation `{}` in `{}` and `{}`",
                    operation.name, previous_path, operation.source_path
                ),
            ));
        }
        if let Some(previous_name) =
            modules.insert(operation.module_path.as_str(), operation.name.as_str())
        {
            return Err(ClientCompileError::manifest(
                "client.operation.module_collision",
                format!(
                    "operations `{}` and `{}` collide at generated module `{}`",
                    previous_name, operation.name, operation.module_path
                ),
            ));
        }
    }

    for operation in registrations.keys() {
        if !operations
            .iter()
            .any(|candidate| &candidate.name == operation)
        {
            return Err(ClientCompileError::manifest(
                "client.route.unknown_registration",
                format!("explicit route registration names unknown operation `{operation}`"),
            ));
        }
        if !used_registrations.contains(operation) {
            return Err(ClientCompileError::manifest(
                "client.route.unused_registration",
                format!(
                    "explicit route registration for `{operation}` is unused; registrations are only valid as the `@load` fallback"
                ),
            ));
        }
    }

    validate_unique_routes(&operations)?;
    render_project(&manifest, operations)
}

fn normalize_source_path(path: &str) -> Result<String, ClientCompileError> {
    let normalized = path.trim().replace('\\', "/");
    if normalized.is_empty() {
        return Err(ClientCompileError::manifest(
            "client.documents.empty_path",
            "GraphQL document path must not be empty",
        ));
    }
    if normalized.split('/').any(|segment| segment == "..") {
        return Err(ClientCompileError::manifest(
            "client.documents.parent_path",
            format!("GraphQL document path `{path}` must not contain `..`"),
        ));
    }
    Ok(normalized)
}

fn validate_route_registrations(
    mut registrations: Vec<ClientRouteRegistration>,
) -> Result<BTreeMap<String, String>, ClientCompileError> {
    registrations.sort_by(|left, right| {
        left.operation
            .cmp(&right.operation)
            .then_with(|| left.route.cmp(&right.route))
    });
    let mut result = BTreeMap::new();
    for registration in registrations {
        if !is_graphql_name(&registration.operation) {
            return Err(ClientCompileError::manifest(
                "client.route.invalid_operation",
                format!(
                    "route registration operation `{}` is not a GraphQL name",
                    registration.operation
                ),
            ));
        }
        let route = normalize_route(&registration.route)?;
        if result
            .insert(registration.operation.clone(), route)
            .is_some()
        {
            return Err(ClientCompileError::manifest(
                "client.route.duplicate_registration",
                format!(
                    "duplicate route registration for operation `{}`",
                    registration.operation
                ),
            ));
        }
    }
    Ok(result)
}

fn normalize_route(route: &str) -> Result<String, ClientCompileError> {
    let mut normalized = route.trim().replace('\\', "/");
    if normalized.is_empty() || !normalized.starts_with('/') {
        return Err(ClientCompileError::manifest(
            "client.route.invalid",
            format!("route `{route}` must be non-empty and start with `/`"),
        ));
    }
    if normalized.contains('?')
        || normalized.contains('#')
        || normalized.split('/').any(|segment| segment == "..")
    {
        return Err(ClientCompileError::manifest(
            "client.route.invalid",
            format!("route `{route}` must not contain query, fragment, or parent segments"),
        ));
    }
    while normalized.len() > 1 && normalized.ends_with('/') {
        normalized.pop();
    }
    Ok(normalized)
}

fn validate_unique_routes(operations: &[CompiledOperation]) -> Result<(), ClientCompileError> {
    let mut routes = BTreeMap::<&str, &str>::new();
    for operation in operations {
        let Some(route) = &operation.route else {
            continue;
        };
        if let Some(previous) = routes.insert(&route.route, &operation.name) {
            return Err(ClientCompileError::manifest(
                "client.route.duplicate",
                format!(
                    "route `{}` is owned by both `{}` and `{}`",
                    route.route, previous, operation.name
                ),
            ));
        }
    }
    Ok(())
}

pub(crate) fn is_graphql_name(value: &str) -> bool {
    let mut chars = value.chars();
    matches!(chars.next(), Some('_' | 'A'..='Z' | 'a'..='z'))
        && chars.all(|character| matches!(character, '_' | '0'..='9' | 'A'..='Z' | 'a'..='z'))
}

#[cfg(test)]
mod tests;

#[cfg(test)]
mod runtime_bridge_tests;