adrs 0.7.6

Command line tool for managing Architecture Decision Records
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
//! Search ADRs command.

use adrs_core::{Adr, AdrStatus, Repository};
use anyhow::{Context, Result};
use std::path::Path;

/// Search ADRs for matching content.
pub fn search(
    root: &Path,
    query: &str,
    title_only: bool,
    status_filter: Option<String>,
    case_sensitive: bool,
) -> Result<()> {
    let repo =
        Repository::open(root).context("ADR repository not found. Run 'adrs init' first.")?;

    let adrs = repo.list()?;

    // Parse status filter
    let status_filter: Option<AdrStatus> = status_filter.map(|s| s.parse().unwrap());

    // Prepare query for matching
    let query_normalized = if case_sensitive {
        query.to_string()
    } else {
        query.to_lowercase()
    };

    let mut found_any = false;

    for adr in adrs {
        // Apply status filter
        if let Some(ref filter_status) = status_filter
            && !status_matches(&adr.status, filter_status)
        {
            continue;
        }

        // Search for matches
        let matches = find_matches(&adr, &query_normalized, title_only, case_sensitive);

        if !matches.is_empty() {
            found_any = true;
            print_result(&adr, &matches, query);
        }
    }

    if !found_any {
        println!("No matches found for '{}'", query);
    }

    Ok(())
}

/// Find matches in an ADR.
fn find_matches(
    adr: &Adr,
    query: &str,
    title_only: bool,
    case_sensitive: bool,
) -> Vec<SearchMatch> {
    let mut matches = Vec::new();

    // Check title
    if contains_match(&adr.title, query, case_sensitive) {
        matches.push(SearchMatch {
            section: "Title".to_string(),
            snippet: adr.title.clone(),
        });
    }

    if title_only {
        return matches;
    }

    // Check context
    if contains_match(&adr.context, query, case_sensitive) {
        matches.push(SearchMatch {
            section: "Context".to_string(),
            snippet: extract_snippet(&adr.context, query, case_sensitive),
        });
    }

    // Check decision
    if contains_match(&adr.decision, query, case_sensitive) {
        matches.push(SearchMatch {
            section: "Decision".to_string(),
            snippet: extract_snippet(&adr.decision, query, case_sensitive),
        });
    }

    // Check consequences
    if contains_match(&adr.consequences, query, case_sensitive) {
        matches.push(SearchMatch {
            section: "Consequences".to_string(),
            snippet: extract_snippet(&adr.consequences, query, case_sensitive),
        });
    }

    matches
}

/// Check if text contains the query.
fn contains_match(text: &str, query: &str, case_sensitive: bool) -> bool {
    if case_sensitive {
        text.contains(query)
    } else {
        text.to_lowercase().contains(query)
    }
}

/// Extract a snippet around the match.
fn extract_snippet(text: &str, query: &str, case_sensitive: bool) -> String {
    let text_search = if case_sensitive {
        text.to_string()
    } else {
        text.to_lowercase()
    };

    if let Some(pos) = text_search.find(query) {
        // Get some context around the match
        let start = pos.saturating_sub(40);
        let end = (pos + query.len() + 40).min(text.len());

        // Find word boundaries
        let start = text[..start]
            .rfind(char::is_whitespace)
            .map(|p| p + 1)
            .unwrap_or(start);
        let end = text[end..]
            .find(char::is_whitespace)
            .map(|p| end + p)
            .unwrap_or(end);

        let mut snippet = text[start..end].to_string();

        // Add ellipsis if truncated
        if start > 0 {
            snippet = format!("...{}", snippet);
        }
        if end < text.len() {
            snippet = format!("{}...", snippet);
        }

        // Replace newlines with spaces for cleaner output
        snippet.replace('\n', " ")
    } else {
        // Fallback: first 80 chars
        let preview: String = text.chars().take(80).collect();
        if text.len() > 80 {
            format!("{}...", preview)
        } else {
            preview
        }
    }
}

/// Print a search result.
fn print_result(adr: &Adr, matches: &[SearchMatch], _query: &str) {
    println!("{}. {}", adr.number, adr.title);

    for m in matches {
        if m.section != "Title" {
            println!("   {}: {}", m.section, m.snippet);
        }
    }

    println!();
}

/// A match found in an ADR.
struct SearchMatch {
    section: String,
    snippet: String,
}

/// Check if two statuses match (case-insensitive).
fn status_matches(adr_status: &AdrStatus, filter_status: &AdrStatus) -> bool {
    match (adr_status, filter_status) {
        (AdrStatus::Proposed, AdrStatus::Proposed) => true,
        (AdrStatus::Accepted, AdrStatus::Accepted) => true,
        (AdrStatus::Deprecated, AdrStatus::Deprecated) => true,
        (AdrStatus::Superseded, AdrStatus::Superseded) => true,
        (AdrStatus::Custom(a), AdrStatus::Custom(b)) => a.to_lowercase() == b.to_lowercase(),
        (AdrStatus::Custom(s), standard) | (standard, AdrStatus::Custom(s)) => {
            s.to_lowercase() == standard.to_string().to_lowercase()
        }
        _ => false,
    }
}

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

    // ========== contains_match tests ==========

    #[test]
    fn test_contains_match_case_insensitive_found() {
        assert!(contains_match("Hello World", "world", false));
    }

    #[test]
    fn test_contains_match_case_insensitive_not_found() {
        assert!(!contains_match("Hello World", "rust", false));
    }

    #[test]
    fn test_contains_match_case_sensitive_found() {
        assert!(contains_match("Hello World", "World", true));
    }

    #[test]
    fn test_contains_match_case_sensitive_not_found() {
        assert!(!contains_match("Hello World", "world", true));
    }

    #[test]
    fn test_contains_match_empty_text() {
        assert!(!contains_match("", "query", false));
    }

    // ========== extract_snippet tests ==========

    #[test]
    fn test_extract_snippet_match_in_middle() {
        let text = "This is some context about the database decision and consequences.";
        let snippet = extract_snippet(text, "database", false);
        assert!(
            snippet.contains("database"),
            "Snippet should contain the match"
        );
    }

    #[test]
    fn test_extract_snippet_match_at_start() {
        let text = "database is the topic of this context section.";
        let snippet = extract_snippet(text, "database", false);
        assert!(snippet.contains("database"));
    }

    #[test]
    fn test_extract_snippet_match_at_end() {
        let text = "This context is about the database";
        let snippet = extract_snippet(text, "database", false);
        assert!(snippet.contains("database"));
    }

    #[test]
    fn test_extract_snippet_no_match_returns_first_chars() {
        // When no match found, returns up to 80 chars of text
        let text = "This context has no matching term at all.";
        let snippet = extract_snippet(text, "nonexistent", false);
        // Fallback returns the original text (it is under 80 chars)
        assert_eq!(snippet, text);
    }

    #[test]
    fn test_extract_snippet_long_text_no_match_truncates() {
        let text = "a".repeat(100);
        let snippet = extract_snippet(&text, "nonexistent", false);
        // Fallback truncates to 80 chars + "..."
        assert!(snippet.ends_with("..."));
        assert!(snippet.len() <= 83); // 80 chars + "..."
    }

    #[test]
    fn test_extract_snippet_case_insensitive() {
        let text = "The DATABASE decision was made.";
        let snippet = extract_snippet(text, "database", false);
        assert!(snippet.contains("DATABASE"));
    }

    #[test]
    fn test_extract_snippet_adds_ellipsis_for_truncated_start() {
        // Long text with match far from start should add leading ellipsis
        let prefix = "x ".repeat(25); // 50 chars
        let suffix = " y".repeat(25); // 50 chars
        let text = format!("{}match{}", prefix, suffix);
        let snippet = extract_snippet(&text, "match", false);
        assert!(snippet.contains("match"));
    }

    // ========== status_matches tests ==========

    #[test]
    fn test_status_matches_proposed_proposed() {
        assert!(status_matches(&AdrStatus::Proposed, &AdrStatus::Proposed));
    }

    #[test]
    fn test_status_matches_accepted_accepted() {
        assert!(status_matches(&AdrStatus::Accepted, &AdrStatus::Accepted));
    }

    #[test]
    fn test_status_matches_deprecated_deprecated() {
        assert!(status_matches(
            &AdrStatus::Deprecated,
            &AdrStatus::Deprecated
        ));
    }

    #[test]
    fn test_status_matches_superseded_superseded() {
        assert!(status_matches(
            &AdrStatus::Superseded,
            &AdrStatus::Superseded
        ));
    }

    #[test]
    fn test_status_matches_different_standard_statuses() {
        assert!(!status_matches(&AdrStatus::Proposed, &AdrStatus::Accepted));
        assert!(!status_matches(
            &AdrStatus::Accepted,
            &AdrStatus::Deprecated
        ));
    }

    #[test]
    fn test_status_matches_custom_custom_case_insensitive() {
        assert!(status_matches(
            &AdrStatus::Custom("Draft".into()),
            &AdrStatus::Custom("draft".into())
        ));
        assert!(status_matches(
            &AdrStatus::Custom("DRAFT".into()),
            &AdrStatus::Custom("Draft".into())
        ));
    }

    #[test]
    fn test_status_matches_custom_vs_standard_case_insensitive() {
        // "accepted" as Custom should match Accepted
        assert!(status_matches(
            &AdrStatus::Custom("accepted".into()),
            &AdrStatus::Accepted
        ));
        assert!(status_matches(
            &AdrStatus::Accepted,
            &AdrStatus::Custom("Accepted".into())
        ));
    }

    #[test]
    fn test_status_matches_custom_vs_standard_no_match() {
        assert!(!status_matches(
            &AdrStatus::Custom("draft".into()),
            &AdrStatus::Accepted
        ));
    }

    // ========== find_matches tests ==========

    #[test]
    fn test_find_matches_title_match() {
        let mut adr = Adr::new(1, "Use PostgreSQL for persistence");
        adr.context = "We need a database.".to_string();
        adr.decision = "We chose PostgreSQL.".to_string();
        adr.consequences = "Higher complexity.".to_string();

        let matches = find_matches(&adr, "postgresql", false, false);
        assert!(!matches.is_empty());
        assert!(matches.iter().any(|m| m.section == "Title"));
    }

    #[test]
    fn test_find_matches_no_match_returns_empty() {
        let mut adr = Adr::new(1, "Use PostgreSQL");
        adr.context = "We need a database.".to_string();
        adr.decision = "We chose PostgreSQL.".to_string();
        adr.consequences = "Higher complexity.".to_string();

        let matches = find_matches(&adr, "nonexistent_term_xyz", false, false);
        assert!(matches.is_empty());
    }

    #[test]
    fn test_find_matches_title_only_mode() {
        let mut adr = Adr::new(1, "Use PostgreSQL");
        adr.context = "This context also mentions PostgreSQL.".to_string();

        // In title_only mode, only the title is checked
        let matches = find_matches(&adr, "postgresql", true, false);
        assert!(matches.iter().any(|m| m.section == "Title"));
        // Should NOT search context in title_only mode
        assert!(!matches.iter().any(|m| m.section == "Context"));
    }

    #[test]
    fn test_find_matches_context_match() {
        let mut adr = Adr::new(1, "Architecture Decision");
        adr.context = "We need to store data in a relational database.".to_string();
        adr.decision = "We will use PostgreSQL.".to_string();
        adr.consequences = "Team needs training.".to_string();

        let matches = find_matches(&adr, "relational", false, false);
        assert!(matches.iter().any(|m| m.section == "Context"));
    }

    #[test]
    fn test_find_matches_multiple_sections() {
        let mut adr = Adr::new(1, "test adr");
        adr.context = "The keyword is here.".to_string();
        adr.decision = "The keyword applies.".to_string();
        adr.consequences = "keyword consequences.".to_string();

        let matches = find_matches(&adr, "keyword", false, false);
        // Should find in context, decision, consequences
        assert!(matches.len() >= 3);
    }
}