feat: add rendering-primitives crate, share non_import_rels across renderers
This commit is contained in:
8
crates/adapters/rendering-primitives/Cargo.toml
Normal file
8
crates/adapters/rendering-primitives/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "archlens-rendering-primitives"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
archlens-domain.workspace = true
|
||||
11
crates/adapters/rendering-primitives/src/lib.rs
Normal file
11
crates/adapters/rendering-primitives/src/lib.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use archlens_domain::{Relationship, RelationshipKind};
|
||||
|
||||
/// Returns an iterator over all relationships except those with kind `Import`.
|
||||
pub fn non_import_rels(rels: &[Relationship]) -> impl Iterator<Item = &Relationship> {
|
||||
rels.iter().filter(|r| r.kind() != RelationshipKind::Import)
|
||||
}
|
||||
|
||||
/// Replaces `::`, `-`, `.`, and space with `_`.
|
||||
pub fn sanitize_identifier(name: &str) -> String {
|
||||
name.replace("::", "_").replace(['-', '.', ' '], "_")
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
use archlens_domain::{
|
||||
FilePath, Relationship, RelationshipKind,
|
||||
};
|
||||
use archlens_rendering_primitives::{non_import_rels, sanitize_identifier};
|
||||
|
||||
fn rel(src: &str, tgt: &str, kind: RelationshipKind) -> Relationship {
|
||||
Relationship::new(src, tgt, kind).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_import_rels_excludes_import_relationships() {
|
||||
let rels = vec![
|
||||
rel("A", "B", RelationshipKind::Composition),
|
||||
rel("C", "D", RelationshipKind::Import),
|
||||
rel("E", "F", RelationshipKind::Inheritance),
|
||||
];
|
||||
let filtered: Vec<_> = non_import_rels(&rels).collect();
|
||||
assert_eq!(filtered.len(), 2);
|
||||
assert!(filtered.iter().all(|r| r.kind() != RelationshipKind::Import));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_import_rels_passes_all_non_import_kinds() {
|
||||
let rels = vec![
|
||||
rel("A", "B", RelationshipKind::Composition),
|
||||
rel("C", "D", RelationshipKind::Inheritance),
|
||||
];
|
||||
let filtered: Vec<_> = non_import_rels(&rels).collect();
|
||||
assert_eq!(filtered.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sanitize_identifier_replaces_double_colon_with_underscore() {
|
||||
assert_eq!(sanitize_identifier("foo::bar"), "foo_bar");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sanitize_identifier_replaces_hyphen_with_underscore() {
|
||||
assert_eq!(sanitize_identifier("my-crate"), "my_crate");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sanitize_identifier_replaces_dot_with_underscore() {
|
||||
assert_eq!(sanitize_identifier("v1.2"), "v1_2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sanitize_identifier_replaces_space_with_underscore() {
|
||||
assert_eq!(sanitize_identifier("my crate"), "my_crate");
|
||||
}
|
||||
Reference in New Issue
Block a user