citeworks_csl/
lib.rs

1//! Citation Style Language serde types and implementations.
2//!
3//! This targets CSL 1.0.2 but may not support the entire specification.
4//!
5//! At the moment, only CSL-JSON items are covered.
6//!
7//! The top level API mimics [serde_json]'s:
8//!
9//! ```
10//! let csl = citeworks_csl::from_str(r#"
11//! [{
12//!   "id": "example-id",
13//!   "type": "report",
14//!   "author": [
15//!     {"given": "Jane", "family": "Roe"},
16//!     {"literal": "John Doe"}
17//!   ]
18//! }]
19//! "#).unwrap();
20//!
21//! assert_eq!(csl[0].author[0].given, Some("Jane".into()));
22//! ```
23#![warn(clippy::unwrap_used, missing_docs)]
24#![deny(rust_2018_idioms)]
25#![forbid(unsafe_code)]
26
27use std::io::{Read, Write};
28
29pub use serde_json::Result;
30
31pub use items::Item;
32
33pub mod dates;
34pub mod items;
35pub mod names;
36pub mod ordinaries;
37
38/// Deserialize CSL items from an IO stream of JSON.
39pub fn from_reader<R>(rdr: R) -> Result<Vec<Item>>
40where
41	R: Read,
42{
43	serde_json::from_reader(rdr)
44}
45
46/// Deserialize CSL items from bytes of JSON text.
47pub fn from_slice(v: &[u8]) -> Result<Vec<Item>> {
48	serde_json::from_slice(v)
49}
50
51/// Deserialize CSL items from a string of JSON text.
52pub fn from_str(s: &str) -> Result<Vec<Item>> {
53	serde_json::from_str(s)
54}
55
56/// Serialize the given CSL items as a String of JSON.
57pub fn to_string(value: &[Item]) -> Result<String> {
58	serde_json::to_string(value)
59}
60
61/// Serialize the given CSL items as a pretty-printed String of JSON.
62pub fn to_string_pretty(value: &[Item]) -> Result<String> {
63	serde_json::to_string_pretty(value)
64}
65
66/// Serialize the given CSL items as a JSON byte vector.
67pub fn to_vec(value: &[Item]) -> Result<Vec<u8>> {
68	serde_json::to_vec(value)
69}
70
71/// Serialize the given CSL items as a pretty-printed JSON byte vector.
72pub fn to_vec_pretty(value: &[Item]) -> Result<Vec<u8>> {
73	serde_json::to_vec_pretty(value)
74}
75
76/// Serialize the given CSL items as JSON into the IO stream.
77pub fn to_writer<W>(writer: W, value: &[Item]) -> Result<()>
78where
79	W: Write,
80{
81	serde_json::to_writer(writer, value)
82}
83
84/// Serialize the given CSL items as pretty-printed JSON into the IO stream.
85pub fn to_writer_pretty<W>(writer: W, value: &[Item]) -> Result<()>
86where
87	W: Write,
88{
89	serde_json::to_writer_pretty(writer, value)
90}