luthien_plugin/
lib.rs

1//! # Luthien Plugins in Rust
2//!
3//! `luthien-plugin` is a Rust library for writing Luthien plugins in Rust. It is not to be
4//! confused with `luthien` itself.
5//!
6//! ## Input Deserialization
7//! `luthien-plugin` provides plugin input data structures which can be deserialized with Serde, as well
8//! as a utility function to get the input from stdin.
9//!
10//! ## Luthien IO
11//! Luthien provides a named pipe which copies to and from its stdout and stdin respectively.
12//! `luthien-plugin` can automatically get this pipe for you.
13
14use serde::{Deserialize, Serialize};
15use std::path::PathBuf;
16
17pub mod theme;
18
19#[cfg(feature = "io")]
20pub mod io;
21
22pub use theme::{Colors, Palette, Theme};
23
24pub use serde_json;
25
26#[cfg(feature = "palette")]
27pub use palette;
28#[cfg(not(feature = "palette"))]
29pub mod palette {
30    use serde::{Deserialize, Serialize};
31
32    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
33    pub struct Srgb<T = f32> {
34        pub red: T,
35        pub green: T,
36        pub blue: T,
37    }
38
39    impl<T> Srgb<T> {
40        pub const fn new(red: T, green: T, blue: T) -> Self {
41            Self { red, green, blue }
42        }
43    }
44}
45
46/// The directories in which the plugin should store and output data.
47///
48/// These directories are guarunteed to exist and are exclusive to each plugin.
49#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
50pub struct Directories {
51    /// For user configuration of the plugin.
52    pub config: PathBuf,
53    /// For plugin outputs.
54    pub output: PathBuf,
55    /// For cached reproducible data.
56    pub cache: PathBuf,
57    /// For miscellaneous plugin data.
58    pub data: PathBuf,
59}
60
61/// All data passed to the plugin.
62#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
63pub struct Input {
64    #[serde(rename = "pipe")]
65    pub pipe_path: Option<PathBuf>,
66    /// Directories which can be used to store data between runs.
67    pub directories: Directories,
68
69    /// Unique human-readable name.
70    pub name: String,
71    /// User-provided options.
72    pub options: serde_json::Value,
73    /// The provided theme.
74    pub theme: Theme,
75}
76
77/// Get the plugin's input or [`None`] if it couldn't be deserialized.
78pub fn get_input() -> Option<Input> {
79    serde_json::from_reader(&mut std::io::stdin()).ok()
80}