Skip to main content

standarbuild_detect/
lib.rs

1//! Detect the kind of a **project** (Rust, Node, Bun, Deno, Python, Lua, C,
2//! C++, or any custom kind you register) AND the **workspace** organizer
3//! (Cargo, Npm, Pnpm, Yarn, Bun, Deno, Go, Lerna, Nx, Turborepo, Mira) from
4//! on-disk signals, and scan a polyglot monorepo recursively.
5//!
6//! # Two ways in
7//!
8//! For the common case where the built-ins are enough:
9//!
10//! ```no_run
11//! use std::path::Path;
12//! use standarbuild_detect::{detect_in_dir, discover, DiscoverOptions};
13//!
14//! for hit in detect_in_dir(Path::new("./my-app")) {
15//!     println!("{:?}", hit);
16//! }
17//!
18//! let result = discover(Path::new("./my-monorepo"), &DiscoverOptions::default());
19//! for p in &result.projects {
20//!     println!("{} -> {} at {} (member_of {:?})", p.label, p.kind, p.rel_path, p.member_of);
21//! }
22//! for w in &result.workspaces {
23//!     println!("{} workspace at {:?} with {} members", w.kind, w.root, w.members.len());
24//! }
25//! ```
26//!
27//! To extend the detection space (custom kinds, custom precedence, or
28//! disabling a built-in), build your own registry:
29//!
30//! ```no_run
31//! use std::path::Path;
32//! use standarbuild_detect::{
33//!     builtin, Detector, DetectorHit, DetectorRegistry, KindId,
34//! };
35//!
36//! struct WgslDetector;
37//! impl Detector for WgslDetector {
38//!     fn name(&self) -> &str { "wgsl" }
39//!     fn priority(&self) -> i32 { 60 }
40//!     fn detect(&self, dir: &Path) -> Option<DetectorHit> {
41//!         dir.join("shaders").is_dir().then(|| DetectorHit::Project {
42//!             kind: KindId::custom("wgsl"),
43//!             signals: vec!["shaders/".into()],
44//!         })
45//!     }
46//! }
47//!
48//! let mut registry = DetectorRegistry::with_builtins();
49//! registry.remove("c"); // we don't care about plain C
50//! registry.add(WgslDetector);
51//! ```
52//!
53//! # Features
54//!
55//! - `serde` *(default)* — derives `Serialize`/`Deserialize` on the public
56//!   types and exposes POSIX-path serializers in [`path_norm`]. Disable with
57//!   `default-features = false` for the leanest dep tree.
58//!
59//! # Breaking changes vs 0.2
60//!
61//! - `Detector::kind() -> KindId` was replaced by `Detector::name() -> &str`
62//!   (string identifier for registry removal). Each detector decides what
63//!   facet(s) to emit via its `DetectorHit` return.
64//! - `Detector::detect() -> Option<DetectMatch>` is now
65//!   `Option<DetectorHit>`. `DetectMatch` is gone; `DetectorHit` carries
66//!   project + workspace info.
67//! - `DetectorRegistry::detect(dir)` returns `Vec<DetectorHit>` (previously
68//!   `Option<DetectMatch>`) so multiple facets at the same dir (e.g. Cargo
69//!   workspace + npm workspace) can all surface.
70//! - `discover()` / `discover_with()` return `DetectionResult { projects,
71//!   workspaces }` instead of `Vec<Discovered>`. `Discovered` was renamed
72//!   to `ProjectInfo` and gained `member_of: Vec<PathBuf>`.
73//! - `DetectorRegistry::remove` now takes `&str` (detector name) instead
74//!   of `&KindId`, and returns the count of removed detectors instead of
75//!   a `bool`.
76
77pub mod builtin;
78pub mod detect;
79pub mod detector;
80pub mod discover;
81pub mod kind;
82pub mod path_norm;
83pub mod workspace;
84
85pub use detect::detect_in_dir;
86pub use detector::{Detector, DetectorHit, DetectorRegistry};
87pub use discover::{
88    discover, discover_with, DetectionResult, DiscoverOptions, LabelStrategy, ProjectInfo,
89    WorkspaceInfo,
90};
91pub use kind::KindId;
92pub use path_norm::to_posix;
93#[cfg(feature = "serde")]
94pub use path_norm::{serialize_path, serialize_path_opt};
95pub use workspace::WorkspaceKindId;