plux_rs/function/request.rs
1use std::fmt::Display;
2
3use serde::{Deserialize, Serialize};
4
5use crate::variable::VariableType;
6
7/// Represents a function request that plugins must implement.
8///
9/// A Request defines the interface that plugins are expected to provide.
10/// It specifies the function signature that plugins should implement,
11/// including input and output types.
12///
13/// # Fields
14///
15/// * `name` - The name of the requested function
16/// * `inputs` - List of input parameter types
17/// * `output` - Optional output type (None for void functions)
18///
19/// # Examples
20///
21/// ```rust
22/// use plux_rs::function::Request;
23/// use plux_rs::variable::VariableType;
24///
25/// // Request a function that takes two integers and returns their sum
26/// let add_request = Request::new(
27/// "add",
28/// vec![VariableType::I32, VariableType::I32],
29/// Some(VariableType::I32)
30/// );
31///
32/// // Request a logging function that takes a string and returns nothing
33/// let log_request = Request::new(
34/// "log",
35/// vec![VariableType::String],
36/// None
37/// );
38/// ```
39#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct Request {
41 /// The name of the requested function
42 pub name: String,
43 /// List of input parameter types
44 pub inputs: Vec<VariableType>,
45 /// Optional output type (None for void functions)
46 pub output: Option<VariableType>,
47}
48
49impl Request {
50 /// Creates a new function request.
51 ///
52 /// # Parameters
53 ///
54 /// * `name` - The function name (will be converted to String)
55 /// * `inputs` - Vector of input parameter types
56 /// * `output` - Optional output type (None for void functions)
57 ///
58 /// # Returns
59 ///
60 /// Returns a new Request instance.
61 ///
62 /// # Type Parameters
63 ///
64 /// * `S` - Type that can be converted into String
65 ///
66 /// # Example
67 ///
68 /// ```rust
69 /// use plux_rs::function::Request;
70 /// use plux_rs::variable::VariableType;
71 ///
72 /// let request = Request::new(
73 /// "calculate_area",
74 /// vec![VariableType::F64, VariableType::F64], // width, height
75 /// Some(VariableType::F64) // area
76 /// );
77 /// ```
78 pub fn new<S: Into<String>>(
79 name: S,
80 inputs: Vec<VariableType>,
81 output: Option<VariableType>,
82 ) -> Self {
83 Self {
84 name: name.into(),
85 inputs,
86 output,
87 }
88 }
89}
90
91impl Display for Request {
92 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93 write!(
94 f,
95 "{}({}) -> {}",
96 self.name,
97 self.inputs
98 .iter()
99 .map(|x| format!("{x}"))
100 .collect::<Vec<_>>()
101 .join(", "),
102 match self.output {
103 Some(x) => format!("{x}"),
104 None => "void".to_string(),
105 }
106 )
107 }
108}