rsynko-memory 0.1.0

Deterministic in-memory interpreters for Rsynko specifications
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! The context every specification law scenario runs against.
//!
//! One value ties every sort at once, but it states almost nothing itself: each capability is
//! delegated to the component interpreter that already provides it. The struct is generic in those
//! components, so the composition is structural rather than hard-wired, and with zero-sized
//! syntax interpreters the compiler leaves no trace of it.

use crate::{
    Extraction, Format, InfoRecord, ManagerState, Media, MediaSyntax, ProcessingStep,
    ProcessingSyntax, ProcessorId, REFERENCE_MEDIA_EXTENSION, REFERENCE_MEDIA_URL,
    REFERENCE_VIDEO_ID, REFERENCE_WATCH_URL, ReferenceDownloadEnv, ReferenceExtractor,
    ReferenceExtractorRegistry, ReferenceYoutubeDownloadEnv, ReferenceYoutubeEnv, YoutubeRequest,
};
use crate::{
    MemoryManager, PlannedChange, QueueEntry, ReferenceSyncEnv, RsyncEndpoint, SyncCommand,
    SyncObservation, TextScreen,
};
use ambassador::Delegate;
use rsynko_download::*;
use rsynko_manager::*;
use rsynko_media::*;
use rsynko_rsync::*;
use rsynko_ui::ScreenLawFixture;
use rsynko_yt::*;
use std::path::{Path, PathBuf};

const LAW_RESOURCE: &str = "memory://law-resource";
const LAW_BYTES: &[u8] = b"exact bytes";

/// Denotes a processor the reference context refuses to apply.
#[derive(Debug, thiserror::Error)]
#[error("processor {0} refused")]
pub struct ReferenceProcessingRefusal(String);

/// Composes reference interpreters into one context that satisfies every law scenario.
// Several capabilities delegate to the same component, which Clippy reads as a repeated attribute.
#[allow(
    clippy::duplicated_attributes,
    reason = "one delegation per capability, not per target"
)]
#[derive(Debug, Delegate)]
#[delegate(MetadataAlg, target = "syntax")]
#[delegate(FormatAlg, target = "syntax")]
#[delegate(FormatViewAlg, target = "syntax")]
#[delegate(ArtifactAlg, target = "syntax")]
#[delegate(ExtractionAlg, target = "syntax")]
#[delegate(ExtractionViewAlg, target = "syntax")]
#[delegate(MediaViewAlg, target = "syntax")]
#[delegate(FormatPredicateAlg, target = "syntax")]
#[delegate(FormatSelectionAlg, target = "syntax")]
#[delegate(FormatPredicateMatchAlg, target = "syntax")]
#[delegate(FormatSelectionApplyAlg, target = "syntax")]
#[delegate(OutputNameAlg, target = "syntax")]
#[delegate(ProcessingProgramAlg, target = "programs")]
#[delegate(
    ExtractionCatalogAlg,
    target = "catalog",
    where = "Catalog: ExtractionCatalogAlg + MediaSorts<Extractor = <Syntax as MediaSorts>::Extractor>, Syntax: MediaSorts"
)]
#[delegate(
    ExtractionApplyAlg,
    target = "catalog",
    where = "Catalog: ExtractionApplyAlg + MediaSorts<Extractor = <Syntax as MediaSorts>::Extractor, Extraction = <Syntax as MediaSorts>::Extraction>, Syntax: MediaSorts"
)]
#[delegate(DraftStateAlg, target = "manager")]
#[delegate(OutputDraftAlg, target = "manager")]
#[delegate(InputDraftAlg, target = "manager")]
#[delegate(TextEditorStateAlg, target = "manager")]
#[delegate(NavigationStateAlg, target = "manager")]
#[delegate(DetailSelectionAlg, target = "manager")]
#[delegate(ManagerStatusAlg, target = "manager")]
#[delegate(SafeExitAlg, target = "manager")]
#[delegate(QueueCatalogAlg, target = "manager")]
#[delegate(QueueAppendAlg, target = "manager")]
#[delegate(QueueRemoveAlg, target = "manager")]
#[delegate(QueuePauseAlg, target = "manager")]
#[delegate(QueueDuplicateAlg, target = "manager")]
#[delegate(QueueFormatEditAlg, target = "manager")]
#[delegate(QueueOutputAlg, target = "manager")]
#[delegate(QueueSourceAlg, target = "manager")]
#[delegate(FormatCatalogStateAlg, target = "manager")]
#[delegate(QueueDryRunAlg, target = "manager")]
#[delegate(RehearsalStateAlg, target = "manager")]
#[delegate(SourceMetadataAlg, target = "manager")]
#[delegate(DownloadLogAlg, target = "manager")]
#[delegate(TransferStateAlg, target = "manager")]
#[delegate(SourceRequestAlg, target = "manager")]
#[delegate(SourceRecognitionAlg, target = "manager")]
#[delegate(SubmissionAlg, target = "manager")]
#[delegate(MediaOptionsAlg, target = "manager")]
#[delegate(OutputChoiceAlg, target = "manager")]
#[delegate(YoutubeUrlAlg, target = "youtube")]
#[delegate(
    YoutubeRequestAlg,
    target = "youtube",
    where = "Youtube: YoutubeRequestAlg + YoutubeSorts"
)]
#[delegate(YoutubeProgramAlg, target = "youtube")]
#[delegate(YoutubeClientAlg, target = "youtube")]
#[delegate(
    YoutubeRequestBytesAlg,
    target = "youtube",
    where = "Youtube: YoutubeRequestBytesAlg + YoutubeSorts"
)]
#[delegate(YoutubeResponseAlg, target = "youtube")]
#[delegate(
    YoutubeChallengeAlg,
    target = "youtube",
    where = "Youtube: YoutubeChallengeAlg + YoutubeSorts"
)]
#[delegate(
    YoutubeSolutionAlg,
    target = "youtube",
    where = "Youtube: YoutubeSolutionAlg + YoutubeSorts"
)]
#[delegate(AtomicPublishAlg, target = "downloads")]
#[delegate(RsyncEndpointAlg, target = "sync")]
#[delegate(RsyncEndpointViewAlg, target = "sync")]
#[delegate(SyncCommandAlg, target = "sync")]
#[delegate(SyncCommandViewAlg, target = "sync")]
#[delegate(SyncChangeAlg, target = "sync")]
#[delegate(SyncObservationAlg, target = "sync")]
#[delegate(SyncObservationViewAlg, target = "sync")]
#[delegate(SyncRunAlg, target = "sync")]
#[delegate(SyncWatchAlg, target = "sync")]
#[delegate(DownloadObservationAlg, target = "downloads")]
#[delegate(DownloadReportAlg, target = "downloads")]
#[delegate(DownloadProgressAlg, target = "downloads")]
pub struct LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager> {
    syntax: Syntax,
    programs: Programs,
    catalog: Catalog,
    downloads: Downloads,
    youtube: Youtube,
    manager: Manager,
    sync: ReferenceSyncEnv,
    applied: Vec<String>,
    refused: Option<String>,
}

impl<Syntax, Programs, Catalog, Downloads, Youtube> RehearsalLawFixture
    for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, ManagerState>
{
    fn law_change(&self, path: &str, kind: ChangeKind, size: Option<u64>) -> Self::Change {
        PlannedChange::new(path.to_owned(), kind, size)
    }

    fn law_offer_rehearsal(&mut self, id: Self::Id) {
        self.manager.transfer_request(id);
    }
}

impl<Syntax, Programs, Catalog, Downloads, Youtube, Manager> RsyncSorts
    for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager>
{
    type Endpoint = RsyncEndpoint;
    type Command = SyncCommand;
    type Observation = SyncObservation;
    type Change = PlannedChange;
}

impl<Syntax, Programs, Catalog, Downloads, Youtube, Manager> SubmissionLawFixture
    for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager>
{
    fn law_folder_submission(&self) -> (String, String, String) {
        let input = "backup@nas.local:/volume1/photos/2026".to_owned();
        let output = "/home/dev/photos/2026".to_owned();
        (format!("{input} {output}"), input, output)
    }

    fn law_media_source(&self) -> String {
        "https://www.youtube.com/watch?v=VIDEO_ID".to_owned()
    }

    fn law_unrecognized_source(&self) -> String {
        "/home/dev/music".to_owned()
    }

    fn law_lone_submission(&self) -> (String, String) {
        (
            "backup@nas.local:/volume1/photos/2026/".to_owned(),
            "2026".to_owned(),
        )
    }
}

impl<Syntax, Programs, Catalog, Downloads, Youtube, Manager> SyncLawFixture
    for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager>
{
    fn law_transcript(&mut self, lines: Vec<String>) {
        self.sync.register_transcript(lines);
    }

    fn law_ran(&self, command: &Self::Command) -> bool {
        self.sync.commands().last() == Some(command)
    }
}

impl<Syntax, Programs, Catalog, Downloads, Youtube, Manager> ScreenLawFixture
    for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager>
{
    type Syntax = TextScreen;

    fn law_screen_syntax(&self) -> Self::Syntax {
        TextScreen
    }
}

/// Names the reference composition every law scenario runs against.
pub type ReferenceLaws = LawEnv<
    MediaSyntax,
    ProcessingSyntax,
    ReferenceExtractorRegistry,
    ReferenceDownloadEnv,
    ReferenceYoutubeEnv,
    ManagerState,
>;

/// Names the composition the Youtube law scenarios run against.
///
/// Youtube retrieval consumes the reified request rather than a URL, so this composition differs
/// from the generic one in exactly one component: the interpreter that locates a request's bytes.
pub type ReferenceYoutubeLaws = LawEnv<
    MediaSyntax,
    ProcessingSyntax,
    ReferenceExtractorRegistry,
    ReferenceYoutubeDownloadEnv,
    ReferenceYoutubeEnv,
    ManagerState,
>;

impl Default for ReferenceYoutubeLaws {
    fn default() -> Self {
        let mut downloads = ReferenceYoutubeDownloadEnv::default();
        downloads.register_resource(REFERENCE_MEDIA_URL, LAW_BYTES.to_vec());
        Self {
            syntax: MediaSyntax,
            programs: ProcessingSyntax,
            catalog: ReferenceExtractorRegistry::default(),
            downloads,
            youtube: ReferenceYoutubeEnv::default(),
            manager: ManagerState::downloads(),
            sync: ReferenceSyncEnv::default(),
            applied: Vec::default(),
            refused: None,
        }
    }
}

impl Default for ReferenceLaws {
    fn default() -> Self {
        let mut catalog = ReferenceExtractorRegistry::default();
        for key in ["first", "second"] {
            catalog.push(ReferenceExtractor::succeeds(
                key,
                "https://example.test/",
                Extraction::Media(Media::new(
                    key.to_owned(),
                    InfoRecord::default(),
                    Vec::default(),
                )),
            ));
        }
        let mut downloads = ReferenceDownloadEnv::default();
        downloads.register_resource(LAW_RESOURCE, LAW_BYTES.to_vec());
        Self {
            syntax: MediaSyntax,
            programs: ProcessingSyntax,
            catalog,
            downloads,
            youtube: ReferenceYoutubeEnv::default(),
            manager: ManagerState::downloads(),
            sync: ReferenceSyncEnv::default(),
            applied: Vec::default(),
            refused: None,
        }
    }
}

// The composition states no sort of its own: it reads each one off the component that interprets
// it, so the components must agree on the sorts they share.
impl<
    Syntax,
    Programs,
    Catalog,
    Downloads,
    Youtube,
    Manager,
    Value,
    Metadata,
    Format,
    Artifact,
    Media,
    Extraction,
    Extractor,
    Predicate,
    Selection,
    Output,
> MediaSorts for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager>
where
    Syntax: MediaSorts<
            Value = Value,
            Metadata = Metadata,
            Format = Format,
            Artifact = Artifact,
            Media = Media,
            Extraction = Extraction,
            Extractor = Extractor,
            Predicate = Predicate,
            Selection = Selection,
            Output = Output,
        >,
{
    type Value = Value;
    type Metadata = Metadata;
    type Format = Format;
    type Artifact = Artifact;
    type Media = Media;
    type Extraction = Extraction;
    type Extractor = Extractor;
    type Predicate = Predicate;
    type Selection = Selection;
    type Output = Output;
}

impl<
    Syntax,
    Programs,
    Catalog,
    Downloads,
    Youtube,
    Manager,
    Id,
    Requested,
    Options,
    Named,
    Described,
    Planned,
    Entry,
    Collection,
> ManagerSorts for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager>
where
    Manager: ManagerSorts<
            Id = Id,
            Source = Requested,
            Options = Options,
            Output = Named,
            Format = Described,
            Change = Planned,
            Entry = Entry,
            Downloads = Collection,
        >,
{
    type Id = Id;
    type Source = Requested;
    type Options = Options;
    type Output = Named;
    type Format = Described;
    type Change = Planned;
    type Entry = Entry;
    type Downloads = Collection;
}

impl<Syntax, Programs, Catalog, Downloads, Youtube, Manager, Request, Solutions> YoutubeSorts
    for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager>
where
    Youtube: YoutubeSorts<Request = Request, Solutions = Solutions>,
{
    type Request = Request;
    type Solutions = Solutions;
}

impl<Syntax, Programs, Catalog, Downloads, Youtube, Manager, Processor, Step, Program>
    ProcessingSorts for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager>
where
    Programs: ProcessingSorts<Processor = Processor, Step = Step, Program = Program>,
{
    type Processor = Processor;
    type Step = Step;
    type Program = Program;
}

// Associated functions carry no receiver, so the view is projected rather than delegated.
impl<Syntax, Programs, Catalog, Downloads, Youtube, Manager> ProcessingProgramViewAlg
    for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager>
where
    Programs: ProcessingProgramViewAlg,
{
    fn processing_steps<'a>(program: &'a Self::Program) -> impl Iterator<Item = &'a Self::Step>
    where
        Self::Step: 'a,
    {
        Programs::processing_steps(program)
    }

    fn processing_stage(step: &Self::Step) -> ProcessingStage {
        Programs::processing_stage(step)
    }
}

// Retrieval is delegated by hand: the fixture states its source sort, and the component
// interprets the default one.
impl<Syntax, Programs, Catalog, Downloads, Youtube, Manager, FetchError, Stream>
    FetchStreamAlg<String> for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager>
where
    Downloads: FetchStreamAlg<Error = FetchError, Stream = Stream>,
{
    type Error = FetchError;
    type Stream = Stream;

    fn open_fetch(&self, source: &String) -> Result<FetchStream<Self::Stream>, Self::Error> {
        self.downloads.open_fetch(source)
    }

    fn read_fetch(
        &self,
        stream: &mut Self::Stream,
        buffer: &mut [u8],
    ) -> Result<usize, Self::Error> {
        self.downloads.read_fetch(stream, buffer)
    }
}

// The context records its own application trace, so these it states rather than delegates.
impl<Syntax, Programs, Catalog, Downloads, Youtube, Manager> ProcessingApplyAlg
    for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager>
where
    Programs: ProcessingSorts<Processor = ProcessorId, Step = ProcessingStep>,
{
    type Error = ReferenceProcessingRefusal;

    fn apply_processing_step(&mut self, step: &Self::Step) -> Result<(), Self::Error> {
        let processor = step.processor.0.clone();
        if self.refused.as_ref() == Some(&processor) {
            return Err(ReferenceProcessingRefusal(processor));
        }
        self.applied.push(processor);
        Ok(())
    }
}

impl<Syntax, Programs, Catalog, Downloads, Youtube, Manager> ProcessingLawFixture
    for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager>
{
    fn law_applied(&self) -> Vec<String> {
        self.applied.clone()
    }

    fn refuse_law_processor(&mut self, id: &str) {
        self.refused = Some(id.to_owned());
    }
}

// The fixture reaches this context's own catalog, so it is stated for the reference composition.
impl ExtractionLawFixture for ReferenceLaws {
    fn accepted_law_url(&self) -> String {
        "https://example.test/video".to_owned()
    }

    fn unsupported_law_url(&self) -> String {
        "https://unsupported.test/video".to_owned()
    }

    fn law_applied_extractors(&self) -> Vec<String> {
        self.catalog
            .applications()
            .iter()
            .map(|key| key.0.clone())
            .collect()
    }
}

// A format states where its bytes rest, so locating one is reading that observation back.
impl<Syntax, Programs, Catalog, Downloads, Youtube, Manager> FormatSourceAlg<String>
    for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager>
where
    Syntax: MediaSorts<Format = Format>,
{
    fn format_source(&self, format: &Format) -> Option<String> {
        format.source().map(str::to_owned)
    }
}

impl MediaProgramLawFixture for ReferenceLaws {
    fn located_law_source(&self) -> String {
        LAW_RESOURCE.to_owned()
    }

    fn law_extension(&self) -> String {
        "bin".to_owned()
    }

    fn expected_law_bytes(&self) -> Vec<u8> {
        LAW_BYTES.to_vec()
    }

    fn law_published_bytes(&self, path: &Path) -> Option<Vec<u8>> {
        self.downloads.file(path)
    }
}

impl DownloadLawFixture for ReferenceLaws {
    type Source = String;

    fn law_source(&self) -> Self::Source {
        LAW_RESOURCE.to_owned()
    }
    fn law_destination(&self) -> PathBuf {
        PathBuf::from("published.bin")
    }
    fn law_bytes(&self) -> Vec<u8> {
        LAW_BYTES.to_vec()
    }
    fn forget_law_resource(&mut self) {
        self.downloads = ReferenceDownloadEnv::default();
    }
    fn refuse_law_publication(&mut self) {
        self.downloads.refuse_publication();
    }
    fn law_progress(&self) -> Vec<u64> {
        self.downloads
            .progress()
            .iter()
            .map(|observed| observed.downloaded)
            .collect()
    }
    fn law_terminal_events(&self) -> usize {
        self.downloads.events().len()
    }
    fn law_published(&self) -> Option<Vec<u8>> {
        self.downloads.file(&self.law_destination())
    }
    fn law_abandoned(&self) -> usize {
        self.downloads.abandoned()
    }
}

// The Youtube fixtures reach this context's own seeded player, so they are stated wherever that
// interpreter is the Youtube component.
impl<Syntax, Programs, Catalog, Downloads, Manager> YoutubeChallengeLawFixture
    for LawEnv<Syntax, Programs, Catalog, Downloads, ReferenceYoutubeEnv, Manager>
{
    fn solve_law_challenge(&mut self, challenge: YoutubeChallenge, solution: &str) {
        self.youtube.solve(challenge, solution);
    }

    fn law_challenge_applications(&self) -> Vec<Vec<YoutubeChallenge>> {
        self.youtube.applications()
    }
}

impl<Syntax, Programs, Catalog, Manager> YoutubeLawFixture
    for LawEnv<Syntax, Programs, Catalog, ReferenceYoutubeDownloadEnv, ReferenceYoutubeEnv, Manager>
{
    fn law_watch_url(&self) -> String {
        REFERENCE_WATCH_URL.to_owned()
    }

    fn law_video_id(&self) -> String {
        REFERENCE_VIDEO_ID.to_owned()
    }

    fn law_watch_bytes(&self) -> Vec<u8> {
        ReferenceYoutubeEnv::watch_bytes()
    }

    fn law_player_bytes(&self) -> Vec<u8> {
        ReferenceYoutubeEnv::player_bytes()
    }

    fn law_executed_requests(&self) -> Vec<YoutubeRequest> {
        self.youtube.executed()
    }

    fn law_retrieved_requests(&self) -> Vec<YoutubeRequest> {
        self.downloads.opened()
    }
}

impl MediaProgramLawFixture for ReferenceYoutubeLaws {
    fn located_law_source(&self) -> String {
        REFERENCE_MEDIA_URL.to_owned()
    }

    fn law_extension(&self) -> String {
        REFERENCE_MEDIA_EXTENSION.to_owned()
    }

    fn expected_law_bytes(&self) -> Vec<u8> {
        LAW_BYTES.to_vec()
    }

    fn law_published_bytes(&self, path: &Path) -> Option<Vec<u8>> {
        self.downloads.file(path)
    }
}

// A Youtube format states where its bytes rest, and retrieving them is a media request.
impl<Syntax, Programs, Catalog, Downloads, Youtube, Manager> FormatSourceAlg<YoutubeRequest>
    for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager>
where
    Syntax: MediaSorts<Format = Format>,
    Youtube: YoutubeSorts<Request = YoutubeRequest> + YoutubeRequestAlg,
{
    fn format_source(&self, format: &Format) -> Option<YoutubeRequest> {
        format.source().map(|url| self.youtube.media_request(url))
    }
}

impl<Syntax, Programs, Catalog, Downloads, Youtube, Manager, FetchError, Stream>
    FetchStreamAlg<YoutubeRequest>
    for LawEnv<Syntax, Programs, Catalog, Downloads, Youtube, Manager>
where
    Downloads: FetchStreamAlg<YoutubeRequest, Error = FetchError, Stream = Stream>,
{
    type Error = FetchError;
    type Stream = Stream;

    fn open_fetch(
        &self,
        request: &YoutubeRequest,
    ) -> Result<FetchStream<Self::Stream>, Self::Error> {
        self.downloads.open_fetch(request)
    }

    fn read_fetch(
        &self,
        stream: &mut Self::Stream,
        buffer: &mut [u8],
    ) -> Result<usize, Self::Error> {
        self.downloads.read_fetch(stream, buffer)
    }
}

// The downloads collection is the memory interpreter's own manager state, so it observes itself.
impl DownloadsLawFixture for MemoryManager {
    fn law_collected_sources(&self, downloads: &Self::Downloads) -> Vec<String> {
        downloads
            .queue()
            .iter()
            .map(|entry| QueueEntry::label(entry).to_owned())
            .collect()
    }
}