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
//! `yarner-lib` is a simple library for building plugins for the Literate Programming tool [Yarner](https://github.com/mlange-42/yarner).
//!
//! See chapter [Writing plugins](https://mlange-42.github.io/yarner/plugins/writing.html) of the Yarner user guide on how to use it.
//!

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use toml::Value;

mod document;

pub use document::*;

/// Version of this library
pub const YARNER_VERSION: &str = env!(
    "CARGO_PKG_VERSION",
    "Environmental variable CARGO_PKG_VERSION not found"
);

/// Plugin call context
#[derive(Debug, Serialize, Deserialize)]
pub struct YarnerData {
    /// The context of the plugin call, including configuration
    pub context: Context,
    /// The documents, mapped to file paths
    pub documents: HashMap<PathBuf, Document>,
}

/// Plugin call context
#[derive(Debug, Serialize, Deserialize)]
pub struct Context {
    /// Configuration of plugin
    pub config: Value,
    /// Name of the plugin
    pub name: String,
    /// Yarner version from from which the plugin is called
    pub yarner_version: String,
}

/// Read inputs from STDIN and parse into Context and Documents
pub fn parse_input() -> serde_json::Result<YarnerData> {
    serde_json::from_reader(std::io::stdin())
}

/// Write Documents as JSON to STDOUT
pub fn write_output(data: &YarnerData) -> serde_json::Result<()> {
    println!("{}", serde_json::to_string_pretty(data)?);
    Ok(())
}