plux_rs/function/arg.rs
1use std::fmt::Display;
2
3use serde::{Deserialize, Serialize};
4
5use crate::variable::VariableType;
6
7/// Represents a function argument with name and type.
8///
9/// An Arg describes a single parameter or return value of a function,
10/// including both its name and data type.
11///
12/// # Fields
13///
14/// * `name` - The argument name (used for documentation and debugging)
15/// * `ty` - The data type of the argument
16///
17/// # Examples
18///
19/// ```rust
20/// use plux_rs::function::Arg;
21/// use plux_rs::variable::VariableType;
22///
23/// // Create input arguments
24/// let x_arg = Arg::new("x", VariableType::F64);
25/// let y_arg = Arg::new("y", VariableType::F64);
26///
27/// // Create output argument
28/// let result_arg = Arg::new("result", VariableType::F64);
29/// ```
30#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Serialize, Deserialize)]
31pub struct Arg {
32 /// The name of the argument
33 pub name: String,
34 /// The data type of the argument
35 pub ty: VariableType,
36}
37
38impl Arg {
39 /// Creates a new argument with the given name and type.
40 ///
41 /// # Parameters
42 ///
43 /// * `name` - The argument name (will be converted to String)
44 /// * `ty` - The data type of the argument
45 ///
46 /// # Returns
47 ///
48 /// Returns a new Arg instance.
49 ///
50 /// # Type Parameters
51 ///
52 /// * `S` - Type that can be converted into String
53 ///
54 /// # Example
55 ///
56 /// ```rust
57 /// use plux_rs::function::Arg;
58 /// use plux_rs::variable::VariableType;
59 ///
60 /// let arg = Arg::new("count", VariableType::I32);
61 /// assert_eq!(arg.name, "count");
62 /// assert_eq!(arg.ty, VariableType::I32);
63 /// ```
64 pub fn new<S: Into<String>>(name: S, ty: VariableType) -> Self {
65 Self {
66 name: name.into(),
67 ty,
68 }
69 }
70}
71
72impl Default for Arg {
73 fn default() -> Self {
74 Self {
75 name: "arg".to_string(),
76 ty: Default::default(),
77 }
78 }
79}
80
81impl Display for Arg {
82 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83 write!(f, "{}: {}", self.name, self.ty)
84 }
85}