Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions rust/Cargo.lock

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

1 change: 0 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ bimap = "0.6.3"
slotmap = "1.0.7"
getset = "0.1.3"
derive-new = "0.7.0"
lazy_static = "1.5.0"
string-interner = "0.18.0"
thiserror = "2.0.11"
itertools = "0.14.0"
Expand Down
47 changes: 24 additions & 23 deletions rust/src/filesystem.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use itertools::Itertools;
use pyo3::exceptions::{PyFileNotFoundError, PyUnicodeDecodeError};
use pyo3::prelude::*;
use regex::Regex;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
use unindent::unindent;
use lazy_static::lazy_static;


lazy_static! {
static ref ENCODING_RE: Regex = Regex::new(r"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)").unwrap();
}
static ENCODING_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)").unwrap()
});
Comment on lines -12 to +15
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


pub trait FileSystem: Send + Sync {
fn sep(&self) -> String;
fn sep(&self) -> &str;

fn join(&self, components: Vec<String>) -> String;

Expand All @@ -37,16 +38,16 @@ pub struct PyRealBasicFileSystem {
}

impl FileSystem for RealBasicFileSystem {
fn sep(&self) -> String {
std::path::MAIN_SEPARATOR.to_string()
fn sep(&self) -> &str {
std::path::MAIN_SEPARATOR_STR
}

fn join(&self, components: Vec<String>) -> String {
let mut path = PathBuf::new();
for component in components {
path.push(component);
}
path.to_str().unwrap().to_string()
path.to_str().expect("Path components should be valid unicode").to_string()
}

fn split(&self, file_name: &str) -> (String, String) {
Expand All @@ -65,8 +66,8 @@ impl FileSystem for RealBasicFileSystem {
};

(
head.to_str().unwrap().to_string(),
tail.to_str().unwrap().to_string(),
head.to_str().expect("Path components should be valid unicode").to_string(),
tail.to_str().expect("Path components should be valid unicode").to_string(),
)
}

Expand Down Expand Up @@ -136,7 +137,7 @@ impl PyRealBasicFileSystem {
}

#[getter]
fn sep(&self) -> String {
fn sep(&self) -> &str {
self.inner.sep()
}

Expand Down Expand Up @@ -191,17 +192,16 @@ impl FakeBasicFileSystem {
}

impl FileSystem for FakeBasicFileSystem {
fn sep(&self) -> String {
"/".to_string()
fn sep(&self) -> &str {
"/"
}

fn join(&self, components: Vec<String>) -> String {
let sep = self.sep();
components
.into_iter()
.map(|c| c.trim_end_matches(&sep).to_string())
.collect::<Vec<String>>()
.join(&sep)
.map(|c| c.trim_end_matches(sep).to_string())
.join(sep)
}

fn split(&self, file_name: &str) -> (String, String) {
Expand All @@ -217,8 +217,8 @@ impl FileSystem for FakeBasicFileSystem {
tail = path.file_name().unwrap_or(OsStr::new(""));
}
(
head.to_str().unwrap().to_string(),
tail.to_str().unwrap().to_string(),
head.to_str().expect("Path components should be valid unicode").to_string(),
tail.to_str().expect("Path components should be valid unicode").to_string(),
)
}

Expand All @@ -230,7 +230,7 @@ impl FileSystem for FakeBasicFileSystem {
fn read(&self, file_name: &str) -> PyResult<String> {
match self.contents.get(file_name) {
Some(file_name) => Ok(file_name.clone()),
None => Err(PyFileNotFoundError::new_err("")),
None => Err(PyFileNotFoundError::new_err(format!("No such file: {file_name}"))),
}
}
}
Expand All @@ -246,7 +246,7 @@ impl PyFakeBasicFileSystem {
}

#[getter]
fn sep(&self) -> String {
fn sep(&self) -> &str {
self.inner.sep()
}

Expand Down Expand Up @@ -288,7 +288,8 @@ pub fn parse_indented_file_system_string(file_system_string: &str) -> HashMap<St
let buffer = file_system_string.replace("\r\n", "\n");
let lines: Vec<&str> = buffer.split('\n').collect();

for line_raw in lines.clone() {
let first_line_starts_with_slash = lines[0].trim().starts_with('/');
for line_raw in lines {
let line = line_raw.trim_end(); // Remove trailing whitespace
if line.is_empty() {
continue; // Skip empty lines
Expand Down Expand Up @@ -331,7 +332,7 @@ pub fn parse_indented_file_system_string(file_system_string: &str) -> HashMap<St
let mut joined = path_stack.join("/");
// If the original root started with a slash, ensure the final path does too.
// But be careful not to double-slash if a component is e.g. "/root"
if lines[0].trim().starts_with('/') && !joined.starts_with('/') {
if first_line_starts_with_slash && !joined.starts_with('/') {
joined = format!("/{joined}");
}
joined
Expand All @@ -353,7 +354,7 @@ pub fn parse_indented_file_system_string(file_system_string: &str) -> HashMap<St
// Edge case: If the very first line was a file and it ended up on the stack, it needs to be processed.
// This handles single-file inputs like "myfile.txt"
if !path_stack.is_empty()
&& !path_stack.last().unwrap().ends_with('/')
&& !path_stack.last().expect("path_stack should be non-empty").ends_with('/')
&& !file_paths_map.contains_key(&path_stack.join("/"))
{
file_paths_map.insert(path_stack.join("/"), String::new());
Expand Down
24 changes: 13 additions & 11 deletions rust/src/graph/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use bimap::BiMap;
use derive_new::new;
use getset::{CopyGetters, Getters};
use lazy_static::lazy_static;
use rustc_hash::{FxHashMap, FxHashSet};
use slotmap::{SecondaryMap, SlotMap, new_key_type};
use std::sync::RwLock;
use std::sync::{LazyLock, RwLock};
use string_interner::backend::StringBackend;
use string_interner::{DefaultSymbol, StringInterner};

Expand All @@ -16,15 +15,18 @@ pub mod import_chain_queries;

pub(crate) mod pathfinding;

lazy_static! {
static ref MODULE_NAMES: RwLock<StringInterner<StringBackend>> =
RwLock::new(StringInterner::default());
static ref IMPORT_LINE_CONTENTS: RwLock<StringInterner<StringBackend>> =
RwLock::new(StringInterner::default());
static ref EMPTY_MODULE_TOKENS: FxHashSet<ModuleToken> = FxHashSet::default();
static ref EMPTY_IMPORT_DETAILS: FxHashSet<ImportDetails> = FxHashSet::default();
static ref EMPTY_IMPORTS: FxHashSet<(ModuleToken, ModuleToken)> = FxHashSet::default();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EMPTY_IMPORTS was unused, so I've removed it.

}
static MODULE_NAMES: LazyLock<RwLock<StringInterner<StringBackend>>> = LazyLock::new(|| {
RwLock::new(StringInterner::default())
});
static IMPORT_LINE_CONTENTS: LazyLock<RwLock<StringInterner<StringBackend>>> = LazyLock::new(|| {
RwLock::new(StringInterner::default())
});
static EMPTY_MODULE_TOKENS: LazyLock<FxHashSet<ModuleToken>> = LazyLock::new(|| {
FxHashSet::default()
});
static EMPTY_IMPORT_DETAILS: LazyLock<FxHashSet<ImportDetails>> = LazyLock::new(|| {
FxHashSet::default()
});

new_key_type! { pub struct ModuleToken; }

Expand Down
9 changes: 4 additions & 5 deletions rust/src/module_expressions.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use crate::errors::{GrimpError, GrimpResult};
use const_format::formatcp;
use itertools::Itertools;
use lazy_static::lazy_static;
use regex::Regex;
use std::fmt::Display;
use std::str::FromStr;
use std::sync::LazyLock;

lazy_static! {
static ref MODULE_EXPRESSION_PATTERN: Regex =
Regex::new(r"^(\w+|\*{1,2})(\.(\w+|\*{1,2}))*$").unwrap();
}
static MODULE_EXPRESSION_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^(\w+|\*{1,2})(\.(\w+|\*{1,2}))*$").unwrap()
});

/// A module expression is used to refer to sets of modules.
///
Expand Down
Loading