Skip to main content

source_map_tauri/tauri_config/
capabilities.rs

1use anyhow::Result;
2use serde_json::Value;
3
4use crate::{
5    config::{normalize_path, ResolvedConfig},
6    discovery::RepoDiscovery,
7    ids::document_id,
8    model::ArtifactDoc,
9    security::apply_artifact_security,
10};
11
12pub fn extract_capabilities(
13    config: &ResolvedConfig,
14    discovery: &RepoDiscovery,
15) -> Result<Vec<ArtifactDoc>> {
16    let mut docs = Vec::new();
17    for path in &discovery.capability_files {
18        let text = std::fs::read_to_string(path)?;
19        let value = if path.extension().and_then(|item| item.to_str()) == Some("json") {
20            serde_json::from_str::<Value>(&text)?
21        } else {
22            serde_json::to_value(toml::from_str::<toml::Value>(&text)?)?
23        };
24        let identifier = value
25            .get("identifier")
26            .and_then(Value::as_str)
27            .unwrap_or("capability");
28        let source_path = normalize_path(&config.root, path);
29        let mut doc = ArtifactDoc {
30            id: document_id(
31                &config.repo,
32                "tauri_capability",
33                Some(&source_path),
34                Some(1),
35                Some(identifier),
36            ),
37            repo: config.repo.clone(),
38            kind: "tauri_capability".to_owned(),
39            side: Some("config".to_owned()),
40            language: path
41                .extension()
42                .and_then(|item| item.to_str())
43                .map(str::to_owned),
44            name: Some(identifier.to_owned()),
45            display_name: Some(identifier.to_owned()),
46            source_path: Some(source_path),
47            line_start: Some(1),
48            line_end: Some(text.lines().count() as u32),
49            column_start: None,
50            column_end: None,
51            package_name: None,
52            comments: Vec::new(),
53            tags: vec!["capability".to_owned()],
54            related_symbols: Vec::new(),
55            related_tests: Vec::new(),
56            risk_level: "medium".to_owned(),
57            risk_reasons: Vec::new(),
58            contains_phi: false,
59            has_related_tests: false,
60            updated_at: chrono::Utc::now().to_rfc3339(),
61            data: Default::default(),
62        };
63        for key in ["windows", "permissions"] {
64            if let Some(value) = value.get(key) {
65                doc.data.insert(key.to_owned(), value.clone());
66            }
67        }
68        apply_artifact_security(&mut doc);
69        docs.push(doc);
70    }
71    Ok(docs)
72}