sdml_core/model/
comments.rs1use crate::model::{HasSourceSpan, Span};
6use std::fmt::Display;
7
8#[cfg(feature = "serde")]
9use serde::{Deserialize, Serialize};
10
11#[derive(Clone, Debug)]
17#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
18pub struct Comment {
19 span: Option<Span>,
20 content: String,
21}
22
23impl From<String> for Comment {
28 fn from(value: String) -> Self {
29 Self::new(value)
30 }
31}
32
33impl From<&str> for Comment {
34 fn from(value: &str) -> Self {
35 Self::new(value)
36 }
37}
38
39impl AsRef<str> for Comment {
40 fn as_ref(&self) -> &str {
41 self.content.as_str()
42 }
43}
44
45impl Display for Comment {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 write!(f, ";{}", self.content)
48 }
49}
50
51impl HasSourceSpan for Comment {
52 fn with_source_span(self, span: Span) -> Self {
53 let mut self_mut = self;
54 self_mut.span = Some(span);
55 self_mut
56 }
57
58 fn source_span(&self) -> Option<&Span> {
59 self.span.as_ref()
60 }
61
62 fn set_source_span(&mut self, span: Span) {
63 self.span = Some(span);
64 }
65
66 fn unset_source_span(&mut self) {
67 self.span = None;
68 }
69}
70
71impl Comment {
72 pub fn new<S>(content: S) -> Self
77 where
78 S: Into<String>,
79 {
80 Self {
81 span: Default::default(),
82 content: content.into(),
83 }
84 }
85
86 pub const fn content(&self) -> &String {
91 &self.content
92 }
93
94 pub fn set_content<T>(&mut self, content: T)
95 where
96 T: Into<String>,
97 {
98 self.content = content.into();
99 }
100}