use serde_json::{json, Value};
use super::{Component, Role};
#[derive(Clone, Debug)]
pub struct Output {
id: String,
label: String,
value: String,
}
impl Output {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: String::new(),
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn value(mut self, v: impl Into<String>) -> Self {
self.value = v.into();
self
}
}
impl Component for Output {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"output"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({ "label": self.label, "value": self.value })
}
}
#[derive(Clone, Debug)]
pub struct Markdown {
id: String,
text: String,
}
impl Markdown {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
text: String::new(),
}
}
pub fn text(mut self, t: impl Into<String>) -> Self {
self.text = t.into();
self
}
pub fn value(mut self, t: impl Into<String>) -> Self {
self.text = t.into();
self
}
}
impl Component for Markdown {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"markdown"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({ "text": self.text })
}
}
#[derive(Clone, Debug)]
pub struct Progress {
id: String,
label: String,
variant: String,
size: Option<usize>,
}
impl Progress {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
variant: "bar".to_string(),
size: None,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn variant(mut self, v: impl Into<String>) -> Self {
self.variant = v.into();
self
}
pub fn bar(mut self) -> Self {
self.variant = "bar".to_string();
self
}
pub fn circle(mut self) -> Self {
self.variant = "circle".to_string();
self
}
pub fn pie(mut self) -> Self {
self.variant = "pie".to_string();
self
}
pub fn size(mut self, s: usize) -> Self {
self.size = Some(s);
self
}
}
impl Component for Progress {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"progress"
}
fn props(&self) -> Value {
json!({
"label": self.label,
"variant": self.variant,
"size": self.size
})
}
}
#[derive(Clone, Debug)]
pub struct Timer {
id: String,
label: String,
interval: f64,
running: bool,
}
impl Timer {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
interval: 1.0,
running: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn interval(mut self, s: f64) -> Self {
self.interval = s.max(0.05);
self
}
pub fn running(mut self, on: bool) -> Self {
self.running = on;
self
}
}
impl Component for Timer {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"timer"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({ "label": self.label, "interval": self.interval, "running": self.running })
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct ChatMessage {
pub role: String,
pub content: String,
}
impl ChatMessage {
pub fn user(content: impl Into<String>) -> Self {
Self {
role: "user".into(),
content: content.into(),
}
}
pub fn assistant(content: impl Into<String>) -> Self {
Self {
role: "assistant".into(),
content: content.into(),
}
}
pub fn system(content: impl Into<String>) -> Self {
Self {
role: "system".into(),
content: content.into(),
}
}
}
#[derive(Clone, Debug)]
pub struct Chatbot {
id: String,
label: String,
messages: Vec<ChatMessage>,
height: Option<u32>,
placeholder: String,
}
impl Chatbot {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
messages: Vec::new(),
height: Some(420),
placeholder: "Commencez la conversation...".into(),
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn messages(mut self, m: Vec<ChatMessage>) -> Self {
self.messages = m;
self
}
pub fn message(mut self, role: impl Into<String>, content: impl Into<String>) -> Self {
self.messages.push(ChatMessage {
role: role.into(),
content: content.into(),
});
self
}
pub fn height(mut self, h: u32) -> Self {
self.height = Some(h);
self
}
pub fn placeholder(mut self, p: impl Into<String>) -> Self {
self.placeholder = p.into();
self
}
}
impl Component for Chatbot {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"chatbot"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({
"label": self.label,
"value": self.messages,
"height": self.height,
"placeholder": self.placeholder
})
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct TextSegment {
pub text: String,
pub label: Option<String>,
}
#[derive(Clone, Debug)]
pub struct HighlightedText {
id: String,
label: String,
segments: Vec<TextSegment>,
color_map: Vec<(String, String)>,
show_legend: bool,
}
impl HighlightedText {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
segments: Vec::new(),
color_map: Vec::new(),
show_legend: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn segment(mut self, text: impl Into<String>, label: Option<&str>) -> Self {
self.segments.push(TextSegment {
text: text.into(),
label: label.map(|s| s.to_string()),
});
self
}
pub fn segments(mut self, segs: &[(&str, Option<&str>)]) -> Self {
self.segments = segs
.iter()
.map(|(t, l)| TextSegment {
text: t.to_string(),
label: l.map(|s| s.to_string()),
})
.collect();
self
}
pub fn data<T: serde::Serialize>(mut self, d: &T) -> Self {
if let Ok(v) = serde_json::to_value(d) {
if let Some(arr) = v.as_array() {
self.segments = arr
.iter()
.filter_map(|item| {
if let Some(pair) = item.as_array() {
let text = pair
.first()
.and_then(|x| x.as_str())
.unwrap_or("")
.to_string();
let label = pair.get(1).and_then(|x| x.as_str()).map(|s| s.to_string());
Some(TextSegment { text, label })
} else {
serde_json::from_value::<TextSegment>(item.clone()).ok()
}
})
.collect();
}
}
self
}
pub fn color_map(mut self, map: &[(&str, &str)]) -> Self {
self.color_map = map
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
self
}
pub fn show_legend(mut self, on: bool) -> Self {
self.show_legend = on;
self
}
}
impl Component for HighlightedText {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"highlightedtext"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
let colors: serde_json::Map<String, Value> = self
.color_map
.iter()
.map(|(k, v)| (k.clone(), Value::String(v.clone())))
.collect();
json!({
"label": self.label,
"segments": self.segments,
"color_map": colors,
"show_legend": self.show_legend
})
}
}
#[derive(Clone, Debug)]
pub struct CodeDiff {
id: String,
label: String,
old_code: String,
new_code: String,
language: String,
split_view: bool,
}
impl CodeDiff {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
old_code: String::new(),
new_code: String::new(),
language: "rust".into(),
split_view: false,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn old_code(mut self, s: impl Into<String>) -> Self {
self.old_code = s.into();
self
}
pub fn new_code(mut self, s: impl Into<String>) -> Self {
self.new_code = s.into();
self
}
pub fn language(mut self, lang: impl Into<String>) -> Self {
self.language = lang.into();
self
}
pub fn split_view(mut self, on: bool) -> Self {
self.split_view = on;
self
}
}
impl Component for CodeDiff {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"codediff"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({
"label": self.label,
"old_code": self.old_code,
"new_code": self.new_code,
"language": self.language,
"split_view": self.split_view
})
}
}
#[derive(Clone, Debug)]
pub struct Model3D {
id: String,
label: String,
value: String,
clear_color: String,
interactive: bool,
}
impl Model3D {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: String::new(),
clear_color: "#1e293b".into(),
interactive: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn value(mut self, v: impl Into<String>) -> Self {
self.value = v.into();
self
}
pub fn clear_color(mut self, hex: impl Into<String>) -> Self {
self.clear_color = hex.into();
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
}
impl Component for Model3D {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"model3d"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({
"label": self.label,
"value": self.value,
"clear_color": self.clear_color,
"interactive": self.interactive
})
}
}
#[derive(Clone, Debug)]
pub struct Html {
id: String,
label: String,
value: String,
out: bool,
}
impl Html {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: String::new(),
out: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn value(mut self, h: impl Into<String>) -> Self {
self.value = h.into();
self
}
pub fn input(mut self) -> Self {
self.out = false;
self
}
pub fn output(mut self) -> Self {
self.out = true;
self
}
}
impl Component for Html {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"html"
}
fn role(&self) -> Role {
if self.out {
Role::Output
} else {
Role::Input
}
}
fn props(&self) -> Value {
json!({
"label": self.label,
"value": self.value
})
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct MapMarker {
pub lat: f64,
pub lon: f64,
pub label: Option<String>,
pub color: Option<String>,
pub id: Option<String>,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct MapCircle {
pub lat: f64,
pub lon: f64,
pub radius: f64,
pub color: Option<String>,
}
#[derive(Clone, Debug)]
pub struct Map {
id: String,
label: String,
center_lat: f64,
center_lon: f64,
zoom: u8,
markers: Vec<MapMarker>,
circles: Vec<MapCircle>,
height: u32,
interactive: bool,
out: bool,
}
impl Map {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
center_lat: 48.8566,
center_lon: 2.3522,
zoom: 12,
markers: Vec::new(),
circles: Vec::new(),
height: 420,
interactive: true,
out: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn center(mut self, lat: f64, lon: f64) -> Self {
self.center_lat = lat;
self.center_lon = lon;
self
}
pub fn zoom(mut self, z: u8) -> Self {
self.zoom = z.clamp(1, 19);
self
}
pub fn marker(
mut self,
lat: f64,
lon: f64,
label: impl Into<String>,
color: Option<&str>,
) -> Self {
self.markers.push(MapMarker {
lat,
lon,
label: Some(label.into()),
color: color.map(|s| s.to_string()),
id: None,
});
self
}
pub fn marker_with_id(
mut self,
id: impl Into<String>,
lat: f64,
lon: f64,
label: impl Into<String>,
color: Option<&str>,
) -> Self {
self.markers.push(MapMarker {
lat,
lon,
label: Some(label.into()),
color: color.map(|s| s.to_string()),
id: Some(id.into()),
});
self
}
pub fn circle(mut self, lat: f64, lon: f64, radius: f64, color: Option<&str>) -> Self {
self.circles.push(MapCircle {
lat,
lon,
radius,
color: color.map(|s| s.to_string()),
});
self
}
pub fn height(mut self, px: u32) -> Self {
self.height = px;
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
pub fn input(mut self) -> Self {
self.out = false;
self
}
pub fn output(mut self) -> Self {
self.out = true;
self
}
}
impl Component for Map {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"map"
}
fn role(&self) -> Role {
if self.out {
Role::Output
} else {
Role::Input
}
}
fn props(&self) -> Value {
json!({
"label": self.label,
"center": [self.center_lat, self.center_lon],
"zoom": self.zoom,
"markers": self.markers,
"circles": self.circles,
"height": self.height,
"interactive": self.interactive,
})
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct NodeSocket {
pub id: String,
pub label: String,
pub data_type: String,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct GraphNode {
pub id: String,
pub title: String,
pub category: String,
pub x: f64,
pub y: f64,
pub inputs: Vec<NodeSocket>,
pub outputs: Vec<NodeSocket>,
pub status: String,
pub data: Value,
}
impl GraphNode {
pub fn new(
id: impl Into<String>,
title: impl Into<String>,
category: impl Into<String>,
) -> Self {
Self {
id: id.into(),
title: title.into(),
category: category.into(),
x: 50.0,
y: 50.0,
inputs: Vec::new(),
outputs: Vec::new(),
status: "idle".into(),
data: Value::Null,
}
}
pub fn pos(mut self, x: f64, y: f64) -> Self {
self.x = x;
self.y = y;
self
}
pub fn input(mut self, id: impl Into<String>, data_type: impl Into<String>) -> Self {
let sid = id.into();
self.inputs.push(NodeSocket {
label: sid.clone(),
id: sid,
data_type: data_type.into(),
});
self
}
pub fn output(mut self, id: impl Into<String>, data_type: impl Into<String>) -> Self {
let sid = id.into();
self.outputs.push(NodeSocket {
label: sid.clone(),
id: sid,
data_type: data_type.into(),
});
self
}
pub fn status(mut self, s: impl Into<String>) -> Self {
self.status = s.into();
self
}
pub fn data<T: serde::Serialize>(mut self, d: &T) -> Self {
self.data = serde_json::to_value(d).unwrap_or(Value::Null);
self
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct GraphEdge {
pub from_node: String,
pub from_socket: String,
pub to_node: String,
pub to_socket: String,
}
#[derive(Clone, Debug)]
pub struct NodeGraph {
id: String,
label: String,
nodes: Vec<GraphNode>,
edges: Vec<GraphEdge>,
height: u32,
interactive: bool,
out: bool,
}
impl NodeGraph {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
nodes: Vec::new(),
edges: Vec::new(),
height: 480,
interactive: true,
out: false,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn node(mut self, n: GraphNode) -> Self {
self.nodes.push(n);
self
}
pub fn nodes(mut self, nodes: Vec<GraphNode>) -> Self {
self.nodes = nodes;
self
}
pub fn edge(
mut self,
from_node: impl Into<String>,
from_socket: impl Into<String>,
to_node: impl Into<String>,
to_socket: impl Into<String>,
) -> Self {
self.edges.push(GraphEdge {
from_node: from_node.into(),
from_socket: from_socket.into(),
to_node: to_node.into(),
to_socket: to_socket.into(),
});
self
}
pub fn edges(mut self, edges: Vec<GraphEdge>) -> Self {
self.edges = edges;
self
}
pub fn height(mut self, h: u32) -> Self {
self.height = h;
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
pub fn output(mut self) -> Self {
self.out = true;
self
}
pub fn input(mut self) -> Self {
self.out = false;
self
}
}
impl Component for NodeGraph {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"nodegraph"
}
fn role(&self) -> Role {
if self.out {
Role::Output
} else {
Role::Input
}
}
fn props(&self) -> Value {
json!({
"label": self.label,
"nodes": self.nodes,
"edges": self.edges,
"height": self.height,
"interactive": self.interactive,
})
}
}