telltale-language 17.0.0

Shared choreography frontend for Telltale DSL parsing, projection, and macro code generation
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
//! Typed protocol annotations
//!
//! This module provides a type-safe representation for protocol annotations,
//! replacing raw string key-value pairs with structured variants.
//!
//! # Design Philosophy
//!
//! Known annotation types (like `TimedChoice`) get dedicated variants with
//! proper types (e.g., `Duration` instead of milliseconds as string).
//! Unknown/custom annotations use the `Custom` fallback variant.
//!
//! This approach:
//! - Provides type safety for known annotations
//! - Enables pattern matching in code generation
//! - Preserves extensibility for future annotation types

use serde::{Deserialize, Serialize};
use std::time::Duration;

#[path = "annotation_collection.rs"]
mod collection;

pub use collection::Annotations;

/// One raw annotation entry as it appeared in the DSL.
///
/// Telltale preserves these entries in source order for downstream integrations
/// that treat annotation ordering as semantically meaningful.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DslAnnotationEntry {
    /// Annotation key as parsed from the DSL surface.
    pub key: String,
    /// Annotation value as parsed from the DSL surface.
    pub value: String,
}

impl DslAnnotationEntry {
    /// Construct one raw DSL annotation entry.
    #[must_use]
    pub fn new(key: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            value: value.into(),
        }
    }
}

/// A typed annotation on a protocol statement.
///
/// Annotations provide metadata that affects code generation or runtime behavior
/// without changing the core session type semantics.
#[derive(Debug, Clone, PartialEq)]
pub enum ProtocolAnnotation {
    /// Timed choice: race an operation against a timeout.
    ///
    /// When applied to a `Choice`, the choosing role races the operation
    /// against the specified duration. If timeout fires first, takes
    /// the `TimedOut` branch; otherwise takes the `OnTime` branch.
    TimedChoice {
        /// Maximum time to wait before timing out.
        duration: Duration,
    },

    /// Priority hint for scheduling.
    ///
    /// Higher values indicate higher priority. Implementation-specific.
    Priority(u32),

    /// Retry count for transient failures.
    ///
    /// Indicates how many times to retry the operation before failing.
    Retry {
        /// Maximum retry attempts.
        max_attempts: u32,
        /// Optional delay between retries.
        delay: Option<Duration>,
    },

    /// Mark a statement as idempotent (safe to retry).
    Idempotent,

    /// Trace/debug annotation for logging.
    Trace {
        /// Optional trace label for identification.
        label: Option<String>,
    },

    /// Runtime timeout hint (for transport layer, not session type).
    ///
    /// Unlike `TimedChoice`, this doesn't affect the session type - it's
    /// purely a hint to the transport layer.
    RuntimeTimeout(Duration),

    /// Heartbeat pattern: sender sends periodic heartbeats, receiver detects absence.
    ///
    /// Desugars to a recursive choice where receiver decides liveness.
    /// The runtime uses `interval` for heartbeat timing and `on_missing_count`
    /// to determine when to declare the sender dead.
    Heartbeat {
        /// Interval between heartbeats.
        interval: Duration,
        /// Number of missing heartbeats before declaring dead.
        on_missing_count: u32,
    },

    /// Execute broadcast/collect operations in parallel.
    ///
    /// When applied to a message with a wildcard/range destination,
    /// sends or receives are executed concurrently rather than sequentially.
    Parallel,

    /// Preserve strict message ordering.
    ///
    /// When applied to a collect operation, messages are returned in the
    /// order specified by the role list. This is the default behavior
    /// for sequential collect.
    Ordered,

    /// Minimum number of responses required for a collect operation.
    ///
    /// When applied to a collect with wildcard/range source, the operation
    /// succeeds once at least `min` responses are received. Remaining responses
    /// are discarded or handled asynchronously.
    MinResponses(u32),

    /// Custom annotation for extensions or unknown types.
    ///
    /// Falls back to key-value string pairs for extensibility.
    Custom {
        /// The annotation key.
        key: String,
        /// The annotation value.
        value: String,
    },
}

impl ProtocolAnnotation {
    fn custom_kv(key: &str, value: &str) -> Self {
        Self::Custom {
            key: key.to_string(),
            value: value.to_string(),
        }
    }

    fn parse_u32_value(value: &str) -> Option<u32> {
        value.parse::<u32>().ok()
    }

    fn parse_u64_value(value: &str) -> Option<u64> {
        value.parse::<u64>().ok()
    }

    fn parse_duration_value(value: &str) -> Option<Duration> {
        let trimmed = value.trim();
        let (number, unit) = if let Some(number) = trimmed.strip_suffix("ms") {
            (number, "ms")
        } else if let Some(number) = trimmed.strip_suffix('s') {
            (number, "s")
        } else if let Some(number) = trimmed.strip_suffix('m') {
            (number, "m")
        } else if let Some(number) = trimmed.strip_suffix('h') {
            (number, "h")
        } else {
            return Self::parse_u64_value(trimmed).map(Duration::from_millis);
        };

        let value = number.trim().parse::<u64>().ok()?;
        let millis = match unit {
            "ms" => value,
            "s" => value.saturating_mul(1000),
            "m" => value.saturating_mul(60_000),
            "h" => value.saturating_mul(3_600_000),
            _ => return None,
        };
        Some(Duration::from_millis(millis))
    }

    fn format_duration_value(duration: Duration) -> String {
        let millis = duration.as_millis();
        if millis % 3_600_000 == 0 {
            format!("{}h", millis / 3_600_000)
        } else if millis % 60_000 == 0 {
            format!("{}m", millis / 60_000)
        } else if millis % 1000 == 0 {
            format!("{}s", millis / 1000)
        } else {
            format!("{millis}ms")
        }
    }

    /// Create a timed choice annotation from a duration.
    #[must_use]
    pub fn timed_choice(duration: Duration) -> Self {
        Self::TimedChoice { duration }
    }

    /// Create a timed choice annotation from milliseconds.
    #[must_use]
    pub fn timed_choice_ms(ms: u64) -> Self {
        Self::TimedChoice {
            duration: Duration::from_millis(ms),
        }
    }

    /// Create a priority annotation.
    #[must_use]
    pub fn priority(value: u32) -> Self {
        Self::Priority(value)
    }

    /// Create a retry annotation with just max attempts.
    #[must_use]
    pub fn retry(max_attempts: u32) -> Self {
        Self::Retry {
            max_attempts,
            delay: None,
        }
    }

    /// Create a retry annotation with delay between attempts.
    #[must_use]
    pub fn retry_with_delay(max_attempts: u32, delay: Duration) -> Self {
        Self::Retry {
            max_attempts,
            delay: Some(delay),
        }
    }

    /// Create a trace annotation without a label.
    #[must_use]
    pub fn trace() -> Self {
        Self::Trace { label: None }
    }

    /// Create a trace annotation with a label.
    #[must_use]
    pub fn trace_labeled(label: impl Into<String>) -> Self {
        Self::Trace {
            label: Some(label.into()),
        }
    }

    /// Create a runtime timeout annotation.
    #[must_use]
    pub fn runtime_timeout(duration: Duration) -> Self {
        Self::RuntimeTimeout(duration)
    }

    /// Create a heartbeat annotation from a duration and missing count.
    #[must_use]
    pub fn heartbeat(interval: Duration, on_missing_count: u32) -> Self {
        Self::Heartbeat {
            interval,
            on_missing_count,
        }
    }

    /// Create a heartbeat annotation from milliseconds and missing count.
    #[must_use]
    pub fn heartbeat_ms(interval_ms: u64, on_missing_count: u32) -> Self {
        Self::Heartbeat {
            interval: Duration::from_millis(interval_ms),
            on_missing_count,
        }
    }

    /// Create a parallel annotation.
    #[must_use]
    pub fn parallel() -> Self {
        Self::Parallel
    }

    /// Create an ordered annotation.
    #[must_use]
    pub fn ordered() -> Self {
        Self::Ordered
    }

    /// Create a min_responses annotation.
    #[must_use]
    pub fn min_responses(min: u32) -> Self {
        Self::MinResponses(min)
    }

    /// Create a custom annotation.
    #[must_use]
    pub fn custom(key: impl Into<String>, value: impl Into<String>) -> Self {
        Self::Custom {
            key: key.into(),
            value: value.into(),
        }
    }

    /// Check if this is a timed choice annotation.
    #[must_use]
    pub fn is_timed_choice(&self) -> bool {
        matches!(self, Self::TimedChoice { .. })
    }

    /// Get the timed choice duration, if this is a timed choice.
    #[must_use]
    pub fn timed_choice_duration(&self) -> Option<Duration> {
        match self {
            Self::TimedChoice { duration } => Some(*duration),
            _ => None,
        }
    }

    /// Check if this is a priority annotation.
    #[must_use]
    pub fn is_priority(&self) -> bool {
        matches!(self, Self::Priority(_))
    }

    /// Get the priority value, if this is a priority annotation.
    #[must_use]
    pub fn priority_value(&self) -> Option<u32> {
        match self {
            Self::Priority(v) => Some(*v),
            _ => None,
        }
    }

    /// Check if this is a retry annotation.
    #[must_use]
    pub fn is_retry(&self) -> bool {
        matches!(self, Self::Retry { .. })
    }

    /// Get retry parameters, if this is a retry annotation.
    #[must_use]
    pub fn retry_config(&self) -> Option<(u32, Option<Duration>)> {
        match self {
            Self::Retry {
                max_attempts,
                delay,
            } => Some((*max_attempts, *delay)),
            _ => None,
        }
    }

    /// Check if this is an idempotent annotation.
    #[must_use]
    pub fn is_idempotent(&self) -> bool {
        matches!(self, Self::Idempotent)
    }

    /// Check if this is a trace annotation.
    #[must_use]
    pub fn is_trace(&self) -> bool {
        matches!(self, Self::Trace { .. })
    }

    /// Check if this is a heartbeat annotation.
    #[must_use]
    pub fn is_heartbeat(&self) -> bool {
        matches!(self, Self::Heartbeat { .. })
    }

    /// Get the heartbeat parameters, if this is a heartbeat annotation.
    #[must_use]
    pub fn heartbeat_params(&self) -> Option<(Duration, u32)> {
        match self {
            Self::Heartbeat {
                interval,
                on_missing_count,
            } => Some((*interval, *on_missing_count)),
            _ => None,
        }
    }

    /// Check if this is a runtime timeout annotation.
    #[must_use]
    pub fn is_runtime_timeout(&self) -> bool {
        matches!(self, Self::RuntimeTimeout(_))
    }

    /// Get the runtime timeout duration, if this is a runtime timeout annotation.
    #[must_use]
    pub fn runtime_timeout_duration(&self) -> Option<Duration> {
        match self {
            Self::RuntimeTimeout(d) => Some(*d),
            _ => None,
        }
    }

    /// Check if this is a parallel annotation.
    #[must_use]
    pub fn is_parallel(&self) -> bool {
        matches!(self, Self::Parallel)
    }

    /// Check if this is an ordered annotation.
    #[must_use]
    pub fn is_ordered(&self) -> bool {
        matches!(self, Self::Ordered)
    }

    /// Check if this is a min_responses annotation.
    #[must_use]
    pub fn is_min_responses(&self) -> bool {
        matches!(self, Self::MinResponses(_))
    }

    /// Get the min_responses value, if this is a min_responses annotation.
    #[must_use]
    pub fn min_responses_value(&self) -> Option<u32> {
        match self {
            Self::MinResponses(n) => Some(*n),
            _ => None,
        }
    }

    /// Check if this is a custom annotation with the given key.
    #[must_use]
    pub fn is_custom_key(&self, expected_key: &str) -> bool {
        matches!(self, Self::Custom { key, .. } if key == expected_key)
    }

    /// Get the custom value if this is a custom annotation with the given key.
    #[must_use]
    pub fn custom_value(&self, expected_key: &str) -> Option<&str> {
        match self {
            Self::Custom { key, value } if key == expected_key => Some(value),
            _ => None,
        }
    }

    pub(crate) fn parse_dsl_entry(key: &str, value: &str) -> Self {
        match key {
            "timed_choice" if value == "true" => {
                // Duration comes from separate timeout_ms annotation; use zero default
                Self::TimedChoice {
                    duration: Duration::from_secs(0),
                }
            }
            "timeout_ms" => Self::parse_u64_value(value)
                .map(|ms| Self::TimedChoice {
                    duration: Duration::from_millis(ms),
                })
                .unwrap_or_else(|| Self::custom_kv(key, value)),
            "priority" => Self::parse_u32_value(value)
                .map(Self::Priority)
                .unwrap_or_else(|| Self::custom_kv(key, value)),
            "retry" => Self::parse_u32_value(value)
                .map(|max_attempts| Self::Retry {
                    max_attempts,
                    delay: None,
                })
                .unwrap_or_else(|| Self::custom_kv(key, value)),
            "idempotent" if value == "true" => Self::Idempotent,
            "trace" => Self::Trace {
                label: if value.is_empty() || value == "true" {
                    None
                } else {
                    Some(value.to_string())
                },
            },
            "runtime_timeout" => Self::parse_duration_value(value)
                .map(Self::RuntimeTimeout)
                .unwrap_or_else(|| Self::custom_kv(key, value)),
            "parallel" if value.is_empty() || value == "true" => Self::Parallel,
            "ordered" if value.is_empty() || value == "true" => Self::Ordered,
            "min_responses" => Self::parse_u32_value(value)
                .map(Self::MinResponses)
                .unwrap_or_else(|| Self::custom_kv(key, value)),
            _ => Self::custom_kv(key, value),
        }
    }

    pub(crate) fn dsl_entries(&self) -> Vec<(String, String)> {
        match self {
            Self::TimedChoice { duration } => vec![
                ("timed_choice".to_string(), "true".to_string()),
                ("timeout_ms".to_string(), duration.as_millis().to_string()),
            ],
            Self::Priority(value) => vec![("priority".to_string(), value.to_string())],
            Self::Retry { max_attempts, .. } => {
                vec![("retry".to_string(), max_attempts.to_string())]
            }
            Self::Idempotent => vec![("idempotent".to_string(), "true".to_string())],
            Self::Trace { label } => vec![(
                "trace".to_string(),
                label.clone().unwrap_or_else(|| "true".to_string()),
            )],
            Self::RuntimeTimeout(duration) => vec![(
                "runtime_timeout".to_string(),
                Self::format_duration_value(*duration),
            )],
            Self::Heartbeat {
                interval,
                on_missing_count,
            } => vec![(
                "heartbeat".to_string(),
                format!(
                    "every {} on_missing {}",
                    Self::format_duration_value(*interval),
                    on_missing_count
                ),
            )],
            Self::Parallel => vec![("parallel".to_string(), "true".to_string())],
            Self::Ordered => vec![("ordered".to_string(), "true".to_string())],
            Self::MinResponses(value) => vec![("min_responses".to_string(), value.to_string())],
            Self::Custom { key, value } => vec![(key.clone(), value.clone())],
        }
    }
}