use crate::OwnedSource;
use std::borrow::Cow;
#[derive(Debug)]
pub struct Source<'a> {
name: Option<Cow<'a, str>>,
text: Cow<'a, str>,
}
impl<'a> Source<'a> {
pub fn new<N, T>(optname: Option<N>, text: T) -> Self
where
Cow<'a, str>: From<N>,
Cow<'a, str>: From<T>,
{
Source {
name: optname.map(|n| Cow::from(n)),
text: Cow::from(text),
}
}
pub fn new_named<N, T>(name: N, text: T) -> Self
where
Cow<'a, str>: From<N>,
Cow<'a, str>: From<T>,
{
Source::new(Some(name), text)
}
pub fn new_unnamed<T>(text: T) -> Self
where
Cow<'a, str>: From<T>,
{
Source::new(None, text)
}
pub fn name(&self) -> &str {
use crate::optname_to_str;
optname_to_str(self.name.as_ref().map(|cow| cow.as_ref()))
}
pub fn text(&self) -> &str {
self.text.as_ref()
}
}
impl<'a> From<Source<'a>> for OwnedSource {
fn from(s: Source<'a>) -> OwnedSource {
OwnedSource::new(s.name, s.text)
}
}
impl<'a> From<OwnedSource> for Source<'a> {
fn from(s: OwnedSource) -> Source<'a> {
let (optname, text) = s.unwrap();
Source::new(optname, text)
}
}