1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::error::Error;

use crate::{
    file::source::FileSourceResult,
    file::{FileSource, FileStoredFormat},
    Format,
};

/// Describes a file sourced from a string
#[derive(Clone, Debug)]
pub struct FileSourceString(String);

impl<'a> From<&'a str> for FileSourceString {
    fn from(s: &'a str) -> Self {
        Self(s.into())
    }
}

impl<F> FileSource<F> for FileSourceString
where
    F: Format + FileStoredFormat + 'static,
{
    fn resolve(
        &self,
        format_hint: Option<F>,
    ) -> Result<FileSourceResult, Box<dyn Error + Send + Sync>> {
        Ok(FileSourceResult {
            uri: None,
            content: self.0.clone(),
            format: Box::new(format_hint.expect("from_str requires a set file format")),
        })
    }
}