Skip to main content

microcad_lang_base/
source.rs

1// Copyright © 2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4use crate::{GetSourceLocInfoByHash, HashId, Hashed, ResourceLocation, SourceLocInfo};
5use microcad_core::hash::ComputedHash;
6use url::Url;
7
8/// An unparsed source code with a location
9#[derive(Debug, Clone)]
10pub struct Source {
11    /// The source url
12    pub url: Url,
13    /// Line offset
14    pub line_offset: u32,
15    /// The original code
16    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}