|
| 1 | +//! File-association catalog and conflict-checked plans; native hosts own registration I/O. |
| 2 | +
|
| 3 | +use crate::{RuleError, RuleErrorCode}; |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | +use serde_json::{json, Value}; |
| 6 | +use std::collections::{BTreeMap, BTreeSet}; |
| 7 | + |
| 8 | +/// Projects the host's built-in syntax metadata into registrable text suffixes. |
| 9 | +pub const CATALOG: &str = "application.fileAssociations.catalog"; |
| 10 | +/// Validates an explicit selection against fresh native observations before publication. |
| 11 | +pub const PLAN: &str = "application.fileAssociations.plan"; |
| 12 | +const MAX_FORMATS: usize = 512; |
| 13 | + |
| 14 | +fn invalid(reason: &str) -> RuleError { |
| 15 | + RuleError::new(RuleErrorCode::InvalidRequest, reason) |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Deserialize)] |
| 19 | +#[serde(rename_all = "camelCase", deny_unknown_fields)] |
| 20 | +struct Language { |
| 21 | + id: String, |
| 22 | + highlighted: bool, |
| 23 | + extensions: Vec<String>, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Deserialize)] |
| 27 | +#[serde(deny_unknown_fields)] |
| 28 | +struct CatalogRequest { |
| 29 | + languages: Vec<Language>, |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Clone, Debug, Deserialize, Serialize)] |
| 33 | +#[serde(deny_unknown_fields)] |
| 34 | +struct Format { |
| 35 | + extension: String, |
| 36 | + language: String, |
| 37 | +} |
| 38 | + |
| 39 | +// Viva package declarations accept one suffix of at most 16 ASCII characters. |
| 40 | +fn safe_extension(extension: &str) -> bool { |
| 41 | + extension.len() > 1 |
| 42 | + && extension.len() <= 17 |
| 43 | + && extension.starts_with('.') |
| 44 | + && extension.as_bytes()[1].is_ascii_alphanumeric() |
| 45 | + && extension[1..] |
| 46 | + .bytes() |
| 47 | + .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'-' || c == b'_' || c == b'+') |
| 48 | + // Legacy syntax metadata also recognizes compiled/compressed containers. |
| 49 | + // Those are not safe default text-editor associations. |
| 50 | + && !matches!(extension, ".rlib" | ".svgz") |
| 51 | +} |
| 52 | + |
| 53 | +fn valid_language(id: &str) -> bool { |
| 54 | + !id.is_empty() |
| 55 | + && id.len() <= 64 |
| 56 | + && id |
| 57 | + .bytes() |
| 58 | + .all(|c| c.is_ascii_alphanumeric() || c == b'-' || c == b'_') |
| 59 | +} |
| 60 | + |
| 61 | +/// Sorts and deduplicates metadata without loading grammars or inspecting user files. |
| 62 | +pub fn catalog_json(payload: Value) -> Result<Value, RuleError> { |
| 63 | + let request: CatalogRequest = serde_json::from_value(payload) |
| 64 | + .map_err(|_| invalid("Invalid built-in language catalog"))?; |
| 65 | + if request.languages.len() > 128 { |
| 66 | + return Err(invalid("Too many built-in languages")); |
| 67 | + } |
| 68 | + let mut formats = BTreeMap::new(); |
| 69 | + let mut count = 0; |
| 70 | + for language in request.languages { |
| 71 | + if !valid_language(&language.id) { |
| 72 | + return Err(invalid("Invalid built-in language identity")); |
| 73 | + } |
| 74 | + count += language.extensions.len(); |
| 75 | + if count > MAX_FORMATS { |
| 76 | + return Err(invalid("Too many built-in file extensions")); |
| 77 | + } |
| 78 | + if !language.highlighted && language.id != "plaintext" { |
| 79 | + continue; |
| 80 | + } |
| 81 | + for extension in language.extensions { |
| 82 | + if safe_extension(&extension) { |
| 83 | + formats |
| 84 | + .entry(extension) |
| 85 | + .or_insert_with(|| language.id.clone()); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + let formats: Vec<Format> = formats |
| 90 | + .into_iter() |
| 91 | + .map(|(extension, language)| Format { |
| 92 | + extension, |
| 93 | + language, |
| 94 | + }) |
| 95 | + .collect(); |
| 96 | + Ok(json!({"formats": formats})) |
| 97 | +} |
| 98 | + |
| 99 | +/// Display names are excluded: only native identities and owned registration revisions conflict. |
| 100 | +#[derive(Debug, Deserialize, PartialEq, Eq)] |
| 101 | +#[serde(rename_all = "camelCase", deny_unknown_fields)] |
| 102 | +struct Observation { |
| 103 | + extension: String, |
| 104 | + default_id: String, |
| 105 | + registration: String, |
| 106 | +} |
| 107 | + |
| 108 | +#[derive(Deserialize)] |
| 109 | +#[serde(rename_all = "camelCase", deny_unknown_fields)] |
| 110 | +struct PlanRequest { |
| 111 | + formats: Vec<Format>, |
| 112 | + selected: Vec<String>, |
| 113 | + baseline: Vec<Observation>, |
| 114 | + current: Vec<Observation>, |
| 115 | +} |
| 116 | + |
| 117 | +/// Refuses stale selections and returns a deterministic additive registration plan. |
| 118 | +/// |
| 119 | +/// Hosts supply revisions of their owned native registration, re-read before applying the plan. |
| 120 | +/// Only selected suffixes enter the plan; native content-type aliases may share defaults. |
| 121 | +/// Windows UserChoice is never part of a write plan. |
| 122 | +pub fn plan_json(payload: Value) -> Result<Value, RuleError> { |
| 123 | + let request: PlanRequest = |
| 124 | + serde_json::from_value(payload).map_err(|_| invalid("Invalid file-association plan"))?; |
| 125 | + if request.formats.len() > MAX_FORMATS |
| 126 | + || request.selected.is_empty() |
| 127 | + || request.selected.len() > MAX_FORMATS |
| 128 | + || request.baseline.len() > MAX_FORMATS |
| 129 | + || request.current.len() > MAX_FORMATS |
| 130 | + { |
| 131 | + return Err(invalid("File-association plan exceeds its bounds")); |
| 132 | + } |
| 133 | + let allowed: BTreeSet<&str> = request |
| 134 | + .formats |
| 135 | + .iter() |
| 136 | + .map(|f| f.extension.as_str()) |
| 137 | + .collect(); |
| 138 | + if allowed.len() != request.formats.len() |
| 139 | + || allowed.iter().any(|e| !safe_extension(e)) |
| 140 | + || request.formats.iter().any(|f| !valid_language(&f.language)) |
| 141 | + { |
| 142 | + return Err(invalid("Invalid file-association formats")); |
| 143 | + } |
| 144 | + let selected: BTreeSet<&str> = request.selected.iter().map(String::as_str).collect(); |
| 145 | + if selected.len() != request.selected.len() || !selected.is_subset(&allowed) { |
| 146 | + return Err(invalid("Selection is not in the built-in text catalog")); |
| 147 | + } |
| 148 | + let observations = |
| 149 | + |items: Vec<Observation>| -> Result<BTreeMap<String, Observation>, RuleError> { |
| 150 | + let mut out = BTreeMap::new(); |
| 151 | + for item in items { |
| 152 | + if !allowed.contains(item.extension.as_str()) |
| 153 | + || item.default_id.len() > 32768 |
| 154 | + || item.registration.len() > 128 |
| 155 | + { |
| 156 | + return Err(invalid("Invalid native association observation")); |
| 157 | + } |
| 158 | + if out.insert(item.extension.clone(), item).is_some() { |
| 159 | + return Err(invalid("Duplicate native association observation")); |
| 160 | + } |
| 161 | + } |
| 162 | + Ok(out) |
| 163 | + }; |
| 164 | + let baseline = observations(request.baseline)?; |
| 165 | + let current = observations(request.current)?; |
| 166 | + for extension in &selected { |
| 167 | + if !baseline.contains_key(*extension) || !current.contains_key(*extension) { |
| 168 | + return Err(invalid("Missing native association observation")); |
| 169 | + } |
| 170 | + if baseline.get(*extension) != current.get(*extension) { |
| 171 | + return Ok(json!({"conflict": true, "extensions": []})); |
| 172 | + } |
| 173 | + } |
| 174 | + Ok(json!({"conflict": false, "extensions": selected})) |
| 175 | +} |
0 commit comments