docsrs-cli 1.2.0

One-shot stdin/stdout CLI that fetches crates.io and docs.rs docs for LLM agents without a daemon
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
//! crates.io search-crates operation.

use serde::{Deserialize, Serialize};
use url::Url;

use crate::cli::SortKind;
use crate::config::{
    MAX_CRATE_DESCRIPTION_CHARS, MAX_CRATE_NAME_CHARS, MAX_PER_PAGE, MAX_URL_FIELD_CHARS,
    MAX_VERSION_CHARS,
};
use crate::domain::{AllowedOrigin, SearchQuery};
use crate::error::{AppError, AppResult, ErrorKind};
use crate::http::{HttpClient, decode_utf8, require_content_type_json};

/// Truncate `s` to at most `max_chars` Unicode scalars (char-boundary safe).
///
/// Used when mapping untrusted crates.io JSON fields into agent payloads so a
/// single hostile string cannot force multi-MB allocations before output budget.
fn truncate_chars(s: &str, max_chars: usize) -> String {
    if s.chars().count() <= max_chars {
        return s.to_string();
    }
    s.chars().take(max_chars).collect()
}

/// Cap an optional string field; empty after trim becomes `None`.
fn cap_optional_field(value: Option<String>, max_chars: usize) -> Option<String> {
    value.and_then(|s| {
        let t = s.trim();
        if t.is_empty() {
            None
        } else {
            Some(truncate_chars(t, max_chars))
        }
    })
}

/// One hit from crates.io search.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CrateSearchHit {
    /// Crate name on crates.io.
    pub name: String,
    /// Short crate description (may be empty).
    pub description: String,
    /// Total download count.
    pub downloads: u64,
    /// Newest or default version string for display.
    pub version: String,
    /// Documentation URL when provided by crates.io.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub documentation: Option<String>,
    /// Highest published version when present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_version: Option<String>,
    /// Highest stable version when present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_stable_version: Option<String>,
    /// Default version selected by crates.io.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_version: Option<String>,
    /// Recent download count when present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub recent_downloads: Option<u64>,
    /// Whether the query matched the crate name exactly.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exact_match: Option<bool>,
    /// Whether the default version is yanked.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub yanked: Option<bool>,
    /// Source repository URL when present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repository: Option<String>,
    /// Project homepage URL when present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub homepage: Option<String>,
}

/// Pagination meta from crates.io (may contain `page=` or `seek=` tokens).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SearchMeta {
    /// Total hits reported by crates.io.
    pub total: u64,
    /// Opaque next-page token when more results exist.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_page: Option<String>,
    /// Opaque previous-page token when available.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prev_page: Option<String>,
}

/// Full search-crates data payload.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SearchCratesData {
    /// Original search query string.
    pub query: String,
    /// 1-based page number used for the request.
    pub page: u32,
    /// Page size used for the request.
    pub per_page: u32,
    /// Sort order API token.
    pub sort: String,
    /// Search hits on this page.
    pub hits: Vec<CrateSearchHit>,
    /// Pagination metadata from crates.io.
    pub meta: SearchMeta,
    /// True when the HTTP body was served from the local disk cache.
    pub cache_hit: bool,
    /// True when hits were reduced to fit `--max-output-bytes` (JSON path).
    pub truncated: bool,
}

/// Echo fields for `search-crates` taken from the **effective request URL**
/// (single source of truth after URL planning, including `--page-token`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SearchEcho {
    /// Query string (`q=`).
    pub query: String,
    /// 1-based page.
    pub page: u32,
    /// Page size.
    pub per_page: u32,
    /// Sort token.
    pub sort: String,
}

impl SearchEcho {
    /// Build from CLI-side fallbacks before URL planning.
    pub fn from_cli(query: &SearchQuery, page: u32, per_page: u32, sort: SortKind) -> Self {
        let (per_page, page) = clamp_search_pagination(per_page, page);
        Self {
            query: query.as_str().to_string(),
            page,
            per_page,
            sort: sort.as_api_str().to_string(),
        }
    }
}

/// Derive echo params from the planned URL query string.
///
/// Missing pairs fall back to `fallback` so pure `seek=` tokens and partial
/// tokens still produce a coherent agent-facing payload.
pub fn echo_params_from_url(url: &Url, fallback: &SearchEcho) -> SearchEcho {
    let mut query = fallback.query.clone();
    let mut page = fallback.page;
    let mut per_page = fallback.per_page;
    let mut sort = fallback.sort.clone();
    for (k, v) in url.query_pairs() {
        match k.as_ref() {
            "q" => query = v.into_owned(),
            "page" => {
                if let Ok(n) = v.parse::<u32>() {
                    page = n.max(1);
                }
            }
            "per_page" => {
                if let Ok(n) = v.parse::<u32>() {
                    per_page = n.clamp(1, MAX_PER_PAGE);
                }
            }
            "sort" => sort = v.into_owned(),
            _ => {}
        }
    }
    let (per_page, page) = clamp_search_pagination(per_page, page);
    SearchEcho {
        query,
        page,
        per_page,
        sort,
    }
}

#[derive(Debug, Deserialize)]
struct ApiResponse {
    crates: Vec<ApiCrate>,
    meta: ApiMeta,
}

#[derive(Debug, Deserialize)]
struct ApiCrate {
    name: String,
    description: Option<String>,
    downloads: u64,
    newest_version: Option<String>,
    max_version: Option<String>,
    max_stable_version: Option<String>,
    default_version: Option<String>,
    documentation: Option<String>,
    recent_downloads: Option<u64>,
    exact_match: Option<bool>,
    yanked: Option<bool>,
    repository: Option<String>,
    homepage: Option<String>,
}

#[derive(Debug, Deserialize)]
struct ApiMeta {
    total: u64,
    next_page: Option<String>,
    prev_page: Option<String>,
}

/// Clamp crates.io pagination to product bounds (`per_page` 1..=100, `page` >= 1).
///
/// Used for defensive URL echo / seek tokens. Explicit CLI flags must go through
/// [`validate_search_pagination`] so invalid agent input fails closed (exit 65).
pub fn clamp_search_pagination(per_page: u32, page: u32) -> (u32, u32) {
    (per_page.clamp(1, MAX_PER_PAGE), page.max(1))
}

/// Validate explicit CLI pagination flags (fail-closed).
///
/// # Errors
///
/// Returns [`ErrorKind::InvalidInput`] when `page < 1` or `per_page` is outside
/// `1..=MAX_PER_PAGE` (100).
pub fn validate_search_pagination(per_page: u32, page: u32) -> AppResult<(u32, u32)> {
    if page < 1 {
        return Err(AppError::new(
            ErrorKind::InvalidInput,
            "page must be >= 1 (got 0 or missing)",
        ));
    }
    if !(1..=MAX_PER_PAGE).contains(&per_page) {
        return Err(AppError::new(
            ErrorKind::InvalidInput,
            format!("per_page must be 1..={MAX_PER_PAGE} (got {per_page})"),
        ));
    }
    Ok((per_page, page))
}

/// Absolute crates.io API base (`…/api/v1/crates`) for an allowlisted origin.
fn crates_api_base(origin: &AllowedOrigin) -> String {
    format!("{}/api/v1/crates", origin.as_str().trim_end_matches('/'))
}

/// Build the planned crates.io search URL (production origin).
///
/// # Errors
///
/// Returns [`ErrorKind::Internal`] when the base URL cannot be parsed.
pub fn planned_url(query: &SearchQuery, per_page: u32, sort: SortKind, page: u32) -> AppResult<Url> {
    planned_url_on_host(&AllowedOrigin::crates_io_default(), query, per_page, sort, page)
}

/// Build the crates.io search URL for an allowlisted origin (`page` pagination).
///
/// # Errors
///
/// Returns [`ErrorKind::Internal`] when the composed base URL is invalid.
pub fn planned_url_on_host(
    origin: &AllowedOrigin,
    query: &SearchQuery,
    per_page: u32,
    sort: SortKind,
    page: u32,
) -> AppResult<Url> {
    let (per_page, page) = clamp_search_pagination(per_page, page);
    let base = crates_api_base(origin);
    let mut url = Url::parse(&base)
        .map_err(|e| AppError::with_source(ErrorKind::Internal, "invalid crates.io base URL", e))?;
    {
        let mut q = url.query_pairs_mut();
        q.append_pair("q", query.as_str());
        q.append_pair("per_page", &per_page.to_string());
        q.append_pair("sort", sort.as_api_str());
        q.append_pair("page", &page.to_string());
    }
    Ok(url)
}

/// Build search URL using an opaque `next_page` / `prev_page` token from crates.io meta.
///
/// Accepts:
/// - full query string (`?q=…&page=2` or `q=…&page=2`)
/// - pure page number string
/// - `seek=…` tokens
///
/// # Errors
///
/// Returns [`ErrorKind::Internal`] when the base URL is invalid.
/// Returns [`ErrorKind::InvalidInput`] when the token cannot form a valid URL.
pub fn planned_url_with_page_token(
    origin: &AllowedOrigin,
    query: &SearchQuery,
    per_page: u32,
    sort: SortKind,
    page_token: &str,
) -> AppResult<Url> {
    let token = page_token.trim();
    if token.is_empty() {
        return Err(AppError::new(
            ErrorKind::InvalidInput,
            "page-token is empty",
        ));
    }
    // Reject obvious garbage that is neither a page number, query string, nor seek token
    // (GAP-W-007 — fail local before remote "bad request" wording confuses agents).
    if token.chars().any(|c| c.is_control())
        || token.len() > crate::config::MAX_URL_FIELD_CHARS
    {
        return Err(AppError::new(
            ErrorKind::InvalidInput,
            "page-token is invalid (control characters or exceeds max length)",
        ));
    }
    // Pure numeric → page number
    if let Ok(page) = token.parse::<u32>() {
        return planned_url_on_host(origin, query, per_page, sort, page);
    }
    let base = crates_api_base(origin);
    let base_url = Url::parse(&base)
        .map_err(|e| AppError::with_source(ErrorKind::Internal, "invalid crates.io base URL", e))?;

    let qs = token.trim_start_matches('?');
    // If token already carries a full query, use it as the request query.
    if qs.contains('=') {
        let mut url = base_url;
        url.set_query(Some(qs));
        return Ok(url);
    }
    // Opaque seek-like token
    let (per_page, _) = clamp_search_pagination(per_page, 1);
    let mut url = base_url;
    {
        let mut q = url.query_pairs_mut();
        q.append_pair("q", query.as_str());
        q.append_pair("per_page", &per_page.to_string());
        q.append_pair("sort", sort.as_api_str());
        q.append_pair("seek", qs);
    }
    Ok(url)
}
/// Parse crates.io JSON body into product types (offline-testable).
///
/// # Errors
///
/// Returns [`ErrorKind::Parse`] when `text` is not valid crates.io search JSON.
pub fn parse_search_body(
    text: &str,
    query: &str,
    page: u32,
    per_page: u32,
    sort: &str,
    cache_hit: bool,
) -> AppResult<SearchCratesData> {
    let (per_page, page) = clamp_search_pagination(per_page, page);
    let parsed: ApiResponse = serde_json::from_str(text).map_err(|e| {
        AppError::with_source(ErrorKind::Parse, "failed to parse crates.io JSON", e)
    })?;

    let hits = parsed
        .crates
        .into_iter()
        .map(|c| {
            let version_raw = c
                .newest_version
                .or_else(|| c.max_version.clone())
                .unwrap_or_else(|| "unknown".to_string());
            CrateSearchHit {
                // Cap name length (hostile API); charset is not re-validated here —
                // crates.io is the source of truth for published names.
                name: truncate_chars(&c.name, MAX_CRATE_NAME_CHARS),
                description: c
                    .description
                    .filter(|s| !s.is_empty())
                    .map(|s| truncate_chars(&s, MAX_CRATE_DESCRIPTION_CHARS))
                    .unwrap_or_else(|| "No description available".to_string()),
                downloads: c.downloads,
                version: truncate_chars(&version_raw, MAX_VERSION_CHARS),
                documentation: cap_optional_field(c.documentation, MAX_URL_FIELD_CHARS),
                max_version: cap_optional_field(c.max_version, MAX_VERSION_CHARS),
                max_stable_version: cap_optional_field(c.max_stable_version, MAX_VERSION_CHARS),
                default_version: cap_optional_field(c.default_version, MAX_VERSION_CHARS),
                recent_downloads: c.recent_downloads,
                exact_match: c.exact_match,
                yanked: c.yanked,
                repository: cap_optional_field(c.repository, MAX_URL_FIELD_CHARS),
                homepage: cap_optional_field(c.homepage, MAX_URL_FIELD_CHARS),
            }
        })
        .collect();

    Ok(SearchCratesData {
        query: query.to_string(),
        page,
        per_page,
        sort: sort.to_string(),
        hits,
        meta: SearchMeta {
            total: parsed.meta.total,
            next_page: cap_optional_field(parsed.meta.next_page, MAX_URL_FIELD_CHARS),
            prev_page: cap_optional_field(parsed.meta.prev_page, MAX_URL_FIELD_CHARS),
        },
        cache_hit,
        truncated: false,
    })
}

/// Execute search-crates against crates.io (production origin).
///
/// # Errors
///
/// Propagates URL build, HTTP, UTF-8 decode, and JSON parse failures from
/// [`search_crates_on_origin`].
pub async fn search_crates(
    http: &HttpClient,
    query: &SearchQuery,
    per_page: u32,
    sort: SortKind,
    page: u32,
) -> AppResult<SearchCratesData> {
    search_crates_on_origin(
        http,
        &AllowedOrigin::crates_io_default(),
        query,
        per_page,
        sort,
        page,
    )
    .await
}

/// Execute search-crates against an allowlisted origin (offline mocks).
///
/// # Errors
///
/// Returns [`ErrorKind::Internal`] when the origin URL is invalid.
/// Propagates HTTP and parse errors from [`search_crates_at`].
pub async fn search_crates_on_origin(
    http: &HttpClient,
    origin: &AllowedOrigin,
    query: &SearchQuery,
    per_page: u32,
    sort: SortKind,
    page: u32,
) -> AppResult<SearchCratesData> {
    let (per_page, page) = clamp_search_pagination(per_page, page);
    let url = planned_url_on_host(origin, query, per_page, sort, page)?;
    search_crates_at(http, &url, query, per_page, sort, page).await
}

/// Execute search against a pre-built URL (mock hosts in tests).
///
/// Echo fields (`query`/`page`/`per_page`/`sort`) are taken from the **URL**
/// via [`echo_params_from_url`], with CLI values as fallback for missing pairs.
///
/// # Errors
///
/// Propagates HTTP transport errors from [`HttpClient::get_json`].
/// Maps non-success HTTP statuses via [`AppError::from_http_status`].
/// Returns [`ErrorKind::Parse`] when the body is not UTF-8 or not valid crates.io JSON.
pub async fn search_crates_at(
    http: &HttpClient,
    url: &Url,
    query: &SearchQuery,
    per_page: u32,
    sort: SortKind,
    page: u32,
) -> AppResult<SearchCratesData> {
    let fallback = SearchEcho::from_cli(query, page, per_page, sort);
    let echo = echo_params_from_url(url, &fallback);
    let resp = http.get_json(url).await?;
    if resp.status.as_u16() == 404 {
        return Err(AppError::from_http_status(404, "crates.io search"));
    }
    if resp.status.as_u16() == 400 {
        // GAP-W-007: distinguish remote rejection of pagination/query from generic remote 400.
        return Err(AppError::from_http_status(
            400,
            "crates.io search (page-token or query rejected by remote)",
        ));
    }
    if !resp.status.is_success() {
        return Err(AppError::from_http_status(
            resp.status.as_u16(),
            "crates.io search",
        ));
    }
    // Fail closed when origin advertises a non-JSON type (MIME confusion).
    require_content_type_json(resp.content_type.as_deref())?;

    let text = decode_utf8(&resp.body)?;
    parse_search_body(
        &text,
        &echo.query,
        echo.page,
        echo.per_page,
        &echo.sort,
        resp.cache_hit,
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::SortKind;
    use crate::domain::SearchQuery;

    fn q(s: &str) -> SearchQuery {
        SearchQuery::parse(s, true).unwrap()
    }

    #[test]
    fn planned_url_clamps_and_encodes() {
        let u = planned_url(&q("serde json"), 0, SortKind::Alphabetical, 0).unwrap();
        assert!(u.as_str().contains("per_page=1"));
        assert!(u.as_str().contains("page=1"));
        assert!(u.as_str().contains("sort=alphabetical"));
        assert!(u.as_str().contains("q=serde"));
        let u2 = planned_url(&q("x"), 500, SortKind::Relevance, 3).unwrap();
        assert!(u2.as_str().contains("per_page=100"));
        assert!(u2.as_str().contains("page=3"));
    }

    #[test]
    fn clamp_search_pagination_page_and_per_page() {
        assert_eq!(clamp_search_pagination(0, 0), (1, 1));
        assert_eq!(clamp_search_pagination(10, 0), (10, 1));
        assert_eq!(clamp_search_pagination(500, 2), (100, 2));
        assert_eq!(clamp_search_pagination(25, 3), (25, 3));
    }

    #[test]
    fn validate_search_pagination_rejects_invalid() {
        assert!(validate_search_pagination(10, 0).is_err());
        assert!(validate_search_pagination(0, 1).is_err());
        assert!(validate_search_pagination(200, 1).is_err());
        assert_eq!(validate_search_pagination(25, 3).unwrap(), (25, 3));
        assert_eq!(validate_search_pagination(100, 1).unwrap(), (100, 1));
    }

    #[test]
    fn parse_fixture_serde() {
        let body = include_str!("../tests/fixtures/crates_io/search_serde.json");
        let data = parse_search_body(body, "serde", 1, 10, "relevance", false).unwrap();
        assert_eq!(data.hits.len(), 2);
        assert_eq!(data.hits[0].name, "serde");
        assert_eq!(data.hits[0].exact_match, Some(true));
        assert_eq!(data.hits[0].version, "1.0.210");
        assert_eq!(data.hits[1].description, "No description available");
        assert_eq!(data.meta.total, 2);
        assert_eq!(data.meta.next_page.as_deref(), Some("page=2"));
    }

    #[test]
    fn parse_fixture_seek_meta() {
        let body = include_str!("../tests/fixtures/crates_io/search_seek_meta.json");
        let data = parse_search_body(body, "example", 1, 10, "downloads", false).unwrap();
        assert_eq!(
            data.meta.next_page.as_deref(),
            Some("seek=ABC123&per_page=10")
        );
        assert_eq!(data.meta.prev_page.as_deref(), Some("page=1"));
        assert_eq!(data.hits[0].version, "0.1.0");
    }

    #[test]
    fn parse_invalid_json() {
        let err = parse_search_body("{", "q", 1, 10, "relevance", false).unwrap_err();
        assert_eq!(err.kind(), ErrorKind::Parse);
    }

    #[test]
    fn parse_missing_versions_uses_unknown() {
        let body = r#"{"crates":[{"name":"x","downloads":1}],"meta":{"total":1}}"#;
        let data = parse_search_body(body, "x", 1, 10, "new", false).unwrap();
        assert_eq!(data.hits[0].version, "unknown");
        assert!(data.meta.next_page.is_none());
    }

    #[test]
    fn planned_url_on_host_http_base() {
        let origin = AllowedOrigin::parse_with("http://127.0.0.1:9", true).unwrap();
        let u = planned_url_on_host(&origin, &q("q"), 10, SortKind::New, 1).unwrap();
        assert!(u.as_str().starts_with("http://127.0.0.1:9/api/v1/crates"));
    }

    #[test]
    fn echo_params_from_page_token_url() {
        let fallback = SearchEcho::from_cli(&q(""), 1, 10, SortKind::Relevance);
        let origin = AllowedOrigin::crates_io_default();
        let url = planned_url_with_page_token(
            &origin,
            &q(""),
            10,
            SortKind::Relevance,
            "?q=serde&per_page=2&sort=relevance&page=2",
        )
        .unwrap();
        let echo = echo_params_from_url(&url, &fallback);
        assert_eq!(echo.query, "serde");
        assert_eq!(echo.page, 2);
        assert_eq!(echo.per_page, 2);
        assert_eq!(echo.sort, "relevance");
    }

    #[test]
    fn echo_params_fallback_when_pairs_missing() {
        let fallback = SearchEcho::from_cli(&q("local"), 3, 25, SortKind::Downloads);
        let url = Url::parse("https://crates.io/api/v1/crates?seek=ABC").unwrap();
        let echo = echo_params_from_url(&url, &fallback);
        assert_eq!(echo.query, "local");
        assert_eq!(echo.page, 3);
        assert_eq!(echo.per_page, 25);
        assert_eq!(echo.sort, "downloads");
    }

    #[test]
    fn parse_caps_oversized_api_strings() {
        let long_name = "a".repeat(MAX_CRATE_NAME_CHARS + 50);
        let long_desc = "d".repeat(MAX_CRATE_DESCRIPTION_CHARS + 100);
        let long_url = format!("https://example.com/{}", "p".repeat(MAX_URL_FIELD_CHARS));
        let long_token = "t".repeat(MAX_URL_FIELD_CHARS + 10);
        let body = format!(
            r#"{{"crates":[{{"name":"{long_name}","description":"{long_desc}","downloads":1,"newest_version":"1.0.0","documentation":"{long_url}","repository":"{long_url}","homepage":"{long_url}"}}],"meta":{{"total":1,"next_page":"{long_token}","prev_page":"{long_token}"}}}}"#
        );
        let data = parse_search_body(&body, "q", 1, 10, "relevance", false).unwrap();
        assert_eq!(data.hits[0].name.chars().count(), MAX_CRATE_NAME_CHARS);
        assert_eq!(
            data.hits[0].description.chars().count(),
            MAX_CRATE_DESCRIPTION_CHARS
        );
        assert_eq!(
            data.hits[0]
                .documentation
                .as_ref()
                .map(|s| s.chars().count()),
            Some(MAX_URL_FIELD_CHARS)
        );
        assert_eq!(
            data.meta.next_page.as_ref().map(|s| s.chars().count()),
            Some(MAX_URL_FIELD_CHARS)
        );
        assert_eq!(
            data.meta.prev_page.as_ref().map(|s| s.chars().count()),
            Some(MAX_URL_FIELD_CHARS)
        );
    }

    #[test]
    fn truncate_chars_preserves_short_and_utf8() {
        assert_eq!(truncate_chars("abc", 10), "abc");
        assert_eq!(truncate_chars("abcdef", 3), "abc");
        // Multi-byte: take by char, not byte.
        assert_eq!(truncate_chars("日本語abc", 2), "日本");
    }
}