visualizer/
visualizable.rs

1use crate::visualizations;
2
3/// Represents a visualization that can be serialized into a json string.
4pub trait Visualization {
5    // TODO: Should be really return a String here?
6    // Maybe some JSON type would be better.
7    // This would make serde a public dependency though.
8    /// Serializes this visualization to JSON.
9    /// The JSON should match [this schema](https://hediet.github.io/visualization/docs/visualization-data-schema.json).
10    /// There is a playground [here](https://hediet.github.io/visualization/?darkTheme=1).
11    fn to_json(&self) -> String;
12}
13
14/// Represents something that can provide a visualization for itself.
15pub trait Visualizable {
16    type V: Visualization;
17
18    /// Returns a suited visualization.
19    fn visualize(&self) -> Self::V;
20}
21
22impl<T: Visualizable> Visualization for T {
23    fn to_json(&self) -> String {
24        self.visualize().to_json()
25    }
26}
27
28impl<'t> Visualizable for &'t str {
29    type V = visualizations::Text<'t>;
30
31    fn visualize(&self) -> Self::V {
32        visualizations::Text::new((*self).into())
33    }
34}