Skip to main content

meta_language/
configuration.rs

1/// Trivia attachment strategy.
2#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3pub enum TriviaAttachmentPolicy {
4    /// Attach trivia to the containing syntax link.
5    ContainmentLink,
6    /// Attach trivia to the token link.
7    TokenLink,
8    /// Emit both attachment links when they can coexist.
9    Both,
10}
11
12/// Region detection strategy for mixed-language parsing.
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub enum RegionDetectionPolicy {
15    /// Use explicit region names such as fenced-code language tags.
16    NameDriven,
17    /// Use content sniffing.
18    ContentDriven,
19    /// Use name-driven detection first and content-driven detection as a fallback.
20    Both,
21}
22
23/// Natural-language identification backend.
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
25pub enum LanguageIdentificationDetector {
26    /// Use the trigram identifier shared with the JavaScript runtime.
27    Trigram,
28    /// Use `lingua` for language identification.
29    Lingua,
30    /// Use `whatlang` for language identification.
31    Whatlang,
32}
33
34/// Configured amount of formal meaning to expose during reconstruction.
35#[derive(Clone, Copy, Debug, PartialEq, Eq)]
36pub enum FormalizationLevel {
37    /// Natural surface text in the requested target language.
38    Natural,
39    /// Predicate form using target-language labels where they are available.
40    Lexical,
41    /// Predicate form using shared concept identifiers.
42    Concept,
43    /// Link-like proposition form including the truth marker.
44    Logical,
45}
46
47/// Direction for text/network conversion.
48#[derive(Clone, Copy, Debug, PartialEq, Eq)]
49pub enum NaturalizationDirection {
50    /// Prefer target-language natural text when reconstructing.
51    Naturalize,
52    /// Prefer the configured formal representation when reconstructing.
53    Formalize,
54}
55
56/// Whether the engine produces a mutable network or an enforced read-only one.
57#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
58pub enum AccessMode {
59    /// The network is editable: mutators such as `insert_link` and
60    /// `apply_substitution` are available on `&mut LinkNetwork`.
61    #[default]
62    Mutable,
63    /// The network is frozen: only `&self` operations (query, project,
64    /// reconstruct, verify, serialize) are reachable, and mutation attempts at
65    /// the engine boundary fail with a clear diagnostic.
66    ReadOnly,
67}
68
69impl AccessMode {
70    /// Human-readable access-mode name.
71    #[must_use]
72    pub const fn label(self) -> &'static str {
73        match self {
74            Self::Mutable => "mutable",
75            Self::ReadOnly => "read-only",
76        }
77    }
78
79    /// Whether this access mode permits mutation.
80    #[must_use]
81    pub const fn is_mutable(self) -> bool {
82        matches!(self, Self::Mutable)
83    }
84
85    /// Whether this access mode forbids mutation.
86    #[must_use]
87    pub const fn is_read_only(self) -> bool {
88        matches!(self, Self::ReadOnly)
89    }
90}
91
92/// Configuration for parse-to-network operations.
93#[derive(Clone, Copy, Debug, PartialEq, Eq)]
94pub struct ParseConfiguration {
95    trivia_attachment_policy: TriviaAttachmentPolicy,
96    region_detection_policy: RegionDetectionPolicy,
97    language_identification_detector: LanguageIdentificationDetector,
98    formalization_level: FormalizationLevel,
99    naturalization_direction: NaturalizationDirection,
100    access_mode: AccessMode,
101    profile: Option<&'static str>,
102}
103
104impl ParseConfiguration {
105    /// Creates parse configuration with the supplied trivia policy.
106    #[must_use]
107    pub const fn new(trivia_attachment_policy: TriviaAttachmentPolicy) -> Self {
108        Self {
109            trivia_attachment_policy,
110            region_detection_policy: RegionDetectionPolicy::Both,
111            language_identification_detector: LanguageIdentificationDetector::Trigram,
112            formalization_level: FormalizationLevel::Natural,
113            naturalization_direction: NaturalizationDirection::Naturalize,
114            access_mode: AccessMode::Mutable,
115            profile: None,
116        }
117    }
118
119    /// Returns configuration with a mixed-language region detection policy.
120    #[must_use]
121    pub const fn with_region_detection_policy(
122        mut self,
123        region_detection_policy: RegionDetectionPolicy,
124    ) -> Self {
125        self.region_detection_policy = region_detection_policy;
126        self
127    }
128
129    /// Returns configuration with a natural-language identification backend.
130    #[must_use]
131    pub const fn with_language_identification_detector(
132        mut self,
133        detector: LanguageIdentificationDetector,
134    ) -> Self {
135        self.language_identification_detector = detector;
136        self
137    }
138
139    /// Returns configuration with a formalization detail level.
140    #[must_use]
141    pub const fn with_formalization_level(
142        mut self,
143        formalization_level: FormalizationLevel,
144    ) -> Self {
145        self.formalization_level = formalization_level;
146        self
147    }
148
149    /// Returns configuration with a naturalization/formalization direction.
150    #[must_use]
151    pub const fn with_naturalization_direction(
152        mut self,
153        naturalization_direction: NaturalizationDirection,
154    ) -> Self {
155        self.naturalization_direction = naturalization_direction;
156        self
157    }
158
159    /// Returns configuration with a read-only or mutable engine access mode.
160    #[must_use]
161    pub const fn with_access_mode(mut self, access_mode: AccessMode) -> Self {
162        self.access_mode = access_mode;
163        self
164    }
165
166    /// Returns configuration with a language capability profile name.
167    ///
168    /// Built-in profiles such as `"JavaScript"` are materialized as queryable
169    /// links during parsing. User-declared profiles can be supplied directly to
170    /// transform-time APIs.
171    #[must_use]
172    pub const fn with_profile(mut self, profile: &'static str) -> Self {
173        self.profile = Some(profile);
174        self
175    }
176
177    /// Trivia attachment policy.
178    #[must_use]
179    pub const fn trivia_attachment_policy(self) -> TriviaAttachmentPolicy {
180        self.trivia_attachment_policy
181    }
182
183    /// Mixed-language region detection policy.
184    #[must_use]
185    pub const fn region_detection_policy(self) -> RegionDetectionPolicy {
186        self.region_detection_policy
187    }
188
189    /// Natural-language identification backend.
190    #[must_use]
191    pub const fn language_identification_detector(self) -> LanguageIdentificationDetector {
192        self.language_identification_detector
193    }
194
195    /// Formalization detail level.
196    #[must_use]
197    pub const fn formalization_level(self) -> FormalizationLevel {
198        self.formalization_level
199    }
200
201    /// Naturalization/formalization direction.
202    #[must_use]
203    pub const fn naturalization_direction(self) -> NaturalizationDirection {
204        self.naturalization_direction
205    }
206
207    /// Read-only or mutable engine access mode.
208    #[must_use]
209    pub const fn access_mode(self) -> AccessMode {
210        self.access_mode
211    }
212
213    /// Configured language capability profile name.
214    #[must_use]
215    pub const fn profile(self) -> Option<&'static str> {
216        self.profile
217    }
218}
219
220impl Default for ParseConfiguration {
221    fn default() -> Self {
222        Self::new(TriviaAttachmentPolicy::Both)
223    }
224}