1#[cfg(feature = "std")]
2use crate::grid::config::{HorizontalLine, VerticalLine};
3
4/// The structure represent a vertical or horizontal line.
5#[derive(Debug, Default, Clone, Copy)]
6pub struct Line {
7 pub(crate) main: Option<char>,
8 pub(crate) intersection: Option<char>,
9 pub(crate) connector1: Option<char>,
10 pub(crate) connector2: Option<char>,
11}
12
13impl Line {
14 /// Creates a new [`Line`] object.
15 pub const fn new(
16 main: Option<char>,
17 intersection: Option<char>,
18 connector1: Option<char>,
19 connector2: Option<char>,
20 ) -> Self {
21 Self {
22 main,
23 intersection,
24 connector1,
25 connector2,
26 }
27 }
28
29 /// Creates a new [`Line`] object with all chars set.
30 pub const fn full(main: char, intersection: char, connector1: char, connector2: char) -> Self {
31 Self::new(
32 Some(main),
33 Some(intersection),
34 Some(connector1),
35 Some(connector2),
36 )
37 }
38
39 /// Creates a new [`Line`] object with all chars set to the provided one.
40 pub const fn filled(c: char) -> Self {
41 Self::full(c, c, c, c)
42 }
43
44 /// Creates a new [`Line`] object with all chars not set.
45 pub const fn empty() -> Self {
46 Self::new(None, None, None, None)
47 }
48
49 /// Checks if the line has nothing set.
50 pub const fn is_empty(&self) -> bool {
51 self.main.is_none()
52 && self.intersection.is_none()
53 && self.connector1.is_none()
54 && self.connector2.is_none()
55 }
56}
57
58#[cfg(feature = "std")]
59impl From<Line> for HorizontalLine {
60 fn from(l: Line) -> Self {
61 Self {
62 main: l.main,
63 intersection: l.intersection,
64 left: l.connector1,
65 right: l.connector2,
66 }
67 }
68}
69
70#[cfg(feature = "std")]
71impl From<Line> for VerticalLine {
72 fn from(l: Line) -> Self {
73 Self {
74 main: l.main,
75 intersection: l.intersection,
76 top: l.connector1,
77 bottom: l.connector2,
78 }
79 }
80}
81
82impl From<Line> for papergrid::config::Line<char> {
83 fn from(l: Line) -> Self {
84 Self {
85 main: l.main.unwrap_or(default:' '),
86 intersection: l.intersection,
87 connect1: l.connector1,
88 connect2: l.connector2,
89 }
90 }
91}
92