microcad_lang_base/
source.rs1use crate::{GetSourceLocInfoByHash, HashId, Hashed, ResourceLocation, SourceLocInfo};
5use microcad_core::hash::ComputedHash;
6use url::Url;
7
8#[derive(Debug, Clone)]
10pub struct Source {
11 pub url: Url,
13 pub line_offset: u32,
15 pub code: Hashed<String>,
17}
18
19impl Source {
20 pub fn new(url: Url, line_offset: u32, code: String) -> Self {
21 Self {
22 url,
23 line_offset,
24 code: Hashed::new(code),
25 }
26 }
27}
28
29impl GetSourceLocInfoByHash for Source {
30 fn get_source_loc_info_by_hash(&'_ self, hash: HashId) -> Option<SourceLocInfo<'_>> {
31 if hash == self.code.computed_hash() {
32 Some(SourceLocInfo {
33 code: &self.code,
34 url: self.url.clone(),
35 line_offset: self.line_offset,
36 })
37 } else {
38 None
39 }
40 }
41}
42
43impl ResourceLocation for Source {
44 fn url(&self) -> &Url {
45 &self.url
46 }
47}