open-library-api-rs 0.1.0

Async Rust client for the Open Library API
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
// v0.0.1
use crate::error::{Error, Result};

const MAX_RESPONSE_BYTES: u64 = 10 * 1024 * 1024; // 10 MB

pub(crate) fn max_response_bytes() -> u64 {
    MAX_RESPONSE_BYTES
}

// ── OLID helpers ──────────────────────────────────────────────────────────────

fn validate_olid(s: &str, suffix: char) -> Result<()> {
    if !s.starts_with("OL") {
        return Err(Error::InvalidInput(format!(
            "OLID must start with 'OL', got: {s}"
        )));
    }
    if !s.ends_with(suffix) {
        return Err(Error::InvalidInput(format!(
            "OLID must end with '{suffix}', got: {s}"
        )));
    }
    let middle = &s[2..s.len() - 1];
    if middle.is_empty() || !middle.chars().all(|c| c.is_ascii_digit()) {
        return Err(Error::InvalidInput(format!(
            "OLID middle section must be all digits, got: {s}"
        )));
    }
    Ok(())
}

pub fn validate_work_id(id: &str) -> Result<()> {
    validate_olid(id, 'W')
}

pub fn validate_edition_id(id: &str) -> Result<()> {
    validate_olid(id, 'M')
}

pub fn validate_author_id(id: &str) -> Result<()> {
    validate_olid(id, 'A')
}

// ── ISBN ──────────────────────────────────────────────────────────────────────

pub fn validate_isbn(isbn: &str) -> Result<()> {
    let digits: String = isbn.chars().filter(|c| c.is_ascii_alphanumeric()).collect();
    match digits.len() {
        10 => validate_isbn10(&digits),
        13 => validate_isbn13(&digits),
        _ => Err(Error::InvalidInput(format!(
            "ISBN must be 10 or 13 characters, got {} characters: {isbn}",
            digits.len()
        ))),
    }
}

fn validate_isbn10(s: &str) -> Result<()> {
    let bytes = s.as_bytes();
    let mut sum: u32 = 0;
    for (i, &b) in bytes.iter().enumerate() {
        let digit = if i == 9 && (b == b'X' || b == b'x') {
            10
        } else if b.is_ascii_digit() {
            (b - b'0') as u32
        } else {
            return Err(Error::InvalidInput(format!(
                "invalid ISBN-10 character '{}'",
                b as char
            )));
        };
        sum += digit * (10 - i as u32);
    }
    if !sum.is_multiple_of(11) {
        return Err(Error::InvalidInput(format!(
            "invalid ISBN-10 check digit: {s}"
        )));
    }
    Ok(())
}

fn validate_isbn13(s: &str) -> Result<()> {
    if !s.starts_with("978") && !s.starts_with("979") {
        return Err(Error::InvalidInput(format!(
            "ISBN-13 must start with 978 or 979: {s}"
        )));
    }
    let bytes = s.as_bytes();
    let mut sum: u32 = 0;
    for (i, &b) in bytes.iter().enumerate() {
        if !b.is_ascii_digit() {
            return Err(Error::InvalidInput(format!(
                "invalid ISBN-13 character '{}' at position {i}",
                b as char
            )));
        }
        let digit = (b - b'0') as u32;
        sum += digit * if i % 2 == 0 { 1 } else { 3 };
    }
    if !sum.is_multiple_of(10) {
        return Err(Error::InvalidInput(format!(
            "invalid ISBN-13 check digit: {s}"
        )));
    }
    Ok(())
}

// ── Subject slug ──────────────────────────────────────────────────────────────

pub fn validate_subject_slug(slug: &str) -> Result<()> {
    if slug.is_empty() {
        return Err(Error::InvalidInput("subject slug must not be empty".into()));
    }
    if slug.len() > 200 {
        return Err(Error::InvalidInput(format!(
            "subject slug must be ≤ 200 chars, got {}",
            slug.len()
        )));
    }
    if !slug
        .chars()
        .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
    {
        return Err(Error::InvalidInput(format!(
            "subject slug must match [a-z0-9_]+: {slug}"
        )));
    }
    Ok(())
}

// ── Username ──────────────────────────────────────────────────────────────────

pub fn validate_username(username: &str) -> Result<()> {
    if username.is_empty() {
        return Err(Error::InvalidInput("username must not be empty".into()));
    }
    if username.len() > 64 {
        return Err(Error::InvalidInput(format!(
            "username must be ≤ 64 chars, got {}",
            username.len()
        )));
    }
    if !username
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
    {
        return Err(Error::InvalidInput(format!(
            "username must match [a-zA-Z0-9_-]+: {username}"
        )));
    }
    Ok(())
}

// ── List ID ───────────────────────────────────────────────────────────────────

pub fn validate_list_id(id: &str) -> Result<()> {
    if id.is_empty() {
        return Err(Error::InvalidInput("list ID must not be empty".into()));
    }
    if !id
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
    {
        return Err(Error::InvalidInput(format!(
            "list ID must match [a-zA-Z0-9_-]+: {id}"
        )));
    }
    Ok(())
}

// ── Search query ──────────────────────────────────────────────────────────────

pub fn validate_search_query(q: &str) -> Result<()> {
    if q.is_empty() {
        return Err(Error::InvalidInput(
            "search query must not be empty".into(),
        ));
    }
    if q.len() > 1000 {
        return Err(Error::InvalidInput(format!(
            "search query must be ≤ 1000 chars, got {}",
            q.len()
        )));
    }
    Ok(())
}

// ── Pagination ────────────────────────────────────────────────────────────────

pub fn validate_limit(limit: u32) -> Result<()> {
    if limit == 0 || limit > 1000 {
        return Err(Error::InvalidInput(format!(
            "limit must be 1–1000, got {limit}"
        )));
    }
    Ok(())
}

// offset ≥ 0 is guaranteed by u32/usize type; no runtime check needed.

// ── Bibkey ────────────────────────────────────────────────────────────────────

const VALID_BIBKEY_PREFIXES: &[&str] = &["ISBN:", "OCLC:", "LCCN:", "OLID:", "ID:"];

pub fn validate_bibkey(key: &str) -> Result<()> {
    if key.is_empty() {
        return Err(Error::InvalidInput("bibkey must not be empty".into()));
    }
    if !VALID_BIBKEY_PREFIXES
        .iter()
        .any(|prefix| key.starts_with(prefix))
    {
        return Err(Error::InvalidInput(format!(
            "bibkey must start with one of {:?}: {key}",
            VALID_BIBKEY_PREFIXES
        )));
    }
    let value = key.split_once(':').map(|x| x.1).unwrap_or("");
    if value.is_empty() {
        return Err(Error::InvalidInput(format!(
            "bibkey value after ':' must not be empty: {key}"
        )));
    }
    Ok(())
}

pub fn validate_bibkeys(keys: &[String]) -> Result<()> {
    if keys.is_empty() {
        return Err(Error::InvalidInput(
            "bibkeys list must not be empty".into(),
        ));
    }
    for key in keys {
        validate_bibkey(key)?;
    }
    Ok(())
}

// ── Date ──────────────────────────────────────────────────────────────────────

/// Validates a date string in `YYYY-MM-DD` format.
pub fn validate_date(date: &str) -> Result<()> {
    let parts: Vec<&str> = date.split('-').collect();
    if parts.len() != 3 {
        return Err(Error::InvalidInput(format!(
            "date must be YYYY-MM-DD, got: {date}"
        )));
    }
    let year: u32 = parts[0]
        .parse()
        .map_err(|_| Error::InvalidInput(format!("invalid year in date: {date}")))?;
    let month: u32 = parts[1]
        .parse()
        .map_err(|_| Error::InvalidInput(format!("invalid month in date: {date}")))?;
    let day: u32 = parts[2]
        .parse()
        .map_err(|_| Error::InvalidInput(format!("invalid day in date: {date}")))?;

    if year < 1 || !(1..=12).contains(&month) || !(1..=31).contains(&day) {
        return Err(Error::InvalidInput(format!(
            "date out of range: {date}"
        )));
    }
    Ok(())
}

// ── Contact email (User-Agent only) ──────────────────────────────────────────

pub fn validate_contact_email(email: &str) -> Result<()> {
    if email.is_empty() {
        return Err(Error::InvalidInput(
            "contact email must not be empty".into(),
        ));
    }
    if !email.contains('@') || email.starts_with('@') || email.ends_with('@') {
        return Err(Error::InvalidInput(format!(
            "contact email does not look valid: {email}"
        )));
    }
    Ok(())
}

// ─────────────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
    use super::*;

    // ── Work / Edition / Author OLIDs ─────────────────────────────────────────

    #[test]
    fn work_id_valid() {
        assert!(validate_work_id("OL45804W").is_ok());
        assert!(validate_work_id("OL1W").is_ok());
    }

    #[test]
    fn work_id_wrong_suffix() {
        assert!(validate_work_id("OL45804M").is_err());
        assert!(validate_work_id("OL45804A").is_err());
    }

    #[test]
    fn work_id_no_prefix() {
        assert!(validate_work_id("45804W").is_err());
    }

    #[test]
    fn work_id_non_digit_middle() {
        assert!(validate_work_id("OLabcW").is_err());
    }

    #[test]
    fn edition_id_valid() {
        assert!(validate_edition_id("OL7353617M").is_ok());
    }

    #[test]
    fn author_id_valid() {
        assert!(validate_author_id("OL23919A").is_ok());
    }

    // ── ISBN-10 ───────────────────────────────────────────────────────────────

    #[test]
    fn isbn10_valid() {
        // "0-306-40615-2" → check digit valid
        assert!(validate_isbn("0306406152").is_ok());
        // With X check digit (Knuth TAOCP vol.3)
        assert!(validate_isbn("080442957X").is_ok());
    }

    #[test]
    fn isbn10_bad_check_digit() {
        assert!(validate_isbn("0306406151").is_err());
    }

    // ── ISBN-13 ───────────────────────────────────────────────────────────────

    #[test]
    fn isbn13_valid() {
        assert!(validate_isbn("9780306406157").is_ok());
    }

    #[test]
    fn isbn13_bad_prefix() {
        assert!(validate_isbn("1230306406157").is_err());
    }

    #[test]
    fn isbn13_bad_check_digit() {
        assert!(validate_isbn("9780306406158").is_err());
    }

    // ── Subject slug ──────────────────────────────────────────────────────────

    #[test]
    fn subject_slug_valid() {
        assert!(validate_subject_slug("love").is_ok());
        assert!(validate_subject_slug("science_fiction").is_ok());
        assert!(validate_subject_slug("history2024").is_ok());
    }

    #[test]
    fn subject_slug_uppercase_rejected() {
        assert!(validate_subject_slug("Love").is_err());
    }

    #[test]
    fn subject_slug_empty_rejected() {
        assert!(validate_subject_slug("").is_err());
    }

    // ── Username ──────────────────────────────────────────────────────────────

    #[test]
    fn username_valid() {
        assert!(validate_username("alice").is_ok());
        assert!(validate_username("alice_123-ok").is_ok());
    }

    #[test]
    fn username_empty_rejected() {
        assert!(validate_username("").is_err());
    }

    #[test]
    fn username_too_long_rejected() {
        let long = "a".repeat(65);
        assert!(validate_username(&long).is_err());
    }

    // ── Limit ─────────────────────────────────────────────────────────────────

    #[test]
    fn limit_valid() {
        assert!(validate_limit(1).is_ok());
        assert!(validate_limit(1000).is_ok());
    }

    #[test]
    fn limit_zero_rejected() {
        assert!(validate_limit(0).is_err());
    }

    #[test]
    fn limit_over_max_rejected() {
        assert!(validate_limit(1001).is_err());
    }

    // ── Bibkeys ───────────────────────────────────────────────────────────────

    #[test]
    fn bibkey_valid_prefixes() {
        assert!(validate_bibkey("ISBN:0306406152").is_ok());
        assert!(validate_bibkey("OCLC:45883427").is_ok());
        assert!(validate_bibkey("LCCN:2004046975").is_ok());
        assert!(validate_bibkey("OLID:OL7408846M").is_ok());
        assert!(validate_bibkey("ID:5428012").is_ok());
    }

    #[test]
    fn bibkey_unknown_prefix_rejected() {
        assert!(validate_bibkey("ASIN:B000FC1234").is_err());
    }

    #[test]
    fn bibkeys_empty_list_rejected() {
        assert!(validate_bibkeys(&[]).is_err());
    }

    // ── Date ──────────────────────────────────────────────────────────────────

    #[test]
    fn date_valid() {
        assert!(validate_date("2024-06-15").is_ok());
    }

    #[test]
    fn date_invalid_format() {
        assert!(validate_date("20240615").is_err());
        assert!(validate_date("2024/06/15").is_err());
    }

    // ── Email ─────────────────────────────────────────────────────────────────

    #[test]
    fn email_valid() {
        assert!(validate_contact_email("me@example.com").is_ok());
    }

    #[test]
    fn email_no_at_rejected() {
        assert!(validate_contact_email("notanemail").is_err());
    }
}