Skip to content

Commit

Permalink
More getters
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter554 committed Jan 6, 2025
1 parent 412bcc7 commit 5227691
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 47 deletions.
47 changes: 35 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ lazy_static = "1.5.0"
itertools = "0.14.0"
tap = "1.0.1"
derive_more = { version = "1.0.0", features = ["full"] }
derive-getters = "0.5.0"
derive-new = "0.7.0"
derive_builder = "0.20.2"
getset = "0.1.3"

[dev-dependencies]
parameterized = "2.0.0"
Expand Down
9 changes: 5 additions & 4 deletions src/contracts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use crate::{ImportsInfo, PackageItemToken};
use anyhow::Result;
use derive_getters::Getters;
use derive_new::new;
use getset::{CopyGetters, Getters};
use std::collections::HashSet;

pub mod layers;
Expand All @@ -30,15 +30,16 @@ pub enum ContractViolation {
}

/// An import path which is forbidden.
#[derive(Debug, Clone, PartialEq, new, Getters)]
#[derive(Debug, Clone, PartialEq, new, Getters, CopyGetters)]
pub struct ForbiddenImport {
/// The start of the forbidden import path.
#[getter(copy)]
#[getset(get_copy = "pub")]
from: PackageItemToken,
/// The end of the forbidden import path.
#[getter(copy)]
#[getset(get_copy = "pub")]
to: PackageItemToken,
/// Items by which the import path is allowed.
#[new(into)]
#[getset(get = "pub")]
except_via: HashSet<PackageItemToken>,
}
10 changes: 5 additions & 5 deletions src/imports_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ impl ImportsInfo {

// By definition, packages import their init modules.
for package in package_info.get_all_items().filter_packages() {
if let Some(init_module) = package.init_module {
if let Some(init_module) = package.init_module() {
imports_info.add_internal_import(
package.token.into(),
package.token().into(),
init_module.into(),
ImportMetadata::ImplicitImport,
)?;
Expand Down Expand Up @@ -359,21 +359,21 @@ fn get_all_raw_imports(
HashMap::new,
|mut hm: HashMap<PackageItemToken, Vec<ResolvedRawImport>>, module| -> Result<_> {
// Parse the raw imports.
let raw_imports = parse::parse_imports(&module.path)?;
let raw_imports = parse::parse_imports(module.path())?;

// Resolve any relative imports.
let raw_imports = raw_imports
.into_iter()
.map(|o| ResolvedRawImport {
pypath: o
.pypath
.resolve_relative(&module.path, &package_info.get_root().path),
.resolve_relative(module.path(), package_info.get_root().path()),
line_number: o.line_number,
is_typechecking: o.is_typechecking,
})
.collect::<Vec<_>>();

hm.entry(module.token.into())
hm.entry(module.token().into())
.or_default()
.extend(raw_imports);

Expand Down
7 changes: 5 additions & 2 deletions src/imports_info/queries/internal_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::collections::{HashMap, HashSet};

use anyhow::Result;
use derive_builder::Builder;
use derive_getters::Getters;
use derive_new::new;
use getset::Getters;
use pathfinding::prelude::{bfs, bfs_reach};

use crate::{Error, ImportMetadata, ImportsInfo, PackageItemToken};
Expand All @@ -19,10 +19,12 @@ pub struct InternalImportsQueries<'a> {
pub struct InternalImportsPathQuery {
/// Package items from which paths may start.
#[new(into)]
#[getset(get = "pub")]
from: HashSet<PackageItemToken>,

/// Package items where paths may end.
#[new(into)]
#[getset(get = "pub")]
to: HashSet<PackageItemToken>,

/// Paths that would go via these package items should be excluded.
Expand Down Expand Up @@ -86,8 +88,9 @@ pub struct InternalImportsPathQuery {
/// # Ok(())
/// # }
/// ```
#[builder(default)]
#[new(into)]
#[getset(get = "pub")]
#[builder(default)]
excluding_paths_via: HashSet<PackageItemToken>,
}

Expand Down
55 changes: 34 additions & 21 deletions src/package_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::Error;
use crate::{IntoPypath, Pypath};
use anyhow::Result;
use core::fmt;
use getset::{CopyGetters, Getters};
use maplit::hashset;
pub use queries::PackageItemIterator;
use slotmap::{new_key_type, SlotMap};
Expand All @@ -28,23 +29,30 @@ new_key_type! {

/// A python package.
/// See also [PackageItem].
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Getters, CopyGetters)]
pub struct Package {
/// The absolute filesystem path to this package.
pub path: PathBuf,
#[getset(get = "pub")]
path: PathBuf,
/// The absolute pypath to this package.
pub pypath: Pypath,
#[getset(get = "pub")]
pypath: Pypath,

/// This package.
pub token: PackageToken,
#[getset(get_copy = "pub")]
token: PackageToken,
/// The parent package.
pub parent: Option<PackageToken>,
#[getset(get_copy = "pub")]
parent: Option<PackageToken>,
/// Child packages.
pub packages: HashSet<PackageToken>,
#[getset(get = "pub")]
packages: HashSet<PackageToken>,
/// Child modules.
pub modules: HashSet<ModuleToken>,
#[getset(get = "pub")]
modules: HashSet<ModuleToken>,
/// The init module.
pub init_module: Option<ModuleToken>,
#[getset(get_copy = "pub")]
init_module: Option<ModuleToken>,
}

impl fmt::Display for Package {
Expand Down Expand Up @@ -75,19 +83,24 @@ impl Package {

/// A python module.
/// See also [PackageItem].
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Getters, CopyGetters)]
pub struct Module {
/// The absolute filesystem path to this module.
pub path: PathBuf,
#[getset(get = "pub")]
path: PathBuf,
/// The absolute pypath to this module.
pub pypath: Pypath,
#[getset(get = "pub")]
pypath: Pypath,
/// True if this is an init module.
pub is_init: bool,
#[getset(get_copy = "pub")]
is_init: bool,

/// This module.
pub token: ModuleToken,
#[getset(get_copy = "pub")]
token: ModuleToken,
/// The parent package.
pub parent: PackageToken,
#[getset(get_copy = "pub")]
parent: PackageToken,
}

impl fmt::Display for Module {
Expand Down Expand Up @@ -151,13 +164,13 @@ impl Module {
/// ```
#[derive(Debug, Clone)]
pub struct PackageInfo {
pub(crate) root: PackageToken,
pub(crate) packages: SlotMap<PackageToken, Package>,
pub(crate) modules: SlotMap<ModuleToken, Module>,
pub(crate) packages_by_path: HashMap<PathBuf, PackageToken>,
pub(crate) packages_by_pypath: HashMap<Pypath, PackageToken>,
pub(crate) modules_by_path: HashMap<PathBuf, ModuleToken>,
pub(crate) modules_by_pypath: HashMap<Pypath, ModuleToken>,
root: PackageToken,
packages: SlotMap<PackageToken, Package>,
modules: SlotMap<ModuleToken, Module>,
packages_by_path: HashMap<PathBuf, PackageToken>,
packages_by_pypath: HashMap<Pypath, PackageToken>,
modules_by_path: HashMap<PathBuf, ModuleToken>,
modules_by_pypath: HashMap<Pypath, ModuleToken>,
}

/// A unified representation of an item within a package.
Expand Down
4 changes: 2 additions & 2 deletions src/package_info/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl PackageInfo {
/// # };
/// # let package_info = PackageInfo::build(test_package.path()).unwrap();
/// let children = package_info
/// .get_child_items(package_info.get_root().token)?
/// .get_child_items(package_info.get_root().token())?
/// .collect::<Vec<PackageItem>>();
/// # Ok(())
/// # }
Expand Down Expand Up @@ -214,7 +214,7 @@ impl PackageInfo {
/// # };
/// # let package_info = PackageInfo::build(test_package.path()).unwrap();
/// let descendants = package_info
/// .get_descendant_items(package_info.get_root().token)?
/// .get_descendant_items(package_info.get_root().token())?
/// .collect::<Vec<PackageItem>>();
/// # Ok(())
/// # }
Expand Down

0 comments on commit 5227691

Please sign in to comment.