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