use crate::model::{HasSourceSpan, Span};
use std::fmt::Display;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Comment {
span: Option<Span>,
content: String,
}
impl From<String> for Comment {
fn from(value: String) -> Self {
Self::new(value)
}
}
impl From<&str> for Comment {
fn from(value: &str) -> Self {
Self::new(value)
}
}
impl AsRef<str> for Comment {
fn as_ref(&self) -> &str {
self.content.as_str()
}
}
impl Display for Comment {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, ";{}", self.content)
}
}
impl HasSourceSpan for Comment {
fn with_source_span(self, span: Span) -> Self {
let mut self_mut = self;
self_mut.span = Some(span);
self_mut
}
fn source_span(&self) -> Option<&Span> {
self.span.as_ref()
}
fn set_source_span(&mut self, span: Span) {
self.span = Some(span);
}
fn unset_source_span(&mut self) {
self.span = None;
}
}
impl Comment {
pub fn new<S>(content: S) -> Self
where
S: Into<String>,
{
Self {
span: Default::default(),
content: content.into(),
}
}
pub const fn content(&self) -> &String {
&self.content
}
pub fn set_content<T>(&mut self, content: T)
where
T: Into<String>,
{
self.content = content.into();
}
}