Skip to main content

weavatrix_md/
model.rs

1//! Compact observations. Raw DSNs and credentials never appear here.
2
3use std::collections::BTreeSet;
4use std::path::PathBuf;
5
6/// Display name plus canonical root.
7#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
8pub struct RepoId {
9    /// Markdown identity.
10    pub name: String,
11    /// Canonical repository root.
12    pub root: PathBuf,
13}
14
15/// Everything one repository proved about its external boundaries.
16#[derive(Debug, Clone, Default)]
17pub struct RepoInventory {
18    /// Repository identity.
19    pub repo: RepoId,
20    /// Service/package aliases used for host matching.
21    pub aliases: BTreeSet<String>,
22    /// HTTP/gRPC/GraphQL observations.
23    pub api: Vec<ApiObservation>,
24    /// Kafka producer/consumer observations.
25    pub kafka: Vec<KafkaObservation>,
26    /// Sanitized database identities.
27    pub databases: Vec<DatabaseObservation>,
28}
29
30/// Whether a repository exposes or calls an API.
31#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
32pub enum ApiDirection {
33    /// Server/provider.
34    Exposes,
35    /// Client/caller.
36    Calls,
37}
38
39/// API protocol family.
40#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
41pub enum ApiProtocol {
42    /// HTTP/HTTPS.
43    Http,
44    /// gRPC / Protobuf service.
45    Grpc,
46    /// GraphQL operation.
47    Graphql,
48}
49
50/// One API observation.
51#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
52pub struct ApiObservation {
53    /// Provider or consumer.
54    pub direction: ApiDirection,
55    /// Protocol.
56    pub protocol: ApiProtocol,
57    /// HTTP method when known, uppercased.
58    pub method: Option<String>,
59    /// Route, gRPC service, or GraphQL field.
60    pub resource: String,
61    /// Hostname or service alias when statically present.
62    pub host_hint: Option<String>,
63}
64
65/// Kafka producer or consumer.
66#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
67pub enum KafkaRole {
68    /// Publishes to a topic.
69    Producer,
70    /// Reads from a topic.
71    Consumer,
72}
73
74/// One Kafka observation.
75#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
76pub struct KafkaObservation {
77    /// Producer or consumer.
78    pub role: KafkaRole,
79    /// Topic name.
80    pub topic: String,
81    /// Optional cluster/broker identity, never a secret.
82    pub cluster: Option<String>,
83}
84
85/// Database engine.
86#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
87pub enum DatabaseEngine {
88    /// PostgreSQL.
89    Postgres,
90    /// MongoDB.
91    Mongo,
92    /// MySQL or MariaDB.
93    Mysql,
94    /// ClickHouse.
95    Clickhouse,
96    /// Microsoft SQL Server.
97    SqlServer,
98    /// SQLite (never a cross-repo edge by default).
99    Sqlite,
100    /// Redis.
101    Redis,
102    /// HashiCorp Vault.
103    Vault,
104}
105
106impl DatabaseEngine {
107    /// Human label used in Markdown.
108    #[must_use]
109    pub const fn label(self) -> &'static str {
110        match self {
111            Self::Postgres => "PostgreSQL",
112            Self::Mongo => "MongoDB",
113            Self::Mysql => "MySQL",
114            Self::Clickhouse => "ClickHouse",
115            Self::SqlServer => "SQL Server",
116            Self::Sqlite => "SQLite",
117            Self::Redis => "Redis",
118            Self::Vault => "Vault",
119        }
120    }
121
122    /// Markdown section this engine belongs to.
123    #[must_use]
124    pub const fn section(self) -> &'static str {
125        match self {
126            Self::Redis => "Redis",
127            Self::Vault => "Vault",
128            Self::Postgres
129            | Self::Mongo
130            | Self::Mysql
131            | Self::Clickhouse
132            | Self::SqlServer
133            | Self::Sqlite => "Database",
134        }
135    }
136}
137
138/// Sanitized database identity. Host may be a service name, never a DSN.
139#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
140pub struct DatabaseObservation {
141    /// Engine.
142    pub engine: DatabaseEngine,
143    /// Hostname or deployment service name.
144    pub host: Option<String>,
145    /// Logical database name.
146    pub database: Option<String>,
147    /// True when host is loopback and needs shared deployment evidence.
148    pub localhost: bool,
149}
150
151/// Proven database neighborhood for one resource.
152#[derive(Debug, Clone, PartialEq, Eq)]
153pub struct DatabaseRelation {
154    /// Engine label.
155    pub engine: String,
156    /// Logical database name.
157    pub database: String,
158    /// Other repositories sharing the identity.
159    pub peers: Vec<String>,
160}
161
162/// Proven Kafka neighborhood for one topic.
163#[derive(Debug, Clone, PartialEq, Eq)]
164pub struct KafkaRelation {
165    /// Topic.
166    pub topic: String,
167    /// Peers that consume what this repository produces.
168    pub produces: Vec<String>,
169    /// Peers that produce what this repository consumes.
170    pub consumes: Vec<String>,
171}
172
173/// Proven API neighborhood, collapsed to peer names.
174#[derive(Debug, Clone, Default, PartialEq, Eq)]
175pub struct ApiRelation {
176    /// Repositories this one calls.
177    pub outgoing: Vec<String>,
178    /// Repositories that call this one.
179    pub incoming: Vec<String>,
180}
181
182/// Render-ready map for one repository.
183#[derive(Debug, Clone, PartialEq, Eq)]
184pub struct RepositoryMap {
185    /// Display name.
186    pub repository: String,
187    /// Database sections.
188    pub databases: Vec<DatabaseRelation>,
189    /// Kafka sections.
190    pub kafka: Vec<KafkaRelation>,
191    /// API section.
192    pub api: ApiRelation,
193}
194
195impl RepositoryMap {
196    /// Number of proven peer lines across all sections.
197    #[must_use]
198    pub fn connection_count(&self) -> usize {
199        let databases = self
200            .databases
201            .iter()
202            .map(|item| item.peers.len())
203            .sum::<usize>();
204        let kafka = self
205            .kafka
206            .iter()
207            .map(|item| item.produces.len() + item.consumes.len())
208            .sum::<usize>();
209        databases + kafka + self.api.outgoing.len() + self.api.incoming.len()
210    }
211}