use crate::{GetSourceLocInfoByHash, HashId, Hashed, ResourceLocation, SourceLocInfo};
use microcad_core::hash::ComputedHash;
use url::Url;
#[derive(Debug, Clone)]
pub struct Source {
pub url: Url,
pub line_offset: u32,
pub code: Hashed<String>,
}
impl Source {
pub fn new(url: Url, line_offset: u32, code: String) -> Self {
Self {
url,
line_offset,
code: Hashed::new(code),
}
}
}
impl GetSourceLocInfoByHash for Source {
fn get_source_loc_info_by_hash(&'_ self, hash: HashId) -> Option<SourceLocInfo<'_>> {
if hash == self.code.computed_hash() {
Some(SourceLocInfo {
code: &self.code,
url: self.url.clone(),
line_offset: self.line_offset,
})
} else {
None
}
}
}
impl ResourceLocation for Source {
fn url(&self) -> &Url {
&self.url
}
}