feed/channel/text_input_builder.rs
1// This file is part of feed.
2//
3// Copyright © 2015-2017 Chris Palmer <pennstate5013@gmail.com>
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation; either version 3 of the License, or
8// (at your option) any later version.
9
10
11//! The fields can be set for text input by using the methods
12//! under `TextInputBuilder`.
13
14
15use TextInputBuilder;
16use rss::TextInput;
17use utils::string_utils;
18
19
20impl TextInputBuilder
21{
22 /// Construct a new `TextInputBuilder` and return default values.
23 ///
24 /// # Examples
25 ///
26 /// ```
27 /// use feed::TextInputBuilder;
28 ///
29 /// let text_input_builder = TextInputBuilder::new();
30 /// ```
31 pub fn new() -> TextInputBuilder
32 {
33 TextInputBuilder::default()
34 }
35
36
37 /// Set the title that exists under `TextInput`.
38 ///
39 /// # Examples
40 ///
41 /// ```
42 /// use feed::TextInputBuilder;
43 ///
44 /// let mut text_input_builder = TextInputBuilder::new();
45 /// text_input_builder.title("Title");
46 /// ```
47 pub fn title(&mut self, title: &str) -> &mut TextInputBuilder
48 {
49 self.title = title.to_owned();
50 self
51 }
52
53
54 /// Set the description that exists under `TextInput`.
55 ///
56 /// # Examples
57 ///
58 /// ```
59 /// use feed::TextInputBuilder;
60 ///
61 /// let mut text_input_builder = TextInputBuilder::new();
62 /// text_input_builder.description("This is a test description.");
63 /// ```
64 pub fn description(&mut self, description: &str) -> &mut TextInputBuilder
65 {
66 self.description = description.to_owned();
67 self
68 }
69
70
71 /// Set the name that exists under `TextInput`.
72 ///
73 /// # Examples
74 ///
75 /// ```
76 /// use feed::TextInputBuilder;
77 ///
78 /// let mut text_input_builder = TextInputBuilder::new();
79 /// text_input_builder.name("Comments");
80 /// ```
81 pub fn name(&mut self, name: &str) -> &mut TextInputBuilder
82 {
83 self.name = name.to_owned();
84 self
85 }
86
87
88 /// Set the link that exists under `TextInput`.
89 ///
90 /// # Examples
91 ///
92 /// ```
93 /// use feed::TextInputBuilder;
94 ///
95 /// let mut text_input_builder = TextInputBuilder::new();
96 /// text_input_builder.link("http://www.example.com/feedback");
97 /// ```
98 pub fn link(&mut self, link: &str) -> &mut TextInputBuilder
99 {
100 self.link = link.to_owned();
101 self
102 }
103
104
105 /// Validate the contents of `TextInput`.
106 ///
107 /// # Examples
108 ///
109 /// ```
110 /// use feed::TextInputBuilder;
111 ///
112 /// let text_input = TextInputBuilder::new()
113 /// .title("Title")
114 /// .description("This is a test description.")
115 /// .name("Comments")
116 /// .link("http://www.example.com/feedback")
117 /// .validate().unwrap()
118 /// .finalize().unwrap();
119 /// ```
120 pub fn validate(&mut self) -> Result<&mut TextInputBuilder, String>
121 {
122 string_utils::str_to_url(self.link.clone().as_str())?;
123
124 Ok(self)
125 }
126
127
128 /// Construct the `TextInput` from the `TextInputBuilder`.
129 ///
130 /// # Examples
131 ///
132 /// ```
133 /// use feed::TextInputBuilder;
134 ///
135 /// let text_input = TextInputBuilder::new()
136 /// .title("Title")
137 /// .description("This is a test description.")
138 /// .name("Comments")
139 /// .link("http://www.example.com/feedback")
140 /// .finalize()
141 /// .unwrap();
142 /// ```
143 pub fn finalize(&self) -> Result<TextInput, String>
144 {
145 Ok(TextInput {
146 title: self.title.clone(),
147 description: self.description.clone(),
148 name: self.name.clone(),
149 link: self.link.clone(),
150 })
151 }
152}