sofie-model 0.1.0

The app model: modules, areas and routes, read from source without compiling it.
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
//! The app model (principle 8): read once from source and shared by every generator and check so they cannot disagree.

mod areas;
mod code;
mod config;
mod constraints;
mod contract_traits;
mod cross_owner;
mod declarations;
mod effects;
mod entitlements;
mod error;
mod flags;
mod gateways;
mod gating;
mod handlers;
mod identity;
mod islands;
mod load;
mod mails;
mod manifest;
mod menu;
mod messages;
mod metadata;
pub mod naming;
mod package_events;
mod package_routes;
pub mod permissions;
mod posts;
mod provisions;
mod references;
mod routes;
mod shipped;
mod sign_in;
mod sources;
pub mod syntax;
mod targets;
mod telemetry;
#[cfg(feature = "testing")]
pub mod testing;
pub mod usage;

use std::path::PathBuf;

use usage::{Mentions, PublicItem};

pub use code::{
    Aggregate, Code, EnumDeclaration, EnumVariant, FieldDeclaration, Function, InputDeclaration,
    KeyLiteral, RawSql, ReadModel, ScreenRender, TypeMethod, TypeName,
};
pub use config::{ConfigKey, DATABASE_URL, KeyKind, ModuleConfig, config_type_name};
pub use contract_traits::{ContractMethod, ContractTrait};
pub use cross_owner::CrossOwnerCheck;
pub use declarations::{ModuleDeclaration, module_declaration};
pub use effects::{Call, Effects, MethodCall, Queued, Raised};
pub use entitlements::{EntitlementDeclaration, Entitlements};
pub use error::ModelError;
pub use flags::{FeatureCode, FeatureDeclaration, FeatureGate};
pub use gateways::{Gateway, GatewayAdapter};
pub use islands::{Island, IslandBundle, PropsDeclaration, is_island_name, island_bundles};
pub use load::{
    dependency_crate, files_with_extension, load, rust_files, subdirectories, workspace_crates,
};
pub use manifest::{MANIFEST_CONSTANT, Manifest, Tier, manifest_file};
pub use messages::interval_seconds;
pub use metadata::{framework_features, workspace_root};
pub use naming::{KnownArea, WiringSide};
pub use package_routes::{ListedRoute, PackageRoutes, RouteMount, RoutedPackage, ServedRoute};
pub use routes::check_route_path;
pub use shipped::{MountedScreen, ScreenRoute, ShippedScreen};
pub use sign_in::SignInPaths;
pub use telemetry::{ProductEvent, TelemetryCode};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct App {
    pub root: PathBuf,
    pub app: Crate,
    pub modules: Vec<Module>,
    pub identity: Option<Identity>,
    /// Raised by the framework packages the app uses, which its listeners may hear.
    pub package_events: Vec<EventDeclaration>,
    /// The framework packages other than sofie-identity whose routes the app serves.
    pub packages: Vec<RoutedPackage>,
    pub split: Split,
    /// Set by `[package.metadata.sofie] structure-ceilings = true`.
    pub structure_ceilings: bool,
}

/// What `sofie split` wrote into the app's `[package.metadata.sofie]`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Split {
    /// This app's modules whose contracts, lookups and events it serves to other apps.
    pub serves: Vec<String>,
    pub remote: Vec<RemoteModule>,
}

/// A module split into another app, known here only through its contract.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RemoteModule {
    pub name: String,
    pub contract: Crate,
    pub defined_areas: Vec<AreaDefinition>,
    pub exports: Vec<String>,
    pub events: Vec<String>,
    pub traits: Vec<ContractTrait>,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Identity {
    pub directory: PathBuf,
    pub screens: Vec<String>,
    pub routes: Vec<ListedRoute>,
    pub mounts: Vec<RouteMount>,
    pub shipped: Vec<ShippedScreen>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Crate {
    pub package: String,
    pub crate_name: String,
    pub path: PathBuf,
    pub dependencies: Vec<Dependency>,
    pub sources: Vec<Source>,
    pub tests_and_examples: Vec<Source>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Dependency {
    pub package: String,
    pub crate_name: String,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Source {
    pub file: PathBuf,
    pub paths: Vec<PathReference>,
    pub macros: Vec<MacroCall>,
    pub mentions: Mentions,
    pub public_items: Vec<PublicItem>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PathReference {
    pub segments: Vec<String>,
    pub line: usize,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MacroCall {
    pub path: Vec<String>,
    pub first_string: Option<String>,
    pub line: usize,
}

impl MacroCall {
    #[must_use]
    pub fn name(&self) -> &str {
        self.path.last().map_or("", String::as_str)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Module {
    pub krate: Crate,
    pub manifest: Option<Manifest>,
    pub config: Option<ModuleConfig>,
    pub contract: Option<Crate>,
    pub defined_areas: Vec<AreaDefinition>,
    pub contract_declares_gen: bool,
    pub contract_exports: Vec<String>,
    pub contract_traits: Vec<ContractTrait>,
    pub migrations: Vec<PathBuf>,
    /// `migrations/sqlite/*.sql`, run on SQLite instead of the files of the same name.
    pub sqlite_migrations: Vec<PathBuf>,
    pub areas: Vec<AreaContribution>,
    pub listeners: Vec<Listener>,
    pub schedules: Vec<Schedule>,
    pub events: Vec<String>,
    pub module_events: Vec<EventDeclaration>,
    pub jobs: Vec<Job>,
    pub tasks: Vec<Task>,
    pub implementations: Vec<Provision>,
    pub extensions: Vec<Provision>,
    pub gateways: Vec<Gateway>,
    pub islands: Vec<Island>,
    /// `web/<Name>.html`, written in any screen as `<Name … />`.
    pub components: Vec<PathBuf>,
    /// `lang/<language>.toml`.
    pub languages: Vec<PathBuf>,
    pub mails: Vec<MailTemplate>,
    /// `src/plans.rs`, whose `PLANS` sofie-billing is installed with.
    pub plans: Option<PathBuf>,
    pub private_items: Vec<PrivateItem>,
    pub code: Code,
}

/// A `mod` or a wired function that is not `pub`: the wiring cannot name it or what is below it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PrivateItem {
    pub path: Vec<String>,
    pub file: PathBuf,
    pub line: usize,
    /// `pub(crate)`: the module's own wiring reaches it, the app's does not.
    pub crate_visible: bool,
}

/// `src/mails/<name>.html`, keyed `<module>/<name>`; one under
/// `src/mails/<crate>/` is keyed `<crate>/<name>`, replacing that crate's own mail.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MailTemplate {
    pub key: String,
    pub file: PathBuf,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AreaDefinition {
    pub name: String,
    pub type_path: Vec<String>,
    pub prefix: Option<String>,
    pub client: AreaClient,
    pub principal: Vec<String>,
    pub implements_member_area: bool,
    /// It implements sofie-identity's `SignUp`, so a generated `MemberArea` opens sign-up.
    pub opens_sign_up: bool,
    pub file: PathBuf,
    pub line: usize,
}

/// What an area's pages are: HTML over the wire, or a client framework's components compiled
/// from the same templates.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum AreaClient {
    #[default]
    Html,
    Svelte,
    React,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Job {
    pub file: PathBuf,
    pub line: usize,
    pub function: Vec<String>,
}

/// Named `<module>.<function>` wherever under `tasks` it lives.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Task {
    pub file: PathBuf,
    pub line: usize,
    pub function: Vec<String>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Provision {
    pub contract: Vec<String>,
    pub ty: Vec<String>,
    /// A unit struct is built as written; any other type from the database through its `From<Db>`.
    pub unit: bool,
    pub file: PathBuf,
    pub line: usize,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EventDeclaration {
    pub name: String,
    pub file: PathBuf,
    pub line: usize,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Listener {
    pub file: PathBuf,
    pub line: usize,
    pub function: Vec<String>,
    pub event: Vec<String>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Schedule {
    pub file: PathBuf,
    pub line: usize,
    pub function: Vec<String>,
    pub cadence: Cadence,
    pub each: Option<Vec<String>>,
}

/// Written as `every 1h`, `daily at 03:00 Europe/Istanbul` or
/// `weekly on monday at 03:00 Europe/Istanbul`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Cadence {
    /// As written, which the schedule-interval rule reads.
    Interval(String),
    WallClock(WallClock),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WallClock {
    pub daily_at: String,
    pub weekly_on: Option<String>,
    pub zone: String,
}

impl std::fmt::Display for Cadence {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Interval(interval) => write!(formatter, "every {interval}"),
            Self::WallClock(WallClock {
                daily_at,
                weekly_on: None,
                zone,
            }) => write!(formatter, "daily at {daily_at} {zone}"),
            Self::WallClock(WallClock {
                daily_at,
                weekly_on: Some(weekday),
                zone,
            }) => write!(formatter, "weekly on {weekday} at {daily_at} {zone}"),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AreaContribution {
    pub area: String,
    pub routes_file: PathBuf,
    /// An unreachable file would be silently left out of the build.
    pub declared: bool,
    pub routes: Vec<Route>,
    pub screens: Vec<PathBuf>,
    pub overrides: Vec<ScreenOverride>,
    pub menu: Option<MenuFile>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MenuFile {
    pub file: PathBuf,
    pub declared: bool,
    pub entries: Vec<MenuEntry>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MenuEntry {
    pub label: String,
    pub feature: Option<String>,
    pub line: usize,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ScreenOverride {
    pub module: String,
    pub file: PathBuf,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Method {
    Get,
    Post,
    Put,
    Patch,
    Delete,
}

impl Method {
    const ALL: [Self; 5] = [Self::Get, Self::Post, Self::Put, Self::Patch, Self::Delete];

    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::Get => "GET",
            Self::Post => "POST",
            Self::Put => "PUT",
            Self::Patch => "PATCH",
            Self::Delete => "DELETE",
        }
    }

    #[must_use]
    pub fn from_routes_method(name: &str) -> Option<Self> {
        Self::ALL
            .into_iter()
            .find(|method| method.name().to_ascii_lowercase() == name)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Route {
    pub method: Method,
    pub path: String,
    pub handler: Vec<String>,
    pub screen: Option<String>,
    pub island: Option<String>,
    pub then: Option<Then>,
    pub modifiers: Vec<Modifier>,
    pub webhook: Option<String>,
    pub line: usize,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Modifier {
    Throttle(String),
    Signed,
    Idempotent,
    BodyLimit(String),
    Feature(String),
    /// The entitlement a principal's plan must hold, as written.
    Entitled(String),
    /// A screen's fragment refetched when what its loader read changes.
    Live(String),
    /// How long a screen's loaded reply is kept, as written.
    Cached(String),
    /// Queues its `#[job]` action instead of running it.
    Queued,
    /// A page answering its loader's text as this media type, as written.
    Document(String),
    /// The largest file a form may upload, as written.
    FileLimit(String),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Then {
    Screen {
        screen: String,
        fragment: String,
    },
    Redirect(String),
    /// To the `https://` location the action replies.
    Follow,
    /// To the path, unless the action replies a location of this app.
    RedirectOrFollow(String),
}