use std::fmt::Debug;
use crate::{Parser, attributes::Attrlist};
pub trait IncludeFileHandler: Debug {
fn resolve_target<'src>(
&self,
source: Option<&str>,
target: &str,
attrlist: &Attrlist<'src>,
parser: &Parser,
) -> Option<IncludeContent>;
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct IncludeContent {
content: String,
encoding_handled: bool,
}
impl IncludeContent {
pub fn new(content: impl Into<String>) -> Self {
Self {
content: content.into(),
encoding_handled: false,
}
}
pub fn transcoded(content: impl Into<String>) -> Self {
Self {
content: content.into(),
encoding_handled: true,
}
}
pub fn content(&self) -> &str {
&self.content
}
pub fn encoding_handled(&self) -> bool {
self.encoding_handled
}
pub fn into_content(self) -> String {
self.content
}
}
impl From<String> for IncludeContent {
fn from(content: String) -> Self {
Self::new(content)
}
}
impl From<&str> for IncludeContent {
fn from(content: &str) -> Self {
Self::new(content)
}
}
#[cfg(test)]
mod tests {
use super::IncludeContent;
#[test]
fn new_does_not_mark_encoding_handled() {
let content = IncludeContent::new("Content.");
assert_eq!(content.content(), "Content.");
assert!(!content.encoding_handled());
assert_eq!(content.into_content(), "Content.".to_owned());
}
#[test]
fn transcoded_marks_encoding_handled() {
let content = IncludeContent::transcoded("Résumé.");
assert_eq!(content.content(), "Résumé.");
assert!(content.encoding_handled());
assert_eq!(content.into_content(), "Résumé.".to_owned());
}
#[test]
fn from_string_and_str_do_not_mark_encoding_handled() {
let from_string = IncludeContent::from("Content.".to_owned());
assert_eq!(from_string, IncludeContent::new("Content."));
assert!(!from_string.encoding_handled());
let from_str: IncludeContent = "Content.".into();
assert_eq!(from_str, IncludeContent::new("Content."));
assert!(!from_str.encoding_handled());
}
}