rstgen/csharp/
argument.rs

1//! Data structure for constructors
2
3use super::modifier::Modifier;
4use cons::Cons;
5use csharp::Csharp;
6use into_tokens::IntoTokens;
7use tokens::Tokens;
8
9/// Model for C# Arguments to functions.
10#[derive(Debug, Clone)]
11pub struct Argument<'el> {
12    /// Modifiers for argument.
13    pub modifiers: Vec<Modifier>,
14    /// Attributes to argument.
15    attributes: Tokens<'el, Csharp<'el>>,
16    /// Type of argument.
17    ty: Csharp<'el>,
18    /// Name of argument.
19    name: Cons<'el>,
20}
21
22impl<'el> Argument<'el> {
23    /// Build a new empty argument.
24    pub fn new<T, N>(ty: T, name: N) -> Argument<'el>
25    where
26        T: Into<Csharp<'el>>,
27        N: Into<Cons<'el>>,
28    {
29        Argument {
30            attributes: Tokens::new(),
31            modifiers: vec![],
32            ty: ty.into(),
33            name: name.into(),
34        }
35    }
36
37    /// Push an attribute.
38    pub fn attribute<T>(&mut self, attribute: T)
39    where
40        T: IntoTokens<'el, Csharp<'el>>,
41    {
42        self.attributes.push(attribute.into_tokens());
43    }
44
45    /// Get the variable of the argument.
46    pub fn var(&self) -> Cons<'el> {
47        self.name.clone()
48    }
49
50    /// The type of the argument.
51    pub fn ty(&self) -> Csharp<'el> {
52        self.ty.clone()
53    }
54}
55
56into_tokens_impl_from!(Argument<'el>, Csharp<'el>);
57
58impl<'el> IntoTokens<'el, Csharp<'el>> for Argument<'el> {
59    fn into_tokens(self) -> Tokens<'el, Csharp<'el>> {
60        let mut s = Tokens::new();
61
62        s.extend(self.attributes.into_iter());
63        s.extend(self.modifiers.into_tokens());
64        s.append(self.ty);
65        s.append(self.name);
66
67        s.join_spacing()
68    }
69}