pecto-core 0.1.0

Core behavior spec model and analysis framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// A complete behavior spec for a project.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectSpec {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub analyzed: Option<String>,
    pub files_analyzed: usize,
    pub capabilities: Vec<Capability>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub dependencies: Vec<DependencyEdge>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub domains: Vec<Domain>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub flows: Vec<RequestFlow>,
}

/// A capability groups related behaviors (e.g., "user-authentication", "order-management").
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Capability {
    pub name: String,
    pub source: String,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub endpoints: Vec<Endpoint>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub operations: Vec<Operation>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub entities: Vec<Entity>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub scheduled_tasks: Vec<ScheduledTask>,
}

/// An HTTP endpoint extracted from a controller.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Endpoint {
    pub method: HttpMethod,
    pub path: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input: Option<EndpointInput>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub validation: Vec<ValidationRule>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub behaviors: Vec<Behavior>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub security: Option<SecurityConfig>,
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum HttpMethod {
    Get,
    Post,
    Put,
    Delete,
    Patch,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EndpointInput {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<TypeRef>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub path_params: Vec<Param>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub query_params: Vec<Param>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Param {
    pub name: String,
    #[serde(rename = "type")]
    pub param_type: String,
    #[serde(skip_serializing_if = "std::ops::Not::not", default)]
    pub required: bool,
}

/// A reference to a type with its fields.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypeRef {
    pub name: String,
    #[serde(skip_serializing_if = "BTreeMap::is_empty", default)]
    pub fields: BTreeMap<String, String>,
}

/// A validation rule on an input field.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationRule {
    pub field: String,
    pub constraints: Vec<String>,
}

/// A behavior describes what happens under specific conditions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Behavior {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub condition: Option<String>,
    pub returns: ResponseSpec,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub side_effects: Vec<SideEffect>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseSpec {
    pub status: u16,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<TypeRef>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum SideEffect {
    #[serde(rename = "db_insert")]
    DbInsert { table: String },
    #[serde(rename = "db_update")]
    DbUpdate { description: String },
    #[serde(rename = "event")]
    Event { name: String },
    #[serde(rename = "call")]
    ServiceCall { target: String },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityConfig {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub authentication: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub roles: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rate_limit: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cors: Option<String>,
}

/// A service-layer operation (non-HTTP).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Operation {
    pub name: String,
    pub source_method: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input: Option<TypeRef>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub behaviors: Vec<Behavior>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub transaction: Option<String>,
}

/// A database entity extracted from JPA/Hibernate/EF annotations.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Entity {
    pub name: String,
    pub table: String,
    pub fields: Vec<EntityField>,
    /// Parent class names (for same-file field inheritance resolution).
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub bases: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityField {
    pub name: String,
    #[serde(rename = "type")]
    pub field_type: String,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub constraints: Vec<String>,
}

/// A scheduled/cron task.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScheduledTask {
    pub name: String,
    pub schedule: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

/// A dependency edge between two capabilities.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DependencyEdge {
    pub from: String,
    pub to: String,
    pub kind: DependencyKind,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub references: Vec<String>,
}

/// The kind of dependency between capabilities.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DependencyKind {
    Calls,
    Queries,
    Listens,
    Validates,
}

/// A domain groups related capabilities by business concern.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Domain {
    pub name: String,
    pub capabilities: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub external_dependencies: Vec<String>,
}

/// A traced request flow for an endpoint — the complete call chain.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestFlow {
    pub trigger: String,
    pub entry_point: String,
    pub steps: Vec<FlowStep>,
}

/// A single step in a request flow.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FlowStep {
    pub actor: String,
    pub method: String,
    pub kind: FlowStepKind,
    pub description: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub condition: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub children: Vec<FlowStep>,
}

/// The type of step in a request flow.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FlowStepKind {
    ServiceCall,
    DbRead,
    DbWrite,
    EventPublish,
    Validation,
    SecurityGuard,
    Condition,
    Return,
    ThrowException,
}

impl ProjectSpec {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            analyzed: Some(chrono_now()),
            files_analyzed: 0,
            capabilities: Vec::new(),
            dependencies: Vec::new(),
            domains: Vec::new(),
            flows: Vec::new(),
        }
    }
}

fn chrono_now() -> String {
    use std::time::SystemTime;
    let duration = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap_or_default();
    let secs = duration.as_secs();
    // Convert to ISO 8601 UTC without external dependency
    let days = secs / 86400;
    let time_secs = secs % 86400;
    let hours = time_secs / 3600;
    let minutes = (time_secs % 3600) / 60;
    let seconds = time_secs % 60;
    // Days since 1970-01-01
    let (year, month, day) = days_to_ymd(days);
    format!(
        "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
        year, month, day, hours, minutes, seconds
    )
}

fn days_to_ymd(mut days: u64) -> (u64, u64, u64) {
    let mut year = 1970;
    loop {
        let days_in_year = if is_leap(year) { 366 } else { 365 };
        if days < days_in_year {
            break;
        }
        days -= days_in_year;
        year += 1;
    }
    let month_days: &[u64] = if is_leap(year) {
        &[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    } else {
        &[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    };
    let mut month = 1;
    for &md in month_days {
        if days < md {
            break;
        }
        days -= md;
        month += 1;
    }
    (year, month, days + 1)
}

fn is_leap(year: u64) -> bool {
    (year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400)
}

impl Capability {
    pub fn new(name: impl Into<String>, source: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            source: source.into(),
            endpoints: Vec::new(),
            operations: Vec::new(),
            entities: Vec::new(),
            scheduled_tasks: Vec::new(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.endpoints.is_empty()
            && self.operations.is_empty()
            && self.entities.is_empty()
            && self.scheduled_tasks.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_project_spec_new() {
        let spec = ProjectSpec::new("my-project");
        assert_eq!(spec.name, "my-project");
        assert!(spec.analyzed.is_some());
        assert_eq!(spec.files_analyzed, 0);
        assert!(spec.capabilities.is_empty());
    }

    #[test]
    fn test_timestamp_is_dynamic() {
        let spec = ProjectSpec::new("test");
        let ts = spec.analyzed.unwrap();
        // Should be a valid ISO 8601 timestamp, not the old hardcoded one
        assert!(ts.contains('T'));
        assert!(ts.ends_with('Z'));
        assert_ne!(ts, "2026-03-25T00:00:00Z");
        // Should contain the current year (2026)
        assert!(ts.starts_with("202"));
    }

    #[test]
    fn test_capability_is_empty() {
        let cap = Capability::new("test", "test.java");
        assert!(cap.is_empty());

        let mut cap_with_endpoint = Capability::new("test", "test.java");
        cap_with_endpoint.endpoints.push(Endpoint {
            method: HttpMethod::Get,
            path: "/test".to_string(),
            input: None,
            validation: Vec::new(),
            behaviors: Vec::new(),
            security: None,
        });
        assert!(!cap_with_endpoint.is_empty());
    }

    #[test]
    fn test_serialization_round_trip_yaml() {
        let mut spec = ProjectSpec::new("round-trip-test");
        spec.files_analyzed = 5;

        let mut cap = Capability::new("user", "User.java");
        cap.endpoints.push(Endpoint {
            method: HttpMethod::Get,
            path: "/api/users".to_string(),
            input: None,
            validation: Vec::new(),
            behaviors: vec![Behavior {
                name: "success".to_string(),
                condition: None,
                returns: ResponseSpec {
                    status: 200,
                    body: Some(TypeRef {
                        name: "User".to_string(),
                        fields: BTreeMap::new(),
                    }),
                },
                side_effects: Vec::new(),
            }],
            security: None,
        });
        spec.capabilities.push(cap);

        let yaml = crate::output::to_yaml(&spec).unwrap();
        assert!(yaml.contains("round-trip-test"));
        assert!(yaml.contains("/api/users"));
        assert!(yaml.contains("GET")); // HttpMethod serialized as uppercase

        // Deserialize back
        let parsed: ProjectSpec = serde_yaml::from_str(&yaml).unwrap();
        assert_eq!(parsed.name, "round-trip-test");
        assert_eq!(parsed.capabilities.len(), 1);
        assert_eq!(parsed.capabilities[0].endpoints[0].path, "/api/users");
    }

    #[test]
    fn test_serialization_round_trip_json() {
        let spec = ProjectSpec::new("json-test");
        let json = crate::output::to_json(&spec).unwrap();
        assert!(json.contains("json-test"));

        let parsed: ProjectSpec = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.name, "json-test");
    }

    #[test]
    fn test_empty_fields_skipped_in_yaml() {
        let spec = ProjectSpec::new("skip-test");
        let yaml = crate::output::to_yaml(&spec).unwrap();
        // Empty capabilities list should still show up (it's a top-level field)
        // but empty sub-fields (endpoints, operations, etc.) should be skipped
        assert!(!yaml.contains("endpoints"));
        assert!(!yaml.contains("operations"));
        assert!(!yaml.contains("entities"));
    }
}