Skip to content

Commit

Permalink
Remove IntoPypath
Browse files Browse the repository at this point in the history
It's too fancy/magic
  • Loading branch information
Peter554 committed Jan 11, 2025
1 parent cfb1481 commit 33d7973
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 155 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn main() -> Result<()> {
let imports_info = ImportsInfo::build(package_info)?;

let item = |pypath: &str| -> Result<PackageItemToken> {
Ok(imports_info.package_info().get_item_by_pypath(pypath)?.unwrap().token())
Ok(imports_info.package_info().get_item_by_pypath(&pypath.parse()?).unwrap().token())
};

let root_pkg = item("testpackage")?;
Expand Down
12 changes: 6 additions & 6 deletions src/contracts/independent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
//! let package_info = PackageInfo::build(testpackage.path())?;
//! let imports_info = ImportsInfo::build(package_info)?;
//!
//! let a = imports_info.package_info().get_item_by_pypath("testpackage.a")?.unwrap().token();
//! let b = imports_info.package_info().get_item_by_pypath("testpackage.b")?.unwrap().token();
//! let a = imports_info.package_info().get_item_by_pypath(&"testpackage.a".parse()?).unwrap().token();
//! let b = imports_info.package_info().get_item_by_pypath(&"testpackage.b".parse()?).unwrap().token();
//!
//! let contract = IndependentItemsContract::new(&[a, b]);
//!
Expand Down Expand Up @@ -59,10 +59,10 @@
//! let package_info = PackageInfo::build(testpackage.path())?;
//! let imports_info = ImportsInfo::build(package_info)?;
//!
//! let a = imports_info.package_info().get_item_by_pypath("testpackage.a")?.unwrap().token();
//! let b = imports_info.package_info().get_item_by_pypath("testpackage.b")?.unwrap().token();
//! let c = imports_info.package_info().get_item_by_pypath("testpackage.c")?.unwrap().token();
//! let d = imports_info.package_info().get_item_by_pypath("testpackage.d")?.unwrap().token();
//! let a = imports_info.package_info().get_item_by_pypath(&"testpackage.a".parse()?).unwrap().token();
//! let b = imports_info.package_info().get_item_by_pypath(&"testpackage.b".parse()?).unwrap().token();
//! let c = imports_info.package_info().get_item_by_pypath(&"testpackage.c".parse()?).unwrap().token();
//! let d = imports_info.package_info().get_item_by_pypath(&"testpackage.d".parse()?).unwrap().token();
//!
//! let contract = IndependentItemsContract::new(&[a, b]);
//!
Expand Down
16 changes: 8 additions & 8 deletions src/contracts/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
//! let package_info = PackageInfo::build(testpackage.path())?;
//! let imports_info = ImportsInfo::build(package_info)?;
//!
//! let data = imports_info.package_info().get_item_by_pypath("testpackage.data")?.unwrap().token();
//! let domain = imports_info.package_info().get_item_by_pypath("testpackage.domain")?.unwrap().token();
//! let application = imports_info.package_info().get_item_by_pypath("testpackage.application")?.unwrap().token();
//! let interfaces = imports_info.package_info().get_item_by_pypath("testpackage.interfaces")?.unwrap().token();
//! let data = imports_info.package_info().get_item_by_pypath(&"testpackage.data".parse()?).unwrap().token();
//! let domain = imports_info.package_info().get_item_by_pypath(&"testpackage.domain".parse()?).unwrap().token();
//! let application = imports_info.package_info().get_item_by_pypath(&"testpackage.application".parse()?).unwrap().token();
//! let interfaces = imports_info.package_info().get_item_by_pypath(&"testpackage.interfaces".parse()?).unwrap().token();
//!
//! let contract = LayeredArchitectureContract::new(&[
//! Layer::new([data], true),
Expand Down Expand Up @@ -75,10 +75,10 @@
//! let package_info = PackageInfo::build(testpackage.path())?;
//! let imports_info = ImportsInfo::build(package_info)?;
//!
//! let data = imports_info.package_info().get_item_by_pypath("testpackage.data")?.unwrap().token();
//! let domain = imports_info.package_info().get_item_by_pypath("testpackage.domain")?.unwrap().token();
//! let application = imports_info.package_info().get_item_by_pypath("testpackage.application")?.unwrap().token();
//! let interfaces = imports_info.package_info().get_item_by_pypath("testpackage.interfaces")?.unwrap().token();
//! let data = imports_info.package_info().get_item_by_pypath(&"testpackage.data".parse()?).unwrap().token();
//! let domain = imports_info.package_info().get_item_by_pypath(&"testpackage.domain".parse()?).unwrap().token();
//! let application = imports_info.package_info().get_item_by_pypath(&"testpackage.application".parse()?).unwrap().token();
//! let interfaces = imports_info.package_info().get_item_by_pypath(&"testpackage.interfaces".parse()?).unwrap().token();
//!
//! let contract = LayeredArchitectureContract::new(&[
//! Layer::new([data], true),
Expand Down
10 changes: 5 additions & 5 deletions src/imports_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ pub enum ImportMetadata {
/// let imports_info = ImportsInfo::build(package_info)?;
///
/// let root_pkg = imports_info.package_info()
/// .get_item_by_pypath("testpackage")?.unwrap()
/// .get_item_by_pypath(&"testpackage".parse()?).unwrap()
/// .token();
/// let root_init = imports_info.package_info()
/// .get_item_by_pypath("testpackage.__init__")?.unwrap()
/// .get_item_by_pypath(&"testpackage.__init__".parse()?).unwrap()
/// .token();
/// let a = imports_info.package_info()
/// .get_item_by_pypath("testpackage.a")?.unwrap()
/// .get_item_by_pypath(&"testpackage.a".parse()?).unwrap()
/// .token();
///
/// assert_eq!(
Expand Down Expand Up @@ -182,13 +182,13 @@ impl ImportsInfo {
if raw_import.pypath.is_internal(&package_info) {
let internal_item = {
if let Some(item) = package_info
.get_item_by_pypath(&raw_import.pypath)?
.get_item_by_pypath(&raw_import.pypath)
.map(|item| item.token())
{
// An imported module.
item
} else if let Some(item) = package_info
.get_item_by_pypath(raw_import.pypath.parent())?
.get_item_by_pypath(&raw_import.pypath.parent())
.map(|item| item.token())
{
// An imported module member.
Expand Down
53 changes: 22 additions & 31 deletions src/imports_info/queries/external_imports.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use std::{
borrow::Borrow,
collections::{HashMap, HashSet},
};
use std::collections::{HashMap, HashSet};

use crate::errors::Error;
use crate::imports_info::{ImportMetadata, ImportsInfo};
use crate::package_info::PackageItemToken;
use crate::prelude::*;
use crate::pypath::Pypath;
use anyhow::Result;

Expand Down Expand Up @@ -36,13 +32,13 @@ impl<'a> ExternalImportsQueries<'a> {
/// let imports_info = ImportsInfo::build(package_info)?;
///
/// let root_pkg = imports_info.package_info()
/// .get_item_by_pypath("testpackage")?.unwrap()
/// .get_item_by_pypath(&"testpackage".parse()?).unwrap()
/// .token();
/// let root_init = imports_info.package_info()
/// .get_item_by_pypath("testpackage.__init__")?.unwrap()
/// .get_item_by_pypath(&"testpackage.__init__".parse()?).unwrap()
/// .token();
/// let a = imports_info.package_info()
/// .get_item_by_pypath("testpackage.a")?.unwrap()
/// .get_item_by_pypath(&"testpackage.a".parse()?).unwrap()
/// .token();
///
/// assert_eq!(
Expand Down Expand Up @@ -80,36 +76,30 @@ impl<'a> ExternalImportsQueries<'a> {
/// let imports_info = ImportsInfo::build(package_info)?;
///
/// let root_init = imports_info.package_info()
/// .get_item_by_pypath("testpackage.__init__")?.unwrap()
/// .get_item_by_pypath(&"testpackage.__init__".parse()?).unwrap()
/// .token();
/// let a = imports_info.package_info()
/// .get_item_by_pypath("testpackage.a")?.unwrap()
/// .get_item_by_pypath(&"testpackage.a".parse()?).unwrap()
/// .token();
///
/// assert!(
/// imports_info.external_imports().direct_import_exists(a, "django.db.models")?,
/// imports_info.external_imports().direct_import_exists(a, &"django.db.models".parse()?)?,
/// );
/// assert!(
/// !imports_info.external_imports().direct_import_exists(root_init, "django.db.models")?,
/// !imports_info.external_imports().direct_import_exists(root_init, &"django.db.models".parse()?)?,
/// );
/// # Ok(())
/// # }
/// ```
pub fn direct_import_exists<T: IntoPypath>(
&self,
from: PackageItemToken,
to: T,
) -> Result<bool> {
let to = to.into_pypath()?;

pub fn direct_import_exists(&self, from: PackageItemToken, to: &Pypath) -> Result<bool> {
self.imports_info.package_info.get_item(from)?;

Ok(self
.imports_info
.external_imports
.get(&from)
.unwrap()
.contains(to.borrow()))
.contains(to))
}

/// Returns the items directly imported by the passed package item.
Expand All @@ -132,7 +122,7 @@ impl<'a> ExternalImportsQueries<'a> {
/// let imports_info = ImportsInfo::build(package_info)?;
///
/// let a = imports_info.package_info()
/// .get_item_by_pypath("testpackage.a")?.unwrap()
/// .get_item_by_pypath(&"testpackage.a".parse()?).unwrap()
/// .token();
///
/// assert_eq!(
Expand Down Expand Up @@ -183,7 +173,7 @@ impl<'a> ExternalImportsQueries<'a> {
/// let imports_info = ImportsInfo::build(package_info)?;
///
/// let a = imports_info.package_info()
/// .get_item_by_pypath("testpackage.a")?.unwrap()
/// .get_item_by_pypath(&"testpackage.a".parse()?).unwrap()
/// .token();
///
/// assert_eq!(
Expand Down Expand Up @@ -243,11 +233,11 @@ impl<'a> ExternalImportsQueries<'a> {
/// let imports_info = ImportsInfo::build(package_info)?;
///
/// let a = imports_info.package_info()
/// .get_item_by_pypath("testpackage.a")?.unwrap()
/// .get_item_by_pypath(&"testpackage.a".parse()?).unwrap()
/// .token();
///
/// assert_eq!(
/// imports_info.external_imports().get_import_metadata(a, "django.db.models")?,
/// imports_info.external_imports().get_import_metadata(a, &"django.db.models".parse()?)?,
/// &ImportMetadata::ExplicitImport {
/// line_number: 1,
/// is_typechecking: false
Expand All @@ -256,23 +246,23 @@ impl<'a> ExternalImportsQueries<'a> {
/// # Ok(())
/// # }
/// ```
pub fn get_import_metadata<T: IntoPypath>(
pub fn get_import_metadata(
&'a self,
from: PackageItemToken,
to: T,
to: &Pypath,
) -> Result<&'a ImportMetadata> {
let to = to.into_pypath()?;
if self.direct_import_exists(from, to.borrow())? {
if self.direct_import_exists(from, to)? {
Ok(self
.imports_info
.external_imports_metadata
.get(&(from, to.borrow().clone()))
.get(&(from, to.clone()))
.unwrap())
} else {
Err(Error::NoSuchImport)?
}
}

#[allow(dead_code)]
fn get_equal_to_or_descendant_imports(&self, pypath: &Pypath) -> HashSet<Pypath> {
self.imports_info
.external_imports
Expand Down Expand Up @@ -362,7 +352,7 @@ mod tests {

let a = imports_info
.package_info()
.get_item_by_pypath("testpackage.a")?
.get_item_by_pypath(&"testpackage.a".parse()?)
.unwrap()
.token();

Expand Down Expand Up @@ -390,7 +380,8 @@ mod tests {
let root_package_init = imports_info._item("testpackage.__init__");

let external_imports = imports_info.external_imports();
let metadata = external_imports.get_import_metadata(root_package_init, "pydantic")?;
let metadata =
external_imports.get_import_metadata(root_package_init, &"pydantic".parse()?)?;

assert_eq!(
metadata,
Expand Down
Loading

0 comments on commit 33d7973

Please sign in to comment.