Skip to content

feat: add crates.io search engine adapter (#145)#191

Open
josephkehan-prog wants to merge 2 commits into
KnockOutEZ:mainfrom
josephkehan-prog:feat/145-crates-io-engine
Open

feat: add crates.io search engine adapter (#145)#191
josephkehan-prog wants to merge 2 commits into
KnockOutEZ:mainfrom
josephkehan-prog:feat/145-crates-io-engine

Conversation

@josephkehan-prog

@josephkehan-prog josephkehan-prog commented Jul 18, 2026

Copy link
Copy Markdown

Closes #145

Adds CratesIoEngine using the crates.io public JSON API (crates.io/api/v1/crates) — scrape-free adapter for the code vertical.

  • Descriptive User-Agent per crates.io crawler policy (matches wikipedia/marginalia/lobsters convention)
  • Maps crate name → title, crates.io/crates/<name> → url, description + version/downloads → snippet
  • Registered as secondary engine (weight 0.3, quality high, secondary: true) alongside MdnEngine
  • 10 unit tests mirroring hn-algolia.test.ts mocked-fetch pattern (UA header asserted, no live network)

Test plan

  • npx vitest run tests/unit/search/engines/crates-io.test.ts — 10/10
  • npx tsc --noEmit clean

Note: will conflict trivially with #144's PR in code.ts registration — whichever merges second rebases in minutes.

Summary by CodeRabbit

  • New Features

    • Added crates.io search results to code searches.
    • Results include crate descriptions, latest versions, download counts, and direct links.
    • Secondary results are ranked appropriately alongside other code sources.
  • Bug Fixes

    • Improved handling of incomplete or unavailable crate metadata.
    • Search failures and unsuccessful responses now surface reliably.

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@josephkehan-prog, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 5 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 020afe90-6594-4e0b-a752-c36ab965624a

📥 Commits

Reviewing files that changed from the base of the PR and between 59fceb5 and a3620e4.

📒 Files selected for processing (2)
  • src/search/core/engine-quality.ts
  • tests/unit/search/v1/verticals/code.test.ts
📝 Walkthrough

Walkthrough

Adds a crates.io API-backed search engine, registers it as a weighted secondary engine for the code vertical, and adds unit tests for request construction, response parsing, error handling, and edge cases.

Changes

Crates.io code search

Layer / File(s) Summary
Crates.io adapter implementation
src/search/engines/crates-io.ts
Implements CratesIoEngine, including crates.io requests, timeouts, pagination, headers, error handling, field normalization, and RawSearchResult mapping.
Code vertical registration
src/search/core/verticals/code.ts
Registers crates.io as a secondary engine with weight 0.3 and documents secondary-result demotion behavior.
Adapter request and parsing tests
tests/unit/search/engines/crates-io.test.ts
Tests request headers and pagination, result mapping, missing fields, HTTP errors, empty responses, and propagated fetch failures.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CodeSearch
  participant CratesIoEngine
  participant CratesIoAPI
  CodeSearch->>CratesIoEngine: search query with timeout and maxResults
  CratesIoEngine->>CratesIoAPI: fetch crates.io API results
  CratesIoAPI-->>CratesIoEngine: return crate JSON
  CratesIoEngine-->>CodeSearch: return weighted raw search results
Loading

Possibly related issues

  • #144: Adds and registers another package-registry search engine with API-backed results and unit tests, matching this PR’s objective.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed Title clearly states the main change: adding a crates.io search engine adapter.
Linked Issues check ✅ Passed The PR adds and registers the crates.io adapter, uses a descriptive User-Agent, respects timeout/maxResults, and includes tests.
Out of Scope Changes check ✅ Passed The changes stay focused on the crates.io adapter, its registration, and related tests/docs.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
src/search/engines/crates-io.ts (2)

37-39: 🩺 Stability & Availability | 🔵 Trivial | 💤 Low value

Cap maxResults to the API limit.

The crates.io API limits per_page to a maximum of 100. Consider capping maxResults defensively so that unexpectedly large requested limits do not result in a 400 Bad Request error from the upstream API.

♻️ Proposed refactor
   async search(query: string, options: SearchEngineOptions = {}): Promise<RawSearchResult[]> {
     const timeoutMs = options.timeoutMs ?? 10000;
-    const maxResults = options.maxResults ?? 10;
+    const maxResults = Math.min(options.maxResults ?? 10, 100);

     const params = new URLSearchParams({
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/search/engines/crates-io.ts` around lines 37 - 39, Cap the maxResults
value initialized in the crates.io search options to the API-supported maximum
of 100, while preserving the existing default of 10 and allowing smaller
requested values unchanged.

71-75: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Prevent leading space in snippets with missing descriptions.

When a crate lacks a description but has a max_version, the current logic produces a snippet with an awkward leading space (e.g., " (v0.1.0, 10 downloads)"). Filtering out empty strings prevents this formatting quirk and creates a cleaner search snippet.

  • src/search/engines/crates-io.ts#L71-L75: Use an array and .filter(Boolean) to construct the snippet without a leading space.
  • tests/unit/search/engines/crates-io.test.ts#L66-L68: Remove the leading space from the test's expected snippet output to match the updated formatting.
♻️ Proposed refactors

src/search/engines/crates-io.ts

       const description = asString(crate.description) ?? '';
       const downloads = asNumber(crate.downloads) ?? 0;
       const maxVersion = asString(crate.max_version);
-      const snippet = maxVersion ? `${description} (v${maxVersion}, ${downloads} downloads)` : description;
+      const suffix = maxVersion ? `(v${maxVersion}, ${downloads} downloads)` : '';
+      const snippet = [description, suffix].filter(Boolean).join(' ');

       results.push({

tests/unit/search/engines/crates-io.test.ts

     const results = await new CratesIoEngine().search('foo');
-    expect(results[0].snippet).toBe(' (v0.1.0, 10 downloads)');
+    expect(results[0].snippet).toBe('(v0.1.0, 10 downloads)');
   });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/search/engines/crates-io.ts` around lines 71 - 75, Update the snippet
construction in crates-io.ts around description, downloads, and maxVersion to
build the components with an array and filter out empty values before joining,
preventing a leading space when the description is missing. Update the expected
snippet in tests/unit/search/engines/crates-io.test.ts at lines 66-68 to remove
the leading space; both sites require these changes.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/search/engines/crates-io.ts`:
- Around line 37-39: Cap the maxResults value initialized in the crates.io
search options to the API-supported maximum of 100, while preserving the
existing default of 10 and allowing smaller requested values unchanged.
- Around line 71-75: Update the snippet construction in crates-io.ts around
description, downloads, and maxVersion to build the components with an array and
filter out empty values before joining, preventing a leading space when the
description is missing. Update the expected snippet in
tests/unit/search/engines/crates-io.test.ts at lines 66-68 to remove the leading
space; both sites require these changes.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c6639398-d5d3-4f41-8e71-d746d41822b0

📥 Commits

Reviewing files that changed from the base of the PR and between a15277b and 59fceb5.

📒 Files selected for processing (3)
  • src/search/core/verticals/code.ts
  • src/search/engines/crates-io.ts
  • tests/unit/search/engines/crates-io.test.ts

josephkehan-prog added a commit to josephkehan-prog/wigolo that referenced this pull request Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a crates.io search engine adapter

1 participant