fallow_core/plugins/
config_dependencies.rs1use std::path::{Path, PathBuf};
5
6use super::{PluginResult, config_parser};
7
8pub(super) fn add_import_referenced_dependencies(
10 result: &mut PluginResult,
11 source: &str,
12 config_path: &Path,
13) {
14 let imports = config_parser::extract_imports(source, config_path);
15 for import in &imports {
16 result
17 .referenced_dependencies
18 .push(crate::resolve::extract_package_name(import));
19 }
20}
21
22#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct RequiredDependency {
27 pub package_name: String,
29 pub config_path: PathBuf,
31 pub line: u32,
33 pub col: u32,
35 pub dev_only: bool,
38 pub provided_by: &'static [&'static str],
41}
42
43impl RequiredDependency {
44 #[must_use]
46 pub(crate) fn at_offset(
47 package_name: String,
48 config_path: &Path,
49 source: &str,
50 offset: usize,
51 dev_only: bool,
52 ) -> Self {
53 let offset = offset.min(source.len());
54 let before = source.get(..offset).unwrap_or_default();
55 let line = before.matches('\n').count() + 1;
56 let line_start = before.rfind('\n').map_or(0, |index| index + 1);
57 Self {
58 package_name,
59 config_path: config_path.to_path_buf(),
60 line: u32::try_from(line).unwrap_or(u32::MAX),
61 col: u32::try_from(offset - line_start).unwrap_or(u32::MAX),
62 dev_only,
63 provided_by: &[],
64 }
65 }
66
67 #[must_use]
69 pub(crate) const fn with_providers(mut self, provided_by: &'static [&'static str]) -> Self {
70 self.provided_by = provided_by;
71 self
72 }
73}