cargo-brief 0.12.0

Visibility-aware Rust API extractor — pseudo-Rust output for AI agent consumption
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
/// A parsed cfg predicate extracted from an `Attribute::Other` string.
///
/// `Feature` carries a concrete feature name. `NonFeature` carries the raw
/// name/value so mixed predicates (e.g. `all(feature = "x", unix)`) round-trip.
#[derive(Debug, PartialEq)]
pub enum CfgPredicate {
    Feature(String),
    NonFeature { name: String, value: Option<String> },
    All(Vec<CfgPredicate>),
    Any(Vec<CfgPredicate>),
    Not(Box<CfgPredicate>),
}

/// Parse a raw `Attribute::Other` string into a `CfgPredicate` tree.
///
/// Returns `None` on any parse failure — callers should fall back to emitting the
/// raw string rather than crashing or silently dropping the attribute.
///
/// Expected wrapper format (from nightly HIR debug serialization):
/// `"#[attr = CfgTrace([<pred>])]"`
pub fn parse_cfg_attribute(raw: &str) -> Option<CfgPredicate> {
    // Strip outer wrapper: `#[attr = CfgTrace([` ... `])]`
    let inner = raw
        .strip_prefix("#[attr = CfgTrace([")?
        .strip_suffix("])]")?;

    let (pred, rest) = parse_pred(inner.trim())?;
    if rest.trim().is_empty() {
        Some(pred)
    } else {
        None
    }
}

/// Reconstruct a `#[cfg(...)]` attribute string from a predicate tree.
///
/// Total — every variant round-trips, so callers never need a fallback for
/// shape reasons (only for upstream parse failure).
pub fn reconstruct_cfg_attr(pred: &CfgPredicate) -> String {
    let mut inner = String::new();
    write_predicate(pred, &mut inner);
    format!("#[cfg({inner})]")
}

fn write_predicate(pred: &CfgPredicate, out: &mut String) {
    match pred {
        CfgPredicate::Feature(f) => out.push_str(&format!("feature = \"{f}\"")),
        CfgPredicate::NonFeature { name, value: None } => out.push_str(name),
        CfgPredicate::NonFeature {
            name,
            value: Some(v),
        } => out.push_str(&format!("{name} = \"{v}\"")),
        CfgPredicate::Not(inner) => {
            out.push_str("not(");
            write_predicate(inner, out);
            out.push(')');
        }
        CfgPredicate::All(ps) => write_list("all", ps, out),
        CfgPredicate::Any(ps) => write_list("any", ps, out),
    }
}

fn write_list(combinator: &str, preds: &[CfgPredicate], out: &mut String) {
    out.push_str(combinator);
    out.push('(');
    for (i, p) in preds.iter().enumerate() {
        if i > 0 {
            out.push_str(", ");
        }
        write_predicate(p, out);
    }
    out.push(')');
}

// ── Parser internals ──────────────────────────────────────────────────────────

/// Parse one predicate from `s`, returning `(pred, remaining_input)`.
fn parse_pred(s: &str) -> Option<(CfgPredicate, &str)> {
    if let Some(rest) = s.strip_prefix("All([") {
        let (preds, rest) = parse_pred_list(rest)?;
        let rest = rest.trim_start();
        // consume ", span)"
        let rest = consume_span_suffix(rest)?;
        return Some((CfgPredicate::All(preds), rest));
    }
    if let Some(rest) = s.strip_prefix("Any([") {
        let (preds, rest) = parse_pred_list(rest)?;
        let rest = rest.trim_start();
        let rest = consume_span_suffix(rest)?;
        return Some((CfgPredicate::Any(preds), rest));
    }
    if let Some(rest) = s.strip_prefix("Not(") {
        let (inner, rest) = parse_pred(rest.trim_start())?;
        let rest = rest.trim_start();
        let rest = consume_span_suffix(rest)?;
        return Some((CfgPredicate::Not(Box::new(inner)), rest));
    }
    if s.starts_with("NameValue") {
        return parse_namevalue(s);
    }
    None
}

/// Consume `, <span_text>)` after a predicate list or Not-inner.
///
/// The span text may itself contain balanced parentheses (e.g., `(#0)`), so we use
/// a depth counter: the first `)` encountered at depth 0 is the enclosing predicate's
/// closing paren.
fn consume_span_suffix(s: &str) -> Option<&str> {
    let s = s.strip_prefix(',')?.trim_start();
    let mut depth: i32 = 0;
    for (i, ch) in s.char_indices() {
        match ch {
            '(' => depth += 1,
            ')' => {
                if depth == 0 {
                    return Some(&s[i + 1..]);
                }
                depth -= 1;
            }
            _ => {}
        }
    }
    None
}

/// Parse a comma-separated list of predicates until the closing `]`.
fn parse_pred_list(mut s: &str) -> Option<(Vec<CfgPredicate>, &str)> {
    let mut preds = Vec::new();

    loop {
        s = s.trim_start();
        if let Some(rest) = s.strip_prefix(']') {
            return Some((preds, rest));
        }
        if !preds.is_empty() {
            // consume comma separator
            s = s.strip_prefix(',')?.trim_start();
        }
        let (pred, rest) = parse_pred(s)?;
        preds.push(pred);
        s = rest.trim_start();
    }
}

/// Parse `NameValue { name: "<name>", value: <val>, span: ... }` and return
/// `(Feature(x) | NonFeature { .. }, remaining)`.
fn parse_namevalue(s: &str) -> Option<(CfgPredicate, &str)> {
    let s = s.strip_prefix("NameValue {")?;
    let s = s.trim_start();

    // name: "<ident>"
    let s = s.strip_prefix("name: \"")?;
    let (name, s) = split_at_unescaped_quote(s)?;
    let s = s.strip_prefix('"')?;

    // skip to value field: ", value: "
    let s = skip_to_field(s, "value:")?;
    let s = s.trim_start();

    let (value, s) = parse_option_value(s)?;

    // skip the rest of the struct up to the closing }
    let close = s.find('}')?;
    let rest = &s[close + 1..];

    let pred = if name == "feature" {
        match value {
            Some(v) => CfgPredicate::Feature(v),
            None => CfgPredicate::NonFeature {
                name: "feature".to_string(),
                value: None,
            },
        }
    } else {
        CfgPredicate::NonFeature {
            name: name.to_string(),
            value,
        }
    };

    Some((pred, rest))
}

/// Parse `Some("<value>")` or `None`.
fn parse_option_value(s: &str) -> Option<(Option<String>, &str)> {
    if let Some(s) = s.strip_prefix("None") {
        return Some((None, s));
    }
    let s = s.strip_prefix("Some(\"")?;
    let (val, s) = split_at_unescaped_quote(s)?;
    let s = s.strip_prefix('"')?;
    let s = s.strip_prefix(')')?;
    Some((Some(val.to_string()), s))
}

/// Advance past the next occurrence of `field_name` (including trailing whitespace).
fn skip_to_field<'a>(s: &'a str, field_name: &str) -> Option<&'a str> {
    let pos = s.find(field_name)?;
    Some(&s[pos + field_name.len()..])
}

/// Split at the first unescaped `"`, returning `(before, after_quote_char)`.
fn split_at_unescaped_quote(s: &str) -> Option<(&str, &str)> {
    let pos = s.find('"')?;
    Some((&s[..pos], &s[pos..]))
}

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

    // Helpers to build the HIR debug strings used in tests
    fn nv(name: &str, value: Option<&str>) -> String {
        let val = match value {
            Some(v) => format!("Some(\"{v}\")"),
            None => "None".to_string(),
        };
        format!("NameValue {{ name: \"{name}\", value: {val}, span: <dummy> }}")
    }

    fn wrap(inner: &str) -> String {
        format!("#[attr = CfgTrace([{inner}])]")
    }

    #[test]
    fn simple_feature() {
        let raw = wrap(&nv("feature", Some("net")));
        let pred = parse_cfg_attribute(&raw).unwrap();
        assert_eq!(pred, CfgPredicate::Feature("net".to_string()));
        assert_eq!(reconstruct_cfg_attr(&pred), "#[cfg(feature = \"net\")]");
    }

    #[test]
    fn non_feature_namevalue() {
        let raw = wrap(&nv("unix", None));
        let pred = parse_cfg_attribute(&raw).unwrap();
        assert_eq!(
            pred,
            CfgPredicate::NonFeature {
                name: "unix".to_string(),
                value: None
            }
        );
        assert_eq!(reconstruct_cfg_attr(&pred), "#[cfg(unix)]");
    }

    #[test]
    fn non_feature_namevalue_with_value() {
        let raw = wrap(&nv("target_os", Some("linux")));
        let pred = parse_cfg_attribute(&raw).unwrap();
        assert_eq!(
            pred,
            CfgPredicate::NonFeature {
                name: "target_os".to_string(),
                value: Some("linux".to_string())
            }
        );
        assert_eq!(reconstruct_cfg_attr(&pred), "#[cfg(target_os = \"linux\")]");
    }

    #[test]
    fn all_two_features() {
        let inner = format!(
            "All([{}, {}], span)",
            nv("feature", Some("net")),
            nv("feature", Some("tls"))
        );
        let pred = parse_cfg_attribute(&wrap(&inner)).unwrap();
        assert_eq!(
            reconstruct_cfg_attr(&pred),
            "#[cfg(all(feature = \"net\", feature = \"tls\"))]"
        );
    }

    #[test]
    fn all_three_features() {
        let inner = format!(
            "All([{}, {}, {}], span)",
            nv("feature", Some("a")),
            nv("feature", Some("b")),
            nv("feature", Some("c"))
        );
        let pred = parse_cfg_attribute(&wrap(&inner)).unwrap();
        assert_eq!(
            reconstruct_cfg_attr(&pred),
            "#[cfg(all(feature = \"a\", feature = \"b\", feature = \"c\"))]"
        );
    }

    #[test]
    fn any_two_features() {
        let inner = format!(
            "Any([{}, {}], span)",
            nv("feature", Some("a")),
            nv("feature", Some("b"))
        );
        let pred = parse_cfg_attribute(&wrap(&inner)).unwrap();
        assert_eq!(
            reconstruct_cfg_attr(&pred),
            "#[cfg(any(feature = \"a\", feature = \"b\"))]"
        );
    }

    #[test]
    fn any_three_features() {
        let inner = format!(
            "Any([{}, {}, {}], span)",
            nv("feature", Some("a")),
            nv("feature", Some("b")),
            nv("feature", Some("c"))
        );
        let pred = parse_cfg_attribute(&wrap(&inner)).unwrap();
        assert_eq!(
            reconstruct_cfg_attr(&pred),
            "#[cfg(any(feature = \"a\", feature = \"b\", feature = \"c\"))]"
        );
    }

    #[test]
    fn not_feature() {
        let inner = format!("Not({}, span)", nv("feature", Some("beta")));
        let pred = parse_cfg_attribute(&wrap(&inner)).unwrap();
        assert_eq!(
            reconstruct_cfg_attr(&pred),
            "#[cfg(not(feature = \"beta\"))]"
        );
    }

    #[test]
    fn mixed_all_reconstructs() {
        // all(feature = "net", unix) — NonFeature inside All now reconstructs cleanly
        let inner = format!(
            "All([{}, {}], span)",
            nv("feature", Some("net")),
            nv("unix", None)
        );
        let pred = parse_cfg_attribute(&wrap(&inner)).unwrap();
        assert_eq!(
            reconstruct_cfg_attr(&pred),
            "#[cfg(all(feature = \"net\", unix))]"
        );
    }

    #[test]
    fn malformed_no_wrapper_returns_none() {
        assert!(parse_cfg_attribute("not a cfg attr").is_none());
    }

    #[test]
    fn truncated_returns_none() {
        // Missing closing "])]"
        assert!(parse_cfg_attribute("#[attr = CfgTrace([NameValue {").is_none());
    }

    // Tests with the real nightly span format (contains "(#0)" inside the span field).
    fn real_nv(name: &str, value: Option<&str>, file: &str, lo: &str, hi: &str) -> String {
        let val = match value {
            Some(v) => format!("Some(\"{v}\")"),
            None => "None".to_string(),
        };
        format!("NameValue {{ name: \"{name}\", value: {val}, span: {file}:{lo}: {hi} (#0) }}")
    }

    #[test]
    fn real_span_simple_feature() {
        let inner = real_nv("feature", Some("net"), "src/lib.rs", "1:1", "1:10");
        let raw = format!("#[attr = CfgTrace([{inner}])]");
        let pred = parse_cfg_attribute(&raw).unwrap();
        assert_eq!(pred, CfgPredicate::Feature("net".to_string()));
        assert_eq!(reconstruct_cfg_attr(&pred), "#[cfg(feature = \"net\")]");
    }

    #[test]
    fn real_span_any_two_features() {
        let a = real_nv("feature", Some("extra"), "src/lib.rs", "1:15", "1:32");
        let b = real_nv(
            "feature",
            Some("experimental"),
            "src/lib.rs",
            "1:34",
            "1:58",
        );
        let inner = format!("Any([{a}, {b}], src/lib.rs:1:14: 1:59 (#0))");
        let raw = format!("#[attr = CfgTrace([{inner}])]");
        let pred = parse_cfg_attribute(&raw).unwrap();
        assert_eq!(
            reconstruct_cfg_attr(&pred),
            "#[cfg(any(feature = \"extra\", feature = \"experimental\"))]"
        );
    }

    #[test]
    fn real_span_not_feature() {
        let nv = real_nv(
            "feature",
            Some("experimental"),
            "src/lib.rs",
            "1:15",
            "1:39",
        );
        let inner = format!("Not({nv}, src/lib.rs:1:14: 1:40 (#0))");
        let raw = format!("#[attr = CfgTrace([{inner}])]");
        let pred = parse_cfg_attribute(&raw).unwrap();
        assert_eq!(
            reconstruct_cfg_attr(&pred),
            "#[cfg(not(feature = \"experimental\"))]"
        );
    }
}