Skip to content

Commit

Permalink
Merge pull request #457 from gauge-sh/refactoring-12-3
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
emdoyle authored Dec 4, 2024
2 parents ff0410b + 0eea1ad commit d81c433
Show file tree
Hide file tree
Showing 33 changed files with 232 additions and 164 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ install: ## Install the crate as module in the current virtualenv

.PHONY: test
test: ## Run tests
cd python/tests && \
../../$(VENV_BIN)/pytest
$(VENV_BIN)/pytest
cargo test


.PHONY: lint fmt lint-rust lint-python fmt-rust fmt-python
Expand Down
8 changes: 5 additions & 3 deletions src/check_ext.rs → src/commands/check_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ use std::path::{Path, PathBuf};

use thiserror::Error;

use crate::external::{
error::ParsingError, parsing::normalize_package_name, parsing::parse_pyproject_toml,
};
use crate::filesystem::relative_to;
use crate::parsing::external::{normalize_package_name, parse_pyproject_toml};
use crate::{filesystem, imports, parsing};
use crate::{filesystem, imports};

#[derive(Error, Debug)]
pub enum ExternalCheckError {
#[error("Parsing error: {0}")]
Parse(#[from] parsing::error::ParsingError),
Parse(#[from] ParsingError),
#[error("Import parsing error: {0}")]
ImportParse(#[from] imports::ImportParseError),
#[error("IO error: {0}")]
Expand Down
15 changes: 6 additions & 9 deletions src/check_int.rs → src/commands/check_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ use pyo3::{pyclass, pymethods};
use thiserror::Error;

use crate::{
core::{
config::{ProjectConfig, RootModuleTreatment, RuleSetting},
interfaces::InterfaceChecker,
module::{ModuleNode, ModuleTree},
},
core::config::{ProjectConfig, RootModuleTreatment, RuleSetting},
exclusion::{self, is_path_excluded, set_excluded_paths},
filesystem as fs,
imports::{get_project_imports, ImportParseError},
parsing::{self, module::build_module_tree},
interfaces::InterfaceChecker,
modules::{self, build_module_tree, ModuleNode, ModuleTree},
};

#[derive(Error, Debug)]
Expand All @@ -26,7 +23,7 @@ pub enum CheckError {
#[error("Filesystem error: {0}")]
Filesystem(#[from] fs::FileSystemError),
#[error("Module tree error: {0}")]
ModuleTree(#[from] parsing::error::ModuleTreeError),
ModuleTree(#[from] modules::error::ModuleTreeError),
#[error("Exclusion error: {0}")]
Exclusion(#[from] exclusion::PathExclusionError),
}
Expand Down Expand Up @@ -477,8 +474,8 @@ pub fn check(
mod tests {
use super::*;
use crate::core::config::InterfaceConfig;
use crate::core::module::ModuleTree;
use crate::tests::check_int::fixtures::{interface_config, module_tree};
use crate::modules::ModuleTree;
use crate::tests::check_internal::fixtures::{interface_config, module_tree};

use rstest::rstest;

Expand Down
5 changes: 5 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod check_external;
pub mod check_internal;
pub mod report;
pub mod sync;
pub mod test;
2 changes: 1 addition & 1 deletion src/reports.rs → src/commands/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::filesystem::{
file_to_module_path, validate_project_modules, walk_pyfiles, FileSystemError,
};
use crate::imports::{get_project_imports, ImportParseError, NormalizedImport};
use crate::parsing::{error::ModuleTreeError, module::build_module_tree};
use crate::modules::{build_module_tree, error::ModuleTreeError};

struct Dependency {
file_path: PathBuf,
Expand Down
5 changes: 3 additions & 2 deletions src/sync.rs → src/commands/sync.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use thiserror::Error;

use crate::check_int::{check, CheckError};
use crate::commands::check_internal::{check, CheckError};
use crate::core::config::{
global_visibility, DependencyConfig, ModuleConfig, ProjectConfig, RootModuleTreatment,
ROOT_MODULE_SENTINEL_TAG,
};
use crate::filesystem::{self as fs, ROOT_MODULE_SENTINEL_TAG};
use crate::filesystem::{self as fs};
use crate::parsing::config::dump_project_config_to_toml;
use std::collections::HashMap;
use std::path::PathBuf;
Expand Down
3 changes: 1 addition & 2 deletions src/test.rs → src/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use pyo3::{pyclass, pymethods};
use thiserror::Error;

use crate::core::config::{ModuleConfig, ProjectConfig};
use crate::core::module::ModuleTree;
use crate::filesystem::{self as fs};
use crate::imports::get_project_imports;
use crate::parsing::module::build_module_tree;
use crate::modules::{build_module_tree, ModuleTree};

#[derive(Error, Debug)]
pub enum TestError {
Expand Down
5 changes: 3 additions & 2 deletions src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;

use crate::filesystem::{self, ROOT_MODULE_SENTINEL_TAG};
pub const ROOT_MODULE_SENTINEL_TAG: &str = "<root>";
pub const DEFAULT_EXCLUDE_PATHS: [&str; 4] = ["tests", "docs", ".*__pycache__", ".*egg-info"];

// for serde
fn default_true() -> bool {
Expand All @@ -14,7 +15,7 @@ fn default_source_roots() -> Vec<PathBuf> {
}

fn default_excludes() -> Vec<String> {
filesystem::DEFAULT_EXCLUDE_PATHS
DEFAULT_EXCLUDE_PATHS
.iter()
.map(|s| s.to_string())
.collect()
Expand Down
16 changes: 16 additions & 0 deletions src/core/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::io;
use thiserror::Error;

use crate::filesystem::FileSystemError;

#[derive(Error, Debug)]
pub enum ParsingError {
#[error("IO error: {0}")]
Io(#[from] io::Error),
#[error("Filesystem error: {0}")]
Filesystem(#[from] FileSystemError),
#[error("TOML parsing error: {0}")]
TomlParse(#[from] toml::de::Error),
#[error("Missing field in TOML: {0}")]
MissingField(String),
}
3 changes: 1 addition & 2 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod config;
pub mod interfaces;
pub mod module;
pub mod error;
16 changes: 16 additions & 0 deletions src/external/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::io;
use thiserror::Error;

use crate::filesystem::FileSystemError;

#[derive(Error, Debug)]
pub enum ParsingError {
#[error("IO error: {0}")]
Io(#[from] io::Error),
#[error("Filesystem error: {0}")]
Filesystem(#[from] FileSystemError),
#[error("TOML parsing error: {0}")]
TomlParse(#[from] toml::de::Error),
#[error("Missing field in TOML: {0}")]
MissingField(String),
}
2 changes: 2 additions & 0 deletions src/external/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod error;
pub mod parsing;
4 changes: 3 additions & 1 deletion src/parsing/external.rs → src/external/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use toml::Value;

use super::error;

pub type Result<T> = std::result::Result<T, error::ParsingError>;

pub struct ProjectInfo {
pub dependencies: HashSet<String>,
pub source_paths: Vec<PathBuf>,
}

pub fn parse_pyproject_toml(pyproject_path: &Path) -> error::Result<ProjectInfo> {
pub fn parse_pyproject_toml(pyproject_path: &Path) -> Result<ProjectInfo> {
let content = fs::read_to_string(pyproject_path)?;
let toml_value: Value = toml::from_str(&content)?;
let dependencies = extract_dependencies(&toml_value);
Expand Down
5 changes: 1 addition & 4 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ use globset::GlobSetBuilder;
use thiserror::Error;
use walkdir::{DirEntry, WalkDir};

use crate::core::config::ModuleConfig;
use crate::core::config::{ModuleConfig, ROOT_MODULE_SENTINEL_TAG};
use crate::exclusion::is_path_excluded;

pub const ROOT_MODULE_SENTINEL_TAG: &str = "<root>";
pub const DEFAULT_EXCLUDE_PATHS: [&str; 4] = ["tests", "docs", ".*__pycache__", ".*egg-info"];

#[derive(Error, Debug)]
pub enum FileSystemError {
#[error("Encountered unexpected I/O error.\n{0}")]
Expand Down
Empty file removed src/impact/mod.rs
Empty file.
6 changes: 3 additions & 3 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ use ruff_python_ast::{Expr, Mod, Stmt, StmtIf, StmtImport, StmtImportFrom};
use ruff_source_file::Locator;
use thiserror::Error;

use crate::parsing::py_ast::parse_python_source;
use crate::{exclusion, filesystem, parsing};
use crate::python::{error::ParsingError, parsing::parse_python_source};
use crate::{exclusion, filesystem};

#[derive(Error, Debug)]
pub enum ImportParseError {
#[error("Failed to parse project imports.\nFile: {file}\nFailure: {source}")]
Parsing {
file: String,
#[source]
source: parsing::ParsingError,
source: ParsingError,
},
#[error("Failed to parse project imports.\n{0}")]
Filesystem(#[from] filesystem::FileSystemError),
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions src/interfaces/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod check;

pub use check::InterfaceChecker;
Loading

0 comments on commit d81c433

Please sign in to comment.