rune/workspace/
manifest.rs

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
use std::ffi::OsStr;
use std::fmt;
use std::fs;
use std::io;
use std::iter;
use std::path::{Path, PathBuf};

use anyhow::Result;
use relative_path::{RelativePath, RelativePathBuf};
use semver::Version;
use serde::de::IntoDeserializer;
use serde::Deserialize;
use serde_hashkey as key;

use crate as rune;
use crate::alloc::prelude::*;
use crate::alloc::{self, String, Vec};
use crate::ast::{Span, Spanned};
use crate::workspace::spanned_value::{Array, SpannedValue, Table, Value};
use crate::workspace::{
    glob, Diagnostics, SourceLoader, WorkspaceError, WorkspaceErrorKind, MANIFEST_FILE,
};
use crate::{SourceId, Sources};

const BIN: &str = "bin";
const TESTS: &str = "tests";
const EXAMPLES: &str = "examples";
const BENCHES: &str = "benches";

/// A workspace filter which in combination with functions such as
/// [Manifest::find_bins] can be used to selectively find things in the
/// workspace.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum WorkspaceFilter<'a> {
    /// Look for one specific named thing.
    Name(&'a str),
    /// Look for all things.
    All,
}

/// The kind of a found entry.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum FoundKind {
    /// The found entry is a binary.
    Binary,
    /// The found entry is a test.
    Test,
    /// The found entry is an example.
    Example,
    /// The found entry is a benchmark.
    Bench,
}

impl fmt::Display for FoundKind {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FoundKind::Binary => "bin".fmt(f),
            FoundKind::Test => "test".fmt(f),
            FoundKind::Example => "example".fmt(f),
            FoundKind::Bench => "bench".fmt(f),
        }
    }
}

/// A found item in the workspace.
#[derive(Debug, TryClone)]
#[non_exhaustive]
pub struct Found {
    /// The kind found.
    #[try_clone(copy)]
    pub kind: FoundKind,
    /// A found path that can be built.
    pub path: PathBuf,
    /// Name of the found thing.
    pub name: String,
}

/// A found item in the workspace associated with a package.
#[derive(Debug, TryClone)]
#[non_exhaustive]
pub struct FoundPackage<'a> {
    /// A found path that can be built.
    pub found: Found,
    /// Index of the package build belongs to.
    pub package: &'a Package,
}

impl WorkspaceFilter<'_> {
    fn matches(self, name: &str) -> bool {
        match self {
            WorkspaceFilter::Name(expected) => name == expected,
            WorkspaceFilter::All => true,
        }
    }
}

impl<T> Spanned for toml::Spanned<T> {
    #[inline]
    fn span(&self) -> Span {
        let range = toml::Spanned::span(self);
        Span::new(range.start, range.end)
    }
}

/// The manifest of a workspace.
#[derive(Default, Debug)]
#[non_exhaustive]
pub struct Manifest {
    /// List of packages found.
    pub packages: Vec<Package>,
}

impl Manifest {
    fn find_paths<'m>(
        &'m self,
        m: WorkspaceFilter<'_>,
        kind: FoundKind,
        auto_path: &Path,
        auto_find: fn(&Package) -> bool,
    ) -> Result<Vec<FoundPackage<'m>>> {
        let mut output = Vec::new();

        for package in self.packages.iter() {
            for found in package.find_paths(m, kind, auto_path, auto_find)? {
                output.try_push(FoundPackage { found, package })?;
            }
        }

        Ok(output)
    }

    /// Find every single entrypoint available.
    pub fn find_all(&self, m: WorkspaceFilter<'_>) -> Result<Vec<FoundPackage<'_>>> {
        let mut output = Vec::new();
        output.try_extend(self.find_bins(m)?)?;
        output.try_extend(self.find_tests(m)?)?;
        output.try_extend(self.find_examples(m)?)?;
        output.try_extend(self.find_benches(m)?)?;
        Ok(output)
    }

    /// Find all binaries matching the given name in the workspace.
    pub fn find_bins(&self, m: WorkspaceFilter<'_>) -> Result<Vec<FoundPackage<'_>>> {
        self.find_paths(m, FoundKind::Binary, Path::new(BIN), |p| p.auto_bins)
    }

    /// Find all tests associated with the given base name.
    pub fn find_tests(&self, m: WorkspaceFilter<'_>) -> Result<Vec<FoundPackage<'_>>> {
        self.find_paths(m, FoundKind::Test, Path::new(TESTS), |p| p.auto_tests)
    }

    /// Find all examples matching the given name in the workspace.
    pub fn find_examples(&self, m: WorkspaceFilter<'_>) -> Result<Vec<FoundPackage<'_>>> {
        self.find_paths(m, FoundKind::Example, Path::new(EXAMPLES), |p| {
            p.auto_examples
        })
    }

    /// Find all benches matching the given name in the workspace.
    pub fn find_benches(&self, m: WorkspaceFilter<'_>) -> Result<Vec<FoundPackage<'_>>> {
        self.find_paths(m, FoundKind::Bench, Path::new(BENCHES), |p| p.auto_benches)
    }
}

/// A single package.
#[derive(Debug)]
#[non_exhaustive]
pub struct Package {
    /// The name of the package.
    pub name: String,
    /// The version of the package..
    pub version: Version,
    /// The root of the package.
    pub root: Option<PathBuf>,
    /// Automatically detect binaries.
    pub auto_bins: bool,
    /// Automatically detect tests.
    pub auto_tests: bool,
    /// Automatically detect examples.
    pub auto_examples: bool,
    /// Automatically detect benches.
    pub auto_benches: bool,
}

impl Package {
    fn find_paths(
        &self,
        m: WorkspaceFilter<'_>,
        kind: FoundKind,
        auto_path: &Path,
        auto_find: fn(&Package) -> bool,
    ) -> Result<Vec<Found>> {
        let mut output = Vec::new();

        if let (Some(path), true) = (&self.root, auto_find(self)) {
            let path = path.join(auto_path);
            let results = find_rune_files(&path)?;

            for result in results {
                let (path, name) = result?;

                if m.matches(&name) {
                    output.try_push(Found { kind, path, name })?;
                }
            }
        }

        Ok(output)
    }

    /// Find every single entrypoint available.
    pub fn find_all(&self, m: WorkspaceFilter<'_>) -> Result<Vec<Found>> {
        let mut output = Vec::new();
        output.try_extend(self.find_bins(m)?)?;
        output.try_extend(self.find_tests(m)?)?;
        output.try_extend(self.find_examples(m)?)?;
        output.try_extend(self.find_benches(m)?)?;
        Ok(output)
    }

    /// Find all binaries matching the given name in the workspace.
    pub fn find_bins(&self, m: WorkspaceFilter<'_>) -> Result<Vec<Found>> {
        self.find_paths(m, FoundKind::Binary, Path::new(BIN), |p| p.auto_bins)
    }

    /// Find all tests associated with the given base name.
    pub fn find_tests(&self, m: WorkspaceFilter<'_>) -> Result<Vec<Found>> {
        self.find_paths(m, FoundKind::Test, Path::new(TESTS), |p| p.auto_tests)
    }

    /// Find all examples matching the given name in the workspace.
    pub fn find_examples(&self, m: WorkspaceFilter<'_>) -> Result<Vec<Found>> {
        self.find_paths(m, FoundKind::Example, Path::new(EXAMPLES), |p| {
            p.auto_examples
        })
    }

    /// Find all benches matching the given name in the workspace.
    pub fn find_benches(&self, m: WorkspaceFilter<'_>) -> Result<Vec<Found>> {
        self.find_paths(m, FoundKind::Bench, Path::new(BENCHES), |p| p.auto_benches)
    }
}

pub(crate) struct Loader<'a> {
    id: SourceId,
    sources: &'a mut Sources,
    diagnostics: &'a mut Diagnostics,
    source_loader: &'a mut dyn SourceLoader,
    manifest: &'a mut Manifest,
}

impl<'a> Loader<'a> {
    pub(crate) fn new(
        id: SourceId,
        sources: &'a mut Sources,
        diagnostics: &'a mut Diagnostics,
        source_loader: &'a mut dyn SourceLoader,
        manifest: &'a mut Manifest,
    ) -> Self {
        Self {
            id,
            sources,
            diagnostics,
            source_loader,
            manifest,
        }
    }

    /// Load a manifest.
    pub(crate) fn load_manifest(&mut self) -> Result<()> {
        let Some(source) = self.sources.get(self.id) else {
            self.fatal(WorkspaceError::new(
                Span::empty(),
                WorkspaceErrorKind::MissingSourceId { source_id: self.id },
            ))?;
            return Ok(());
        };

        let value: SpannedValue = match toml::from_str(source.as_str()) {
            Ok(value) => value,
            Err(e) => {
                let span = match e.span() {
                    Some(span) => Span::new(span.start, span.end),
                    None => Span::new(0, source.len()),
                };

                self.fatal(WorkspaceError::new(span, e))?;
                return Ok(());
            }
        };

        let root = source
            .path()
            .and_then(|p| p.parent().map(TryToOwned::try_to_owned))
            .transpose()?;
        let root = root.as_deref();

        let Some((mut table, _)) = self.ensure_table(value)? else {
            return Ok(());
        };

        // If manifest is a package, add it here.
        if let Some((package, span)) = table
            .remove("package")
            .map(|value| self.ensure_table(value))
            .transpose()?
            .flatten()
        {
            if let Some(package) = self.load_package(package, span, root)? {
                self.manifest.packages.try_push(package)?;
            }
        }

        // Load the [workspace] section.
        if let Some((mut table, span)) = table
            .remove("workspace")
            .map(|value| self.ensure_table(value))
            .transpose()?
            .flatten()
        {
            match &root {
                Some(root) => {
                    if let Some(members) = self.load_members(&mut table, root)? {
                        for (span, path) in members {
                            self.load_member(span, &path)?;
                        }
                    }
                }
                None => {
                    self.fatal(WorkspaceError::new(
                        span,
                        WorkspaceErrorKind::MissingManifestPath,
                    ))?;
                }
            }

            self.ensure_empty(table)?;
        }

        self.ensure_empty(table)?;
        Ok(())
    }

    /// Load members from the given workspace configuration.
    fn load_members(
        &mut self,
        table: &mut Table,
        root: &Path,
    ) -> Result<Option<Vec<(Span, PathBuf)>>> {
        let Some(members) = table.remove("members") else {
            return Ok(None);
        };

        let Some((members, _)) = self.ensure_array(members)? else {
            return Ok(None);
        };

        let mut output = Vec::new();

        for value in members {
            let span = Spanned::span(&value);

            match deserialize::<RelativePathBuf>(value) {
                Ok(member) => {
                    self.glob_relative_path(&mut output, span, &member, root)?;
                }
                Err(error) => {
                    self.fatal(error)?;
                }
            };
        }

        Ok(Some(output))
    }

    /// Glob a relative path.
    ///
    /// Currently only supports expanding `*` and required interacting with the
    /// filesystem.
    fn glob_relative_path(
        &mut self,
        output: &mut Vec<(Span, PathBuf)>,
        span: Span,
        member: &RelativePath,
        root: &Path,
    ) -> Result<()> {
        let glob = glob::Glob::new(root, member)?;

        for m in glob.matcher()? {
            let Some(mut path) = self.glob_error(span, root, m)? else {
                continue;
            };

            path.push(MANIFEST_FILE);

            if !path.is_file() {
                continue;
            }

            output.try_push((span, path))?;
        }

        Ok(())
    }

    /// Helper to convert an [io::Error] into a [WorkspaceErrorKind::SourceError].
    fn glob_error<T>(
        &mut self,
        span: Span,
        path: &Path,
        result: Result<T, glob::GlobError>,
    ) -> alloc::Result<Option<T>> {
        Ok(match result {
            Ok(result) => Some(result),
            Err(error) => {
                self.fatal(WorkspaceError::new(
                    span,
                    WorkspaceErrorKind::GlobError {
                        path: path.try_into()?,
                        error,
                    },
                ))?;

                None
            }
        })
    }

    /// Try to load the given path as a member in the current manifest.
    fn load_member(&mut self, span: Span, path: &Path) -> Result<()> {
        let source = match self.source_loader.load(span, path) {
            Ok(source) => source,
            Err(error) => {
                self.fatal(error)?;
                return Ok(());
            }
        };

        let id = self.sources.insert(source)?;
        let old = std::mem::replace(&mut self.id, id);
        self.load_manifest()?;
        self.id = old;
        Ok(())
    }

    /// Load a package from a value.
    fn load_package(
        &mut self,
        mut table: Table,
        span: Span,
        root: Option<&Path>,
    ) -> alloc::Result<Option<Package>> {
        let name = self.field(&mut table, span, "name")?;
        let version = self.field(&mut table, span, "version")?;
        self.ensure_empty(table)?;

        let (Some(name), Some(version)) = (name, version) else {
            return Ok(None);
        };

        Ok(Some(Package {
            name,
            version,
            root: root.map(|p| p.into()),
            auto_bins: true,
            auto_tests: true,
            auto_examples: true,
            auto_benches: true,
        }))
    }

    /// Ensure that a table is empty and mark any additional elements as erroneous.
    fn ensure_empty(&mut self, table: Table) -> alloc::Result<()> {
        for (key, _) in table {
            let span = Spanned::span(&key);
            self.fatal(WorkspaceError::new(
                span,
                WorkspaceErrorKind::UnsupportedKey {
                    key: key.get_ref().as_str().try_into()?,
                },
            ))?;
        }

        Ok(())
    }

    /// Ensure that value is a table.
    fn ensure_table(&mut self, value: SpannedValue) -> alloc::Result<Option<(Table, Span)>> {
        let span = Spanned::span(&value);

        Ok(match value.into_inner() {
            Value::Table(table) => Some((table, span)),
            _ => {
                let error = WorkspaceError::new(span, WorkspaceErrorKind::ExpectedTable);
                self.fatal(error)?;
                None
            }
        })
    }

    /// Coerce into an array or error.
    fn ensure_array(&mut self, value: SpannedValue) -> alloc::Result<Option<(Array, Span)>> {
        let span = Spanned::span(&value);

        Ok(match value.into_inner() {
            Value::Array(array) => Some((array, span)),
            _ => {
                let error = WorkspaceError::expected_array(span);
                self.fatal(error)?;
                None
            }
        })
    }

    /// Helper to load a single field.
    fn field<T>(
        &mut self,
        table: &mut Table,
        span: Span,
        field: &'static str,
    ) -> alloc::Result<Option<T>>
    where
        T: for<'de> Deserialize<'de>,
    {
        Ok(match table.remove(field) {
            Some(value) => match deserialize(value) {
                Ok(value) => Some(value),
                Err(error) => {
                    self.fatal(error)?;
                    None
                }
            },
            None => {
                let error = WorkspaceError::missing_field(span, field);
                self.fatal(error)?;
                None
            }
        })
    }

    /// Report a fatal diagnostic.
    fn fatal(&mut self, error: WorkspaceError) -> alloc::Result<()> {
        self.diagnostics.fatal(self.id, error)
    }
}

/// Helper to load a single field.
fn deserialize<T>(value: SpannedValue) -> Result<T, WorkspaceError>
where
    T: for<'de> Deserialize<'de>,
{
    let span = Spanned::span(&value);
    let f = key::to_key(value.get_ref()).map_err(|e| WorkspaceError::new(span, e))?;
    let deserializer = f.into_deserializer();
    let value = T::deserialize(deserializer).map_err(|e| WorkspaceError::new(span, e))?;
    Ok(value)
}

/// Find all rune files in the given path.
fn find_rune_files(path: &Path) -> Result<impl Iterator<Item = Result<(PathBuf, String)>>> {
    let mut dir = match fs::read_dir(path) {
        Ok(dir) => Some(dir),
        Err(e) if e.kind() == io::ErrorKind::NotFound => None,
        Err(e) => return Err(e.into()),
    };

    Ok(iter::from_fn(move || loop {
        let e = dir.as_mut()?.next()?;

        let e = match e {
            Ok(e) => e,
            Err(err) => return Some(Err(err.into())),
        };

        let m = match e.metadata() {
            Ok(m) => m,
            Err(err) => return Some(Err(err.into())),
        };

        if !m.is_file() {
            continue;
        }

        let path = e.path();

        let (Some(name), Some(ext)) = (path.file_stem().and_then(OsStr::to_str), path.extension())
        else {
            continue;
        };

        if ext != OsStr::new("rn") {
            continue;
        }

        let name = match String::try_from(name) {
            Ok(name) => name,
            Err(error) => return Some(Err(error.into())),
        };

        return Some(Ok((path, name)));
    }))
}