| 171 | |
| 172 | impl Command for Context { |
| 173 | fn run(&self) -> CliResult<()> { |
| 174 | // Argument validation is client-side presentation: it always runs, |
| 175 | // before any service routing engages. |
| 176 | let as_json = self.json || self.format.eq_ignore_ascii_case("json"); |
| 177 | if !as_json && !self.format.eq_ignore_ascii_case("md") { |
| 178 | return Err(CliError::InvalidArgument { |
| 179 | message: format!("Unknown format: {} (expected md or json)", self.format), |
| 180 | }); |
| 181 | } |
| 182 | |
| 183 | if !(1..=MAX_LIMIT).contains(&self.limit) { |
| 184 | return Err(CliError::InvalidArgument { |
| 185 | message: format!("--limit must be between 1 and {MAX_LIMIT}"), |
| 186 | }); |
| 187 | } |
| 188 | |
| 189 | // Every form routes: the gather+rank runs in the service layer |
| 190 | // (the ranking recipe lives in exactly one place — the handler), |
| 191 | // and the renders below are presentation. The local body keeps |
| 192 | // its own gather+rank for the no-service fallback (outside a |
| 193 | // repository) until the legacy body retires. |
| 194 | if crate::commands::rpc::vault_context(self)? { |
| 195 | return Ok(()); |
| 196 | } |
| 197 | |
| 198 | let root = find_repository_root()?; |
| 199 | let repo = |
| 200 | crate::commands::open_readonly_repository(&root).map_err(CliError::Repository)?; |
| 201 | |
| 202 | let limit = self.limit; |
| 203 | let candidates = self.gather_candidates(&repo, limit)?; |
| 204 | let items = resolve_and_rank(&repo, candidates, limit, !self.candidates_only)?; |
| 205 | |
| 206 | if self.candidates_only { |
| 207 | if as_json { |
| 208 | println!("{}", render_candidates_json(&items, self, limit)); |
| 209 | } else { |
| 210 | let md = render_candidates_md(&items); |
| 211 | if !md.is_empty() { |
| 212 | print!("{}", md); |
| 213 | } |
| 214 | } |
| 215 | return Ok(()); |
| 216 | } |
| 217 | |
| 218 | let items = apply_budget(items, self.budget_chars); |
| 219 | |
| 220 | let md = render_md(&items); |
| 221 | if as_json { |
| 222 | println!("{}", render_json(&items, &md, self, limit)); |
| 223 | } else if !md.is_empty() { |
| 224 | print!("{}", md); |
| 225 | } |
| 226 | |
| 227 | Ok(()) |
| 228 | } |
| 229 | } |
| 230 | |