source_text/
ownedsource.rs1use crate::Source;
2
3#[derive(Debug)]
5pub struct OwnedSource {
6 name: Option<String>,
7 text: String,
8}
9
10impl OwnedSource {
11 pub fn new<N, T>(optname: Option<N>, text: T) -> Self
13 where
14 String: From<N>,
15 String: From<T>,
16 {
17 OwnedSource {
18 name: optname.map(|n| String::from(n)),
19 text: String::from(text),
20 }
21 }
22
23 pub fn new_named<N, T>(name: N, text: T) -> Self
25 where
26 String: From<N>,
27 String: From<T>,
28 {
29 OwnedSource::new(Some(name), text)
30 }
31
32 pub fn new_unnamed<T>(text: T) -> Self
34 where
35 String: From<T>,
36 {
37 OwnedSource::new(None, text)
38 }
39
40 pub fn name(&self) -> &str {
42 use crate::optname_to_str;
43
44 optname_to_str(self.name.as_ref().map(|s| s.as_ref()))
45 }
46
47 pub fn text(&self) -> &str {
49 self.text.as_ref()
50 }
51
52 pub fn source(&self) -> Source {
54 Source::new(self.name.as_ref(), self.text())
55 }
56
57 pub fn unwrap(self) -> (Option<String>, String) {
59 (self.name, self.text)
60 }
61}