yarner_lib/
lib.rs

1//! `yarner-lib` is a simple library for building plugins for the Literate Programming tool [Yarner](https://github.com/mlange-42/yarner).
2//!
3//! See chapter [Writing plugins](https://mlange-42.github.io/yarner/plugins/writing.html) of the Yarner user guide on how to use it.
4//!
5
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8use std::path::PathBuf;
9use toml::Value;
10
11mod document;
12
13pub use document::*;
14
15/// Version of this library
16pub const YARNER_VERSION: &str = env!(
17    "CARGO_PKG_VERSION",
18    "Environmental variable CARGO_PKG_VERSION not found"
19);
20
21/// Plugin call context
22#[derive(Debug, Serialize, Deserialize)]
23pub struct YarnerData {
24    /// The context of the plugin call, including configuration
25    pub context: Context,
26    /// The documents, mapped to file paths
27    pub documents: HashMap<PathBuf, Document>,
28}
29
30/// Plugin call context
31#[derive(Debug, Serialize, Deserialize)]
32pub struct Context {
33    /// Configuration of plugin
34    pub config: Value,
35    /// Name of the plugin
36    pub name: String,
37    /// Yarner version from from which the plugin is called
38    pub yarner_version: String,
39}
40
41/// Read inputs from STDIN and parse into Context and Documents
42pub fn parse_input() -> serde_json::Result<YarnerData> {
43    serde_json::from_reader(std::io::stdin())
44}
45
46/// Write Documents as JSON to STDOUT
47pub fn write_output(data: &YarnerData) -> serde_json::Result<()> {
48    println!("{}", serde_json::to_string_pretty(data)?);
49    Ok(())
50}