use super::Separators;
use crate::display::SubcomponentDisplay;
use std::ops::Range;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Subcomponent<'m> {
pub value: &'m str,
pub range: Range<usize>,
}
impl<'m> Subcomponent<'m> {
pub(crate) fn new_single(source: &'m str, range: Range<usize>) -> Self {
Subcomponent {
value: source,
range,
}
}
#[inline]
pub fn display(&'m self, separators: &'m Separators) -> SubcomponentDisplay<'m> {
SubcomponentDisplay {
value: self.value,
separators,
}
}
#[inline]
pub fn raw_value(&self) -> &'m str {
self.value
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn subcomponents_can_display_raw() {
let separators = Separators::default();
let subcomponent = Subcomponent {
value: r"foo\F\bar",
range: 0..1, };
assert_eq!(
format!("{:#}", subcomponent.display(&separators)),
r"foo\F\bar"
);
}
#[test]
fn subcomponents_can_display_decoded() {
let separators = Separators::default();
let subcomponent = Subcomponent {
value: r"foo\F\bar",
range: 0..1, };
assert_eq!(format!("{}", subcomponent.display(&separators)), "foo|bar");
}
}