use serde_json::{json, Value};
use super::{Component, Role};
#[derive(Clone, Debug)]
pub struct Image {
id: String,
label: String,
value: String,
interactive: bool,
out: bool,
}
impl Image {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: String::new(),
interactive: true,
out: false,
}
}
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 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 Image {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"image"
}
fn role(&self) -> Role {
if self.out {
Role::Output
} else {
Role::Input
}
}
fn props(&self) -> Value {
json!({ "label": self.label, "value": self.value, "interactive": self.interactive })
}
}
#[derive(Clone, Debug)]
pub struct ImageEditor {
id: String,
label: String,
value: String,
interactive: bool,
layers: usize,
brush: bool,
crop: bool,
shapes: bool,
filters: bool,
rotflip: bool,
out: bool,
}
impl ImageEditor {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: String::new(),
interactive: true,
layers: 2,
brush: true,
crop: true,
shapes: true,
filters: true,
rotflip: true,
out: false,
}
}
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 interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
pub fn layers(mut self, n: usize) -> Self {
self.layers = n.clamp(1, 4);
self
}
pub fn brush(mut self, on: bool) -> Self {
self.brush = on;
self
}
pub fn crop(mut self, on: bool) -> Self {
self.crop = on;
self
}
pub fn shapes(mut self, on: bool) -> Self {
self.shapes = on;
self
}
pub fn filters(mut self, on: bool) -> Self {
self.filters = on;
self
}
pub fn rotflip(mut self, on: bool) -> Self {
self.rotflip = 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 ImageEditor {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"imageeditor"
}
fn role(&self) -> Role {
if self.out {
Role::Output
} else {
Role::Input
}
}
fn props(&self) -> Value {
json!({
"label": self.label, "value": self.value, "interactive": self.interactive,
"layers": self.layers, "brush": self.brush, "crop": self.crop,
"shapes": self.shapes, "filters": self.filters, "rotflip": self.rotflip
})
}
}
#[derive(Clone, Debug)]
pub struct Audio {
id: String,
label: String,
value: String,
interactive: bool,
out: bool,
live: bool,
}
impl Audio {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: String::new(),
interactive: true,
out: false,
live: false,
}
}
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 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
}
pub fn live(mut self, on: bool) -> Self {
self.live = on;
self
}
}
impl Component for Audio {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"audio"
}
fn role(&self) -> Role {
if self.out {
Role::Output
} else {
Role::Input
}
}
fn props(&self) -> Value {
json!({ "label": self.label, "value": self.value, "interactive": self.interactive, "live": self.live })
}
}
#[derive(Clone, Debug)]
pub struct Video {
id: String,
label: String,
value: String,
interactive: bool,
out: bool,
live: bool,
}
impl Video {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: String::new(),
interactive: false,
out: true,
live: false,
}
}
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 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
}
pub fn live(mut self, on: bool) -> Self {
self.live = on;
self
}
}
impl Component for Video {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"video"
}
fn role(&self) -> Role {
if self.out {
Role::Output
} else {
Role::Input
}
}
fn props(&self) -> Value {
json!({ "label": self.label, "value": self.value, "interactive": self.interactive, "live": self.live })
}
}
#[derive(Clone, Debug)]
pub struct Gallery {
id: String,
label: String,
value: Value,
columns: usize,
rows: Option<usize>,
height: Option<String>,
min_height: Option<String>,
max_height: Option<String>,
limit: Option<usize>,
item_height: Option<String>,
item_width: Option<String>,
aspect_ratio: Option<String>,
object_fit: String,
allow_preview: bool,
interactive: bool,
upload: bool,
out: bool,
}
impl Gallery {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: json!([]),
columns: 3,
rows: None,
height: None,
min_height: None,
max_height: None,
limit: None,
item_height: None,
item_width: None,
aspect_ratio: None,
object_fit: "cover".into(),
allow_preview: true,
interactive: true,
upload: true,
out: false,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn title(mut self, t: impl Into<String>) -> Self {
self.label = t.into();
self
}
pub fn value(mut self, items: &[&str]) -> Self {
let v: Vec<String> = items.iter().map(|s| s.to_string()).collect();
self.value = json!(v);
self
}
pub fn items(mut self, items: &[impl AsRef<str>]) -> Self {
let v: Vec<String> = items.iter().map(|s| s.as_ref().to_string()).collect();
self.value = json!(v);
self
}
pub fn raw_items(mut self, items: impl serde::Serialize) -> Self {
self.value = serde_json::to_value(items).unwrap_or_else(|_| json!([]));
self
}
pub fn columns(mut self, n: usize) -> Self {
self.columns = n.max(1);
self
}
pub fn rows(mut self, n: usize) -> Self {
self.rows = Some(n.max(1));
self
}
pub fn height(mut self, h: impl Into<String>) -> Self {
self.height = Some(h.into());
self
}
pub fn min_height(mut self, mh: impl Into<String>) -> Self {
self.min_height = Some(mh.into());
self
}
pub fn max_height(mut self, mh: impl Into<String>) -> Self {
self.max_height = Some(mh.into());
self
}
pub fn limit(mut self, n: usize) -> Self {
self.limit = Some(n);
self
}
pub fn item_height(mut self, ih: impl Into<String>) -> Self {
self.item_height = Some(ih.into());
self
}
pub fn item_width(mut self, iw: impl Into<String>) -> Self {
self.item_width = Some(iw.into());
self
}
pub fn aspect_ratio(mut self, ar: impl Into<String>) -> Self {
self.aspect_ratio = Some(ar.into());
self
}
pub fn object_fit(mut self, fit: impl Into<String>) -> Self {
self.object_fit = fit.into();
self
}
pub fn allow_preview(mut self, allow: bool) -> Self {
self.allow_preview = allow;
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
pub fn upload(mut self, on: bool) -> Self {
self.upload = 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 Gallery {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"gallery"
}
fn role(&self) -> Role {
if self.out {
Role::Output
} else {
Role::Input
}
}
fn props(&self) -> Value {
json!({
"label": self.label,
"value": self.value,
"columns": self.columns,
"rows": self.rows,
"height": self.height,
"min_height": self.min_height,
"max_height": self.max_height,
"limit": self.limit,
"item_height": self.item_height,
"item_width": self.item_width,
"aspect_ratio": self.aspect_ratio,
"object_fit": self.object_fit,
"allow_preview": self.allow_preview,
"interactive": self.interactive,
"upload": self.upload
})
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct BoundingBox {
pub box_coords: [f64; 4],
pub label: String,
pub score: Option<f64>,
pub color: String,
}
#[derive(Clone, Debug)]
pub struct AnnotatedImage {
id: String,
label: String,
image: String,
boxes: Vec<BoundingBox>,
show_labels: bool,
show_scores: bool,
}
impl AnnotatedImage {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
image: String::new(),
boxes: Vec::new(),
show_labels: true,
show_scores: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn image(mut self, img: impl Into<String>) -> Self {
self.image = img.into();
self
}
#[allow(clippy::too_many_arguments)]
pub fn box_norm(
mut self,
ymin: f64,
xmin: f64,
ymax: f64,
xmax: f64,
label: impl Into<String>,
score: Option<f64>,
color: impl Into<String>,
) -> Self {
self.boxes.push(BoundingBox {
box_coords: [ymin, xmin, ymax, xmax],
label: label.into(),
score,
color: color.into(),
});
self
}
pub fn boxes(mut self, b: Vec<BoundingBox>) -> Self {
self.boxes = b;
self
}
pub fn data<T: serde::Serialize>(mut self, d: &T) -> Self {
if let Ok(v) = serde_json::to_value(d) {
if let Some(img) = v.get("image").and_then(|x| x.as_str()) {
self.image = img.to_string();
}
if let Some(arr) = v.get("boxes").and_then(|x| x.as_array()) {
if let Ok(b) = serde_json::from_value::<Vec<BoundingBox>>(Value::Array(arr.clone()))
{
self.boxes = b;
}
}
}
self
}
pub fn show_labels(mut self, on: bool) -> Self {
self.show_labels = on;
self
}
pub fn show_scores(mut self, on: bool) -> Self {
self.show_scores = on;
self
}
}
impl Component for AnnotatedImage {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"annotatedimage"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({
"label": self.label,
"image": self.image,
"boxes": self.boxes,
"show_labels": self.show_labels,
"show_scores": self.show_scores
})
}
}
#[derive(Clone, Debug)]
pub struct ImageComparison {
id: String,
label: String,
before: String,
after: String,
before_label: String,
after_label: String,
position: f64,
}
impl ImageComparison {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
before: String::new(),
after: String::new(),
before_label: "Before".into(),
after_label: "After".into(),
position: 50.0,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn before(mut self, img: impl Into<String>, label: impl Into<String>) -> Self {
self.before = img.into();
self.before_label = label.into();
self
}
pub fn after(mut self, img: impl Into<String>, label: impl Into<String>) -> Self {
self.after = img.into();
self.after_label = label.into();
self
}
pub fn position(mut self, pos: f64) -> Self {
self.position = pos.clamp(0.0, 100.0);
self
}
}
impl Component for ImageComparison {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"imagecomparison"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({
"label": self.label,
"before": self.before,
"after": self.after,
"before_label": self.before_label,
"after_label": self.after_label,
"position": self.position
})
}
}
#[derive(Clone, Debug)]
pub struct AudioRecorder {
id: String,
label: String,
max_duration: f64,
interactive: bool,
}
impl AudioRecorder {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
max_duration: 60.0,
interactive: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn max_duration(mut self, d: f64) -> Self {
self.max_duration = d;
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
}
impl Component for AudioRecorder {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"audiorecorder"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
json!({
"label": self.label,
"max_duration": self.max_duration,
"interactive": self.interactive
})
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct PdfHighlight {
pub page: u32,
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
pub label: Option<String>,
pub color: Option<String>,
}
#[derive(Clone, Debug)]
pub struct Pdf {
id: String,
label: String,
src: String,
page: u32,
total_pages: Option<u32>,
zoom: f64,
highlights: Vec<PdfHighlight>,
show_toolbar: bool,
interactive: bool,
}
impl Pdf {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
src: String::new(),
page: 1,
total_pages: None,
zoom: 1.0,
highlights: Vec::new(),
show_toolbar: true,
interactive: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn src(mut self, s: impl Into<String>) -> Self {
self.src = s.into();
self
}
pub fn page(mut self, p: u32) -> Self {
self.page = p.max(1);
self
}
pub fn total_pages(mut self, total: u32) -> Self {
self.total_pages = Some(total);
self
}
pub fn zoom(mut self, z: f64) -> Self {
self.zoom = z.clamp(0.2, 4.0);
self
}
#[allow(clippy::too_many_arguments)]
pub fn highlight(
mut self,
page: u32,
x: f64,
y: f64,
width: f64,
height: f64,
label: impl Into<String>,
color: impl Into<String>,
) -> Self {
self.highlights.push(PdfHighlight {
page,
x,
y,
width,
height,
label: Some(label.into()),
color: Some(color.into()),
});
self
}
pub fn highlights(mut self, hl: Vec<PdfHighlight>) -> Self {
self.highlights = hl;
self
}
pub fn show_toolbar(mut self, on: bool) -> Self {
self.show_toolbar = on;
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
}
impl Component for Pdf {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"pdf"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({
"label": self.label,
"src": self.src,
"page": self.page,
"total_pages": self.total_pages,
"zoom": self.zoom,
"highlights": self.highlights,
"show_toolbar": self.show_toolbar,
"interactive": self.interactive,
})
}
}
#[derive(Clone, Debug)]
pub struct AudioStream {
id: String,
label: String,
sample_rate: u32,
vad: bool,
show_waveform: bool,
interactive: bool,
out: bool,
autoplay: bool,
}
impl AudioStream {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
sample_rate: 16000,
vad: true,
show_waveform: true,
interactive: true,
out: false,
autoplay: true,
}
}
pub fn label(mut self, label: impl Into<String>) -> Self {
self.label = label.into();
self
}
pub fn sample_rate(mut self, hz: u32) -> Self {
self.sample_rate = hz;
self
}
pub fn vad(mut self, on: bool) -> Self {
self.vad = on;
self
}
pub fn show_waveform(mut self, on: bool) -> Self {
self.show_waveform = on;
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
}
pub fn autoplay(mut self, on: bool) -> Self {
self.autoplay = on;
self
}
}
impl Component for AudioStream {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"audiostream"
}
fn role(&self) -> Role {
if self.out {
Role::Output
} else {
Role::Input
}
}
fn props(&self) -> Value {
json!({
"label": self.label,
"sample_rate": self.sample_rate,
"vad": self.vad,
"show_waveform": self.show_waveform,
"interactive": self.interactive,
"autoplay": self.autoplay,
})
}
}
#[derive(Clone, Debug)]
pub struct CameraStream {
id: String,
label: String,
fps: u32,
mirror: bool,
show_telemetry: bool,
facing_mode: String,
interactive: bool,
out: bool,
}
impl CameraStream {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
fps: 30,
mirror: true,
show_telemetry: true,
facing_mode: "user".to_string(),
interactive: true,
out: false,
}
}
pub fn label(mut self, label: impl Into<String>) -> Self {
self.label = label.into();
self
}
pub fn fps(mut self, fps: u32) -> Self {
self.fps = fps.clamp(1, 120);
self
}
pub fn mirror(mut self, on: bool) -> Self {
self.mirror = on;
self
}
pub fn show_telemetry(mut self, on: bool) -> Self {
self.show_telemetry = on;
self
}
pub fn user_camera(mut self) -> Self {
self.facing_mode = "user".to_string();
self.mirror = true;
self
}
pub fn environment_camera(mut self) -> Self {
self.facing_mode = "environment".to_string();
self.mirror = false;
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 CameraStream {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"camerastream"
}
fn role(&self) -> Role {
if self.out {
Role::Output
} else {
Role::Input
}
}
fn props(&self) -> Value {
json!({
"label": self.label,
"fps": self.fps,
"mirror": self.mirror,
"show_telemetry": self.show_telemetry,
"facing_mode": self.facing_mode,
"interactive": self.interactive,
})
}
}