xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/rust/src/greeter.rs (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan // Copyright 2015 The Bazel Authors. All rights reserved.
2*d4726bddSHONG Yifan //
3*d4726bddSHONG Yifan // Licensed under the Apache License, Version 2.0 (the "License");
4*d4726bddSHONG Yifan // you may not use this file except in compliance with the License.
5*d4726bddSHONG Yifan // You may obtain a copy of the License at
6*d4726bddSHONG Yifan //
7*d4726bddSHONG Yifan //    http://www.apache.org/licenses/LICENSE-2.0
8*d4726bddSHONG Yifan //
9*d4726bddSHONG Yifan // Unless required by applicable law or agreed to in writing, software
10*d4726bddSHONG Yifan // distributed under the License is distributed on an "AS IS" BASIS,
11*d4726bddSHONG Yifan // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*d4726bddSHONG Yifan // See the License for the specific language governing permissions and
13*d4726bddSHONG Yifan // limitations under the License.
14*d4726bddSHONG Yifan 
15*d4726bddSHONG Yifan /// Object that displays a greeting.
16*d4726bddSHONG Yifan pub struct Greeter {
17*d4726bddSHONG Yifan     greeting: String,
18*d4726bddSHONG Yifan }
19*d4726bddSHONG Yifan 
20*d4726bddSHONG Yifan /// Implementation of Greeter.
21*d4726bddSHONG Yifan impl Greeter {
22*d4726bddSHONG Yifan     /// Constructs a new `Greeter`.
23*d4726bddSHONG Yifan     ///
24*d4726bddSHONG Yifan     /// # Examples
25*d4726bddSHONG Yifan     ///
26*d4726bddSHONG Yifan     /// ```
27*d4726bddSHONG Yifan     /// use hello_lib::greeter::Greeter;
28*d4726bddSHONG Yifan     ///
29*d4726bddSHONG Yifan     /// let greeter = Greeter::new("Hello");
30*d4726bddSHONG Yifan     /// ```
new(greeting: &str) -> Greeter31*d4726bddSHONG Yifan     pub fn new(greeting: &str) -> Greeter {
32*d4726bddSHONG Yifan         Greeter {
33*d4726bddSHONG Yifan             greeting: greeting.to_string(),
34*d4726bddSHONG Yifan         }
35*d4726bddSHONG Yifan     }
36*d4726bddSHONG Yifan 
37*d4726bddSHONG Yifan     /// Constructs a new `Greeter` with greeting defined in txt file
38*d4726bddSHONG Yifan     ///
39*d4726bddSHONG Yifan     /// # Examples
40*d4726bddSHONG Yifan     ///
41*d4726bddSHONG Yifan     /// ```
42*d4726bddSHONG Yifan     /// use hello_lib::greeter::Greeter;
43*d4726bddSHONG Yifan     ///
44*d4726bddSHONG Yifan     /// let greeter = Greeter::from_txt_file()?;
45*d4726bddSHONG Yifan     /// ```
from_txt_file() -> std::io::Result<Greeter>46*d4726bddSHONG Yifan     pub fn from_txt_file() -> std::io::Result<Greeter> {
47*d4726bddSHONG Yifan         let r = runfiles::Runfiles::create()?;
48*d4726bddSHONG Yifan         Ok(Greeter {
49*d4726bddSHONG Yifan             greeting: std::fs::read_to_string(runfiles::rlocation!(
50*d4726bddSHONG Yifan                 r,
51*d4726bddSHONG Yifan                 "rules_rust/test/rust/greeting.txt"
52*d4726bddSHONG Yifan             ))?,
53*d4726bddSHONG Yifan         })
54*d4726bddSHONG Yifan     }
55*d4726bddSHONG Yifan 
56*d4726bddSHONG Yifan     /// Returns the greeting as a string.
57*d4726bddSHONG Yifan     ///
58*d4726bddSHONG Yifan     /// # Examples
59*d4726bddSHONG Yifan     ///
60*d4726bddSHONG Yifan     /// ```
61*d4726bddSHONG Yifan     /// use hello_lib::greeter::Greeter;
62*d4726bddSHONG Yifan     ///
63*d4726bddSHONG Yifan     /// let greeter = Greeter::new("Hello");
64*d4726bddSHONG Yifan     /// let greeting = greeter.greeting("World");
65*d4726bddSHONG Yifan     /// ```
greeting(&self, thing: &str) -> String66*d4726bddSHONG Yifan     pub fn greeting(&self, thing: &str) -> String {
67*d4726bddSHONG Yifan         format!("{} {}", &self.greeting, thing)
68*d4726bddSHONG Yifan     }
69*d4726bddSHONG Yifan 
70*d4726bddSHONG Yifan     /// Prints the greeting.
71*d4726bddSHONG Yifan     ///
72*d4726bddSHONG Yifan     /// # Examples
73*d4726bddSHONG Yifan     ///
74*d4726bddSHONG Yifan     /// ```
75*d4726bddSHONG Yifan     /// use hello_lib::greeter::Greeter;
76*d4726bddSHONG Yifan     ///
77*d4726bddSHONG Yifan     /// let greeter = Greeter::new("Hello");
78*d4726bddSHONG Yifan     /// greeter.greet("World");
79*d4726bddSHONG Yifan     /// ```
greet(&self, thing: &str)80*d4726bddSHONG Yifan     pub fn greet(&self, thing: &str) {
81*d4726bddSHONG Yifan         println!("{} {}", &self.greeting, thing);
82*d4726bddSHONG Yifan     }
83*d4726bddSHONG Yifan }
84*d4726bddSHONG Yifan 
85*d4726bddSHONG Yifan #[cfg(test)]
86*d4726bddSHONG Yifan mod test {
87*d4726bddSHONG Yifan     use super::Greeter;
88*d4726bddSHONG Yifan 
89*d4726bddSHONG Yifan     #[test]
test_greeting()90*d4726bddSHONG Yifan     fn test_greeting() {
91*d4726bddSHONG Yifan         let hello = Greeter::new("Hi");
92*d4726bddSHONG Yifan         assert_eq!("Hi Rust", hello.greeting("Rust"));
93*d4726bddSHONG Yifan     }
94*d4726bddSHONG Yifan 
95*d4726bddSHONG Yifan     #[test]
test_greeting_from_txt_file()96*d4726bddSHONG Yifan     fn test_greeting_from_txt_file() {
97*d4726bddSHONG Yifan         let welcome = Greeter::from_txt_file().unwrap();
98*d4726bddSHONG Yifan         assert_eq!("Welcome Rust", welcome.greeting("Rust"));
99*d4726bddSHONG Yifan     }
100*d4726bddSHONG Yifan }
101