supa_mdx_lint/
context.rs
1use std::array;
2
3use anyhow::Result;
4use bon::bon;
5use log::debug;
6
7use crate::{
8 comments::{ConfigurationCommentCollection, LintDisables, LintTimeRuleConfigs},
9 location::AdjustedOffset,
10 parser::ParseResult,
11 rope::Rope,
12 rules::RuleFilter,
13};
14
15#[derive(Clone, Hash, PartialEq, Eq)]
16pub(crate) struct ContextId([char; 12]);
17
18impl ContextId {
19 pub fn new() -> Self {
20 Self(array::from_fn(|_| fastrand::alphanumeric()))
21 }
22}
23
24impl std::fmt::Debug for ContextId {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 write!(f, "ContextId({})", String::from_iter(self.0))
27 }
28}
29
30pub(crate) struct Context<'ctx> {
31 pub(crate) key: ContextId,
34 pub(crate) parse_result: &'ctx ParseResult,
35 pub(crate) check_only_rules: RuleFilter<'ctx>,
36 pub(crate) disables: LintDisables<'ctx>,
37 pub(crate) lint_time_rule_configs: LintTimeRuleConfigs<'ctx>,
38}
39
40#[bon]
41impl<'ctx> Context<'ctx> {
42 #[builder]
43 pub(crate) fn new(
44 parse_result: &'ctx ParseResult,
45 check_only_rules: Option<&'ctx [&'ctx str]>,
46 ) -> Result<Self> {
47 let (lint_time_rule_configs, disables) =
48 ConfigurationCommentCollection::from_parse_result(parse_result)
49 .into_parts()
50 .unwrap();
51 debug!("Lint time rule configs: {:?}", lint_time_rule_configs);
52 debug!("Disables: {:?}", disables);
53
54 Ok(Self {
55 key: ContextId::new(),
56 parse_result,
57 check_only_rules,
58 disables,
59 lint_time_rule_configs,
60 })
61 }
62
63 pub(crate) fn rope(&self) -> &Rope {
64 self.parse_result.rope()
65 }
66
67 pub fn content_start_offset(&self) -> AdjustedOffset {
68 self.parse_result.content_start_offset()
69 }
70}