comfy_print/
message.rs

1//! # Message
2//! [comfy_print](crate)'s data type for storing messages that failed to be printed.
3
4use std::fmt::{Display, Formatter};
5use std::ops::Deref;
6
7/// Which stream to write to.
8/// - [Stdout](OutputKind::Stdout) write to [std::io::stdout()](std::io::stdout())
9/// - [Stderr](OutputKind::Stderr) write to [std::io::stderr()](std::io::stderr())
10#[derive(Debug, Copy, Clone, PartialEq, Eq)]
11pub enum OutputKind {
12	/// Write to [std::io::stdout()](std::io::stdout())
13	Stdout,
14	/// Write to [std::io::stderr()](std::io::stderr())
15	Stderr,
16}
17
18/// Structure for storing messages that failed to be printed.
19pub struct Message {
20	string: String,
21	output: OutputKind,
22	should_append_line: bool,
23}
24
25impl Message {
26	pub fn str(&self) -> &str {
27		return self.string.deref();
28	}
29	
30	pub fn output_kind(&self) -> OutputKind {
31		return self.output;
32	}
33
34	pub fn standard(print_me: impl Into<String>) -> Self {
35		return Self {
36			string: print_me.into(),
37			output: OutputKind::Stdout,
38			should_append_line: false,
39		};
40	}
41	
42	pub fn standard_ln(print_me: impl Into<String>) -> Self {
43		return Self {
44			string: print_me.into(),
45			output: OutputKind::Stdout,
46			should_append_line: true,
47		};
48	}
49	
50	pub fn error(print_me: impl Into<String>) -> Self {
51		return Self {
52			string: print_me.into(),
53			output: OutputKind::Stderr,
54			should_append_line: false,
55		};
56	}
57
58	pub fn error_ln(print_me: impl Into<String>) -> Self {
59		return Self {
60			string: print_me.into(),
61			output: OutputKind::Stderr,
62			should_append_line: true,
63		};
64	}
65}
66
67impl Display for Message {
68	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
69		if self.should_append_line {
70			return write!(f, "{}\n", self.string.deref());
71		} else {
72			return write!(f, "{}", self.string.deref());
73		}
74	}
75}