swamp-dep-loader 0.2.19

Parses Swamp modules, scans for dependencies (`mod` and `use`), and determines the correct analysis order.
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
/*
 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
 * Licensed under the MIT License. See LICENSE in the project root for license information.
 */
pub mod prelude;
use dirs::home_dir;
use seq_map::SeqMap;
use source_map_cache::{FileId, SourceMap};
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::{env, io};
use swamp_ast::prelude::*;
use swamp_parser::{AstParser, SpecificError};
use time_dilation::ScopedTimer;
use tracing::error;

pub struct ParseRoot;

#[derive(Debug)]
pub enum ParseRootError {
    ParserError(ParserError),
}

#[derive(Debug)]
pub struct ParsedAstModule {
    pub ast_module: swamp_ast::Module,
    pub file_id: FileId,
}

#[derive(Debug)]
pub struct RelativePath(pub String);

#[derive(Debug)]
pub struct ParserError {
    pub node: Node,
    pub specific: SpecificError,
    pub file_id: FileId,
}

impl Default for ParseRoot {
    fn default() -> Self {
        Self::new()
    }
}

impl ParseRoot {
    #[must_use]
    pub const fn new() -> Self {
        Self {}
    }

    pub fn parse(
        &self,
        contents: String,
        file_id: FileId,
    ) -> Result<ParsedAstModule, ParseRootError> {
        let ast_program = AstParser.parse_module(&contents).map_err(|err| {
            let new_err = ParserError {
                node: Node { span: err.span },
                specific: err.specific,
                file_id,
            };
            ParseRootError::ParserError(new_err)
        })?;

        Ok(ParsedAstModule {
            ast_module: ast_program,
            file_id,
        })
    }
}

#[derive(Clone)]
#[allow(unused)]
pub struct ModuleInfo {
    path: Vec<String>,
    imports: Vec<Vec<String>>,
    uses: Vec<Vec<String>>,
    parsed: bool,
    analyzed: bool,
}

pub struct DependencyParser {
    pub import_scanned_modules: SeqMap<Vec<String>, ModuleInfo>,
    already_parsed_modules: SeqMap<Vec<String>, ParsedAstModule>,
    pub already_resolved_modules: HashSet<Vec<String>>,
}

impl Default for DependencyParser {
    fn default() -> Self {
        Self::new()
    }
}

impl DependencyParser {
    #[must_use]
    pub fn new() -> Self {
        Self {
            import_scanned_modules: SeqMap::new(),
            already_parsed_modules: SeqMap::new(),
            already_resolved_modules: HashSet::new(),
        }
    }

    pub fn add_resolved_module(&mut self, module_path: Vec<String>) {
        self.already_resolved_modules.insert(module_path);
    }

    pub fn add_ast_module(&mut self, module_path: Vec<String>, parsed_module: ParsedAstModule) {
        self.already_parsed_modules
            .insert(module_path, parsed_module)
            .expect("insert");
    }
}

#[derive(Debug)]
pub enum DependencyError {
    CircularDependency(Vec<String>),
    ParseRootError(ParseRootError),
    ReadFileError(io::Error),
}

impl From<ParseRootError> for DependencyError {
    fn from(err: ParseRootError) -> Self {
        Self::ParseRootError(err)
    }
}

pub const LOCAL_ROOT_PACKAGE_PATH: &str = "crate";

#[must_use]
pub fn get_all_local_paths(
    source_map: &SourceMap,
    parsed_module: &ParsedAstModule,
) -> (Vec<Vec<String>>, Vec<Vec<String>>) {
    let mut imports = vec![];
    let mut uses = vec![];

    for def in parsed_module.ast_module.definitions() {
        match &def.kind {
            DefinitionKind::Mod(import) => {
                let mut sections = Vec::new();
                sections.push(LOCAL_ROOT_PACKAGE_PATH.to_string());
                for section_node in &import.module_path.0 {
                    let import_path = source_map
                        .get_span_source(
                            parsed_module.file_id,
                            section_node.span.offset as usize,
                            section_node.span.length.into(),
                        )
                        .to_string();
                    sections.push(import_path);
                }

                imports.push(sections);
            }

            DefinitionKind::Use(import) => {
                let mut sections = Vec::new();
                for section_node in &import.module_path.0 {
                    let import_path = source_map
                        .get_span_source(
                            parsed_module.file_id,
                            section_node.span.offset as usize,
                            section_node.span.length.into(),
                        )
                        .to_string();
                    sections.push(import_path);
                }
                uses.push(sections);
            }
            _ => continue,
        }
    }

    (imports, uses)
}

#[must_use]
pub fn module_path_to_relative_swamp_file(module_path_vec: &[String]) -> PathBuf {
    let mut path_buf = PathBuf::new();

    let orig_len = module_path_vec.len();

    let converted_path = if module_path_vec[0] == "crate" {
        &module_path_vec[1..]
    } else {
        module_path_vec
    };

    path_buf.push(converted_path.join("/"));
    if orig_len == 1 {
        path_buf.push("lib"); // lib is default if the path only contains the package root
    }

    path_buf.set_extension("swamp");

    path_buf
}

#[must_use]
pub fn module_path_to_relative_swamp_file_string(module_path_vec: &[String]) -> String {
    module_path_to_relative_swamp_file(module_path_vec)
        .to_str()
        .unwrap()
        .into()
}

#[must_use]
pub fn mount_name_from_path(path: &[String]) -> &str {
    if path[0] == "crate" {
        "crate"
    } else {
        "registry"
    }
}

/// Parses just a single module. Any `mod` keywords in this file will be ignored. So this
/// is mainly for internal use.
pub fn parse_single_module(
    source_map: &mut SourceMap,
    module_path: &[String],
) -> Result<ParsedAstModule, DependencyError> {
    let debug = format!("parse module {module_path:?}");
    let _parse_module_timer = ScopedTimer::new(&debug);

    let mount_name = mount_name_from_path(module_path);

    let (file_id, script) = source_map
        .read_file_relative(
            mount_name,
            &module_path_to_relative_swamp_file_string(module_path),
        )
        .map_err(DependencyError::ReadFileError)?;

    let parse_module = ParseRoot.parse(script, file_id)?;

    Ok(parse_module)
}

pub fn parse_single_module_from_text(
    source_map: &mut SourceMap,
    module_path: &[String],
    script: &str,
) -> Result<ParsedAstModule, DependencyError> {
    let debug = format!("parse module {module_path:?}");
    let _parse_module_timer = ScopedTimer::new(&debug);

    let mount_name = mount_name_from_path(module_path);


    let file_id = source_map.set(
        mount_name,
        module_path_to_relative_swamp_file_string(module_path).as_ref(),
        script,
    );

    let parse_module = ParseRoot.parse(script.to_string(), file_id)?;

    Ok(parse_module)
}

impl DependencyParser {
    pub fn parse_local_modules(
        &mut self,
        module_path: &[String],
        source_map: &mut SourceMap,
    ) -> Result<(), DependencyError> {
        let mut to_parse = vec![module_path.to_vec()];

        while let Some(path) = to_parse.pop() {
            let module_path_vec = &path.clone();
            if self.import_scanned_modules.contains_key(module_path_vec) {
                continue;
            }

            let parsed_module_to_scan =
                if let Some(parsed_module) = self.already_parsed_modules.get(module_path_vec) {
                    parsed_module
                } else if self.already_resolved_modules.contains(module_path_vec) {
                    continue;
                } else if path == ["core"] || path == ["std"] {
                    continue;
                } else {
                    let parsed_ast_module = parse_single_module(source_map, &path)?;

                    self.already_parsed_modules
                        .insert(path.clone(), parsed_ast_module)
                        .expect("insert");

                    self.already_parsed_modules
                        .get(&path.clone())
                        .expect("we just inserted it")
                };

            let (imports, uses) = get_all_local_paths(source_map, parsed_module_to_scan);
            let filtered_imports: Vec<Vec<String>> = imports
                .into_iter()
                .filter(|import| !self.already_resolved_modules.contains(import))
                .collect();

            let filtered_uses: Vec<Vec<String>> = uses
                .into_iter()
                .filter(|import| !self.already_resolved_modules.contains(import))
                .collect();

            self.import_scanned_modules
                .insert(
                    path.clone(),
                    ModuleInfo {
                        path: path.clone(),
                        imports: filtered_imports.clone(),
                        uses: filtered_uses.clone(),
                        parsed: false,
                        analyzed: false,
                    },
                )
                .expect("insert");

            to_parse.extend(filtered_imports.clone());

            to_parse.extend(filtered_uses.clone());
        }
        Ok(())
    }

    #[must_use]
    pub fn get_parsed_module(&self, path: &[String]) -> Option<&ParsedAstModule> {
        self.already_parsed_modules.get(&path.to_vec())
    }

    pub fn get_parsed_module_mut(&mut self, path: &[String]) -> Option<&mut ParsedAstModule> {
        self.already_parsed_modules.get_mut(&path.to_vec())
    }

    pub(crate) fn get_analysis_order(&self) -> Result<Vec<Vec<String>>, DependencyError> {
        let mut order = Vec::new();
        let mut visited = HashSet::new();
        let mut temp_visited = HashSet::new();

        fn visit(
            graph: &DependencyParser,
            path: &[String],
            visited: &mut HashSet<Vec<String>>,
            temp_visited: &mut HashSet<Vec<String>>,
            order: &mut Vec<Vec<String>>,
            depth: usize,
        ) -> Result<(), DependencyError> {
            if temp_visited.contains(path) {
                error!(?path, depth, "already visited this path");
                return Err(DependencyError::CircularDependency(Vec::from(path)));
            }

            if visited.contains(path) {
                return Ok(());
            }

            //trace!(?path, depth, "visit path");
            temp_visited.insert(Vec::from(path));

            if let Some(module) = graph.import_scanned_modules.get(&path.to_vec()) {
                for import in &module.uses {
                    visit(graph, import, visited, temp_visited, order, depth + 1)?;
                }
                for import in &module.imports {
                    visit(graph, import, visited, temp_visited, order, depth + 1)?;
                }
            }

            order.push(Vec::from(path));
            visited.insert(Vec::from(path));

            temp_visited.remove(path);

            Ok(())
        }

        for path in self.import_scanned_modules.keys() {
            if !visited.contains(path) {
                visit(self, path, &mut visited, &mut temp_visited, &mut order, 0)?;
            }
        }

        Ok(order)
    }
}

#[derive(Debug)]
pub enum DepLoaderError {
    DependencyError(DependencyError),
}

impl From<DependencyError> for DepLoaderError {
    fn from(e: DependencyError) -> Self {
        Self::DependencyError(e)
    }
}

/// # Errors
///
fn os_home_relative_path(project_name: &str) -> Option<PathBuf> {
    home_dir().map(|home_path| home_path.join(format!(".{project_name}")))
}

fn current_directory_relative_path(project_name: &str) -> PathBuf {
    Path::new(".").join(format!(".{project_name}"))
}

#[must_use]
pub fn path_from_environment_variable() -> Option<PathBuf> {
    env::var("SWAMP_HOME")
        .map(|string_value| Path::new(&string_value).to_path_buf())
        .ok()
}

#[must_use]
pub fn swamp_home(run_mode: &RunMode) -> Option<PathBuf> {
    // First try environment variable
    match run_mode {
        RunMode::Development => {
            path_from_environment_variable().or_else(|| os_home_relative_path("swamp"))
        }
        RunMode::Deployed => Some(Path::new(".").to_path_buf()),
    }
}

// Verifies the structure of swamp_home to make sure if we can use it
#[must_use]
pub fn verify_if_swamp_home_seems_correct(swamp_home: &Path) -> bool {
    let mut swamp_packages_dir = swamp_home.to_path_buf();

    swamp_packages_dir.push("packages");

    swamp_packages_dir.exists() && swamp_packages_dir.is_dir()
}

pub enum RunMode {
    Development,
    Deployed,
}

/// # Errors
///
#[must_use]
pub fn swamp_registry_path(run_mode: &RunMode) -> Option<PathBuf> {
    let swamp_home = swamp_home(run_mode)?;

    if verify_if_swamp_home_seems_correct(&swamp_home) {
        let mut packages_path = swamp_home;
        packages_path.push("packages");

        Some(packages_path)
    } else {
        None
    }
}

pub fn parse_local_modules_and_get_order(
    module_path: &[String],
    dependency_parser: &mut DependencyParser,
    source_map: &mut SourceMap,
) -> Result<Vec<Vec<String>>, DepLoaderError> {
    dependency_parser.parse_local_modules(module_path, source_map)?;

    let module_paths_in_order = dependency_parser.get_analysis_order()?;

    Ok(module_paths_in_order)
}