use std::{
collections::BTreeSet,
path::{Path, PathBuf},
};
use crate::ModulePath;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Location {
file: PathBuf,
line: Option<usize>,
column: Option<usize>,
}
impl Location {
#[must_use]
pub fn new(file: impl AsRef<Path>) -> Self {
Self {
file: file.as_ref().to_path_buf(),
line: None,
column: None,
}
}
pub(crate) fn with_line_column(
file: impl AsRef<Path>,
line: Option<usize>,
column: Option<usize>,
) -> Self {
Self {
file: file.as_ref().to_path_buf(),
line,
column,
}
}
#[must_use]
pub fn file(&self) -> &Path {
&self.file
}
#[must_use]
pub fn line(&self) -> Option<usize> {
self.line
}
#[must_use]
pub fn column(&self) -> Option<usize> {
self.column
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Dependency {
source: ModulePath,
target: ModulePath,
location: Location,
}
impl Dependency {
#[must_use]
pub fn new(source: ModulePath, target: ModulePath, location: Location) -> Self {
Self {
source,
target,
location,
}
}
#[must_use]
pub fn source(&self) -> &ModulePath {
&self.source
}
#[must_use]
pub fn target(&self) -> &ModulePath {
&self.target
}
#[must_use]
pub fn location(&self) -> &Location {
&self.location
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct DependencyGraph {
dependencies: Vec<Dependency>,
directories: Vec<SourceDirectory>,
}
impl DependencyGraph {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn from_dependencies<I>(dependencies: I) -> Self
where
I: IntoIterator<Item = Dependency>,
{
Self {
dependencies: dependencies.into_iter().collect(),
directories: Vec::new(),
}
}
pub fn push(&mut self, dependency: Dependency) {
self.dependencies.push(dependency);
}
#[must_use]
pub fn dependencies(&self) -> &[Dependency] {
&self.dependencies
}
pub(crate) fn push_directory(&mut self, directory: SourceDirectory) {
self.directories.push(directory);
}
#[must_use]
pub fn directories(&self) -> &[SourceDirectory] {
&self.directories
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SourceDirectory {
path: PathBuf,
module: ModulePath,
files: BTreeSet<String>,
child_directories: BTreeSet<String>,
}
impl SourceDirectory {
pub(crate) fn new(
path: impl AsRef<Path>,
module: ModulePath,
files: BTreeSet<String>,
child_directories: BTreeSet<String>,
) -> Self {
Self {
path: path.as_ref().to_path_buf(),
module,
files,
child_directories,
}
}
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
#[must_use]
pub fn module(&self) -> &ModulePath {
&self.module
}
#[must_use]
pub fn files(&self) -> &BTreeSet<String> {
&self.files
}
#[must_use]
pub fn child_directories(&self) -> &BTreeSet<String> {
&self.child_directories
}
}