Skip to main content

link_cli/
named_type_links.rs

1//! [`NamedTypeLinks`], the storage interface every layer of the CLI is written
2//! against, and its implementations for the plain store and for each decorator.
3//!
4//! A store that implements this trait can be dropped anywhere in the stack, so
5//! an application can add its own layer -- a cache, a permission check, a
6//! remote store -- without the query processor knowing about it.
7
8use anyhow::{Context, Result};
9use std::collections::HashSet;
10use std::fs::OpenOptions;
11use std::io::{BufWriter, Write};
12use std::path::Path;
13
14use crate::error::LinkError;
15use crate::link::{GenericLink, Link};
16use crate::link_storage::{ChangeObserver, LinkStorage};
17use crate::named_types::{NamedTypes, NamedTypesDecorator};
18use crate::query_address::QueryAddress;
19
20fn validate_ensure_address<T: QueryAddress>(id: T) -> Result<()> {
21    if id == T::from_byte(0) || id == T::MAX {
22        return Err(LinkError::InvalidFormat(format!(
23            "Cannot ensure unsupported link address {id}"
24        ))
25        .into());
26    }
27    Ok(())
28}
29
30pub trait NamedTypeLinks<T: QueryAddress = u32> {
31    /// A checked transport may restrict numeric input and query expansion.
32    fn query_limits(&self) -> Option<crate::query_options::QueryLimits> {
33        None
34    }
35    /// Reserve the actual allocation sequence without staging observable rows.
36    /// Backends with a recycling allocator may retain the legacy probe path.
37    fn reserve_query_allocations(
38        &mut self,
39        _processor: &crate::query_processor::GenericQueryProcessor<T>,
40        _restriction: &crate::lino_link::LinoLink,
41        _substitution: &crate::lino_link::LinoLink,
42    ) -> Result<Option<HashSet<T>>> {
43        Ok(None)
44    }
45    fn create(&mut self, source: T, target: T) -> T;
46    /// Fallible allocation for backends that can report transaction/address errors.
47    fn try_create(&mut self, source: T, target: T) -> Result<T> {
48        Ok(self.create(source, target))
49    }
50    fn ensure_created(&mut self, id: T) -> T;
51    fn try_ensure_created(&mut self, id: T) -> Result<T> {
52        validate_ensure_address(id)?;
53        Ok(self.ensure_created(id))
54    }
55    /// Whether a reference of a query to `id` names a link that exists.
56    ///
57    /// The same as [`Self::exists`], but for a view that hides links: a
58    /// reference is a right of its own, so a link the subject cannot read
59    /// exists for a reference when the subject may refer to it, and otherwise
60    /// answers as a missing one (issue #161).
61    fn reference_exists(&mut self, id: T) -> bool {
62        self.exists(id)
63    }
64    /// Whether `id` is an address of this store, so a link created there
65    /// ([`Self::ensure_created`], the undo of a delete) belongs in it; `None`
66    /// for a plain store, which has no addresses of its own.
67    ///
68    /// A store made of others answers: a [`RemapLinks`](crate::RemapLinks)
69    /// claims the addresses of its map, and a [`UnionLinks`](crate::UnionLinks)
70    /// creates a link at an address in the first member that claims it, the
71    /// primary one when none does, so a union recreates a link in the member
72    /// its address is of (issue #149). The C# twin is `IComposedLinks.Claims`.
73    fn claims(&mut self, id: T) -> Option<bool> {
74        let _ = id;
75        None
76    }
77    /// The addresses the next `count` links created in the store get.
78    ///
79    /// A store reuses the address freed last first, so the lowest free one is
80    /// not necessarily next. Creating the links and deleting them again in
81    /// reverse leaves the store exactly as it was, and is the one way to ask
82    /// any store, a remote one included. A view that may not create asks the
83    /// store below.
84    fn next_created_addresses(&mut self, count: usize) -> Result<Vec<T>> {
85        let mut addresses = Vec::with_capacity(count);
86        for _ in 0..count {
87            addresses.push(self.try_create(T::from_byte(0), T::from_byte(0))?);
88        }
89        for &address in addresses.iter().rev() {
90            self.delete(address)?;
91        }
92        Ok(addresses)
93    }
94    fn get_link(&mut self, id: T) -> Option<GenericLink<T>>;
95    fn exists(&mut self, id: T) -> bool;
96    fn update(&mut self, id: T, source: T, target: T) -> Result<GenericLink<T>> {
97        self.update_observed(id, source, target, &mut |_, _| {})
98    }
99    /// [`Self::update`], reporting every change the update caused.
100    ///
101    /// An update that would duplicate an existing doublet is resolved into a
102    /// merge: the updated link is deleted and its usages are moved to the
103    /// duplicate, so one call can make several changes. The C# CLI sees them
104    /// because `AdvancedMixedQueryProcessor` hands a handler to the store:
105    ///
106    /// ```csharp
107    /// links.Update(before, after, (beforeState, afterState) =>
108    ///     options.ChangesHandler?.Invoke(beforeState, afterState) ?? links.Constants.Continue);
109    /// ```
110    fn update_observed(
111        &mut self,
112        id: T,
113        source: T,
114        target: T,
115        observer: ChangeObserver<'_, T>,
116    ) -> Result<GenericLink<T>>;
117    fn delete(&mut self, id: T) -> Result<GenericLink<T>> {
118        self.delete_observed(id, &mut |_, _| {})
119    }
120    /// [`Self::delete`], reporting every change the deletion caused.
121    ///
122    /// Deleting a link cascades into every link that still used it, and each of
123    /// those removals is a change in its own right, reported the way
124    /// `AdvancedMixedQueryProcessor.RemoveLinks` reports them in C#:
125    ///
126    /// ```csharp
127    /// links.Delete(link, (before, after) =>
128    ///     options.ChangesHandler?.Invoke(before, after) ?? links.Constants.Continue);
129    /// ```
130    fn delete_observed(&mut self, id: T, observer: ChangeObserver<'_, T>)
131        -> Result<GenericLink<T>>;
132    fn all_links(&mut self) -> Vec<GenericLink<T>>;
133    /// The links that match a pattern, where `None` matches any value, in
134    /// the order of their addresses.
135    ///
136    /// The default filters [`Self::all_links`]; a store with a lookup index
137    /// overrides it.
138    fn query_links(
139        &mut self,
140        index: Option<T>,
141        source: Option<T>,
142        target: Option<T>,
143    ) -> Vec<GenericLink<T>> {
144        let mut links: Vec<GenericLink<T>> = self
145            .all_links()
146            .into_iter()
147            .filter(|link| {
148                index.is_none_or(|index| link.index == index)
149                    && source.is_none_or(|source| link.source == source)
150                    && target.is_none_or(|target| link.target == target)
151            })
152            .collect();
153        links.sort_unstable_by_key(|link| link.index);
154        links
155    }
156    fn search(&mut self, source: T, target: T) -> Option<T>;
157    fn get_or_create(&mut self, source: T, target: T) -> T;
158    /// Fallible pair lookup/allocation for composable query and decorator writes.
159    fn try_get_or_create(&mut self, source: T, target: T) -> Result<T> {
160        if let Some(id) = self.search(source, target) {
161            return Ok(id);
162        }
163        self.try_create(source, target)
164    }
165    fn get_name(&mut self, id: T) -> Result<Option<String>>;
166    fn set_name(&mut self, id: T, name: &str) -> Result<T>;
167    fn get_by_name(&mut self, name: &str) -> Result<Option<T>>;
168    fn remove_name(&mut self, id: T) -> Result<()>;
169    fn save(&mut self) -> Result<()>;
170
171    fn get_or_create_named(&mut self, name: &str) -> Result<T> {
172        if let Some(id) = self.get_by_name(name)? {
173            return Ok(id);
174        }
175
176        let id = self.try_create(T::from_byte(0), T::from_byte(0))?;
177        self.set_name(id, name)?;
178        self.update(id, id, id)?;
179        Ok(id)
180    }
181
182    fn format_reference(&mut self, id: T) -> Result<String> {
183        Ok(self
184            .get_name(id)?
185            .map(|name| {
186                if name.parse::<T>().is_ok() && name.parse::<u32>().is_err() {
187                    links_notation::encode_reference_literal(&name)
188                } else {
189                    escape_lino_reference(&name)
190                }
191            })
192            .unwrap_or_else(|| id.to_string()))
193    }
194
195    fn format_lino(&mut self, link: &GenericLink<T>) -> Result<String> {
196        Ok(format!(
197            "({}: {} {})",
198            self.format_reference(link.index)?,
199            self.format_reference(link.source)?,
200            self.format_reference(link.target)?
201        ))
202    }
203
204    fn lino_lines(&mut self) -> Result<Vec<String>> {
205        let mut links = self.all_links();
206        links.sort_by_key(|link| link.index);
207
208        links
209            .iter()
210            .map(|link| self.format_lino(link))
211            .collect::<Result<Vec<_>>>()
212    }
213
214    fn write_lino_output<P: AsRef<Path>>(&mut self, path: P) -> Result<()>
215    where
216        Self: Sized,
217    {
218        let path = path.as_ref();
219        let file = OpenOptions::new()
220            .write(true)
221            .create(true)
222            .truncate(true)
223            .open(path)
224            .with_context(|| format!("Failed to create LiNo output: {}", path.display()))?;
225
226        let mut writer = BufWriter::new(file);
227        for line in self.lino_lines()? {
228            writeln!(writer, "{line}")?;
229        }
230        writer.flush()?;
231        Ok(())
232    }
233
234    fn print_all_lino(&mut self) -> Result<()> {
235        for line in self.lino_lines()? {
236            println!("{line}");
237        }
238        Ok(())
239    }
240
241    fn print_change_lino(
242        &mut self,
243        before: &Option<GenericLink<T>>,
244        after: &Option<GenericLink<T>>,
245    ) -> Result<()> {
246        let before_text = before
247            .map(|link| self.format_lino(&link))
248            .transpose()?
249            .unwrap_or_default();
250        let after_text = after
251            .map(|link| self.format_lino(&link))
252            .transpose()?
253            .unwrap_or_default();
254        println!("({before_text}) ({after_text})");
255        Ok(())
256    }
257
258    fn format_structure(&mut self, id: T) -> Result<String> {
259        let mut visited = HashSet::new();
260        self.format_structure_recursive(id, &mut visited)
261    }
262
263    fn format_structure_recursive(&mut self, id: T, visited: &mut HashSet<T>) -> Result<String> {
264        crate::structure_format::format_structure(
265            self,
266            id,
267            visited,
268            |store, address| store.get_link(address),
269            |store, address| store.exists(address),
270            |store, address| store.format_reference(address),
271        )
272    }
273}
274
275impl NamedTypeLinks for LinkStorage {
276    fn reserve_query_allocations(
277        &mut self,
278        processor: &crate::QueryProcessor,
279        restriction: &crate::lino_link::LinoLink,
280        substitution: &crate::lino_link::LinoLink,
281    ) -> Result<Option<HashSet<u32>>> {
282        preview_recycling_query(self.query_copy(), processor, restriction, substitution)
283    }
284
285    fn create(&mut self, source: u32, target: u32) -> u32 {
286        LinkStorage::create(self, source, target)
287    }
288
289    fn ensure_created(&mut self, id: u32) -> u32 {
290        LinkStorage::ensure_created(self, id)
291    }
292
293    fn get_link(&mut self, id: u32) -> Option<Link> {
294        self.get(id).copied()
295    }
296
297    fn exists(&mut self, id: u32) -> bool {
298        LinkStorage::exists(self, id)
299    }
300
301    fn update_observed(
302        &mut self,
303        id: u32,
304        source: u32,
305        target: u32,
306        observer: ChangeObserver<'_>,
307    ) -> Result<Link> {
308        LinkStorage::update_observed(self, id, source, target, observer)
309    }
310
311    fn delete_observed(&mut self, id: u32, observer: ChangeObserver<'_>) -> Result<Link> {
312        LinkStorage::delete_observed(self, id, observer)
313    }
314
315    fn all_links(&mut self) -> Vec<Link> {
316        self.all().into_iter().copied().collect()
317    }
318
319    fn query_links(
320        &mut self,
321        index: Option<u32>,
322        source: Option<u32>,
323        target: Option<u32>,
324    ) -> Vec<Link> {
325        let mut links: Vec<Link> = LinkStorage::query(self, index, source, target)
326            .into_iter()
327            .copied()
328            .collect();
329        links.sort_unstable_by_key(|link| link.index);
330        links
331    }
332
333    fn search(&mut self, source: u32, target: u32) -> Option<u32> {
334        LinkStorage::search(self, source, target)
335    }
336
337    fn get_or_create(&mut self, source: u32, target: u32) -> u32 {
338        LinkStorage::get_or_create(self, source, target)
339    }
340
341    fn get_name(&mut self, id: u32) -> Result<Option<String>> {
342        Ok(LinkStorage::get_name(self, id).cloned())
343    }
344
345    fn set_name(&mut self, id: u32, name: &str) -> Result<u32> {
346        LinkStorage::set_name(self, id, name);
347        Ok(id)
348    }
349
350    fn get_by_name(&mut self, name: &str) -> Result<Option<u32>> {
351        Ok(LinkStorage::get_by_name(self, name))
352    }
353
354    fn remove_name(&mut self, id: u32) -> Result<()> {
355        LinkStorage::remove_name(self, id);
356        Ok(())
357    }
358
359    fn save(&mut self) -> Result<()> {
360        LinkStorage::save(self)
361    }
362
363    fn get_or_create_named(&mut self, name: &str) -> Result<u32> {
364        Ok(LinkStorage::get_or_create_named(self, name))
365    }
366}
367
368impl NamedTypeLinks for NamedTypesDecorator {
369    fn reserve_query_allocations(
370        &mut self,
371        processor: &crate::QueryProcessor,
372        restriction: &crate::lino_link::LinoLink,
373        substitution: &crate::lino_link::LinoLink,
374    ) -> Result<Option<HashSet<u32>>> {
375        preview_recycling_query(self.query_copy(), processor, restriction, substitution)
376    }
377
378    fn create(&mut self, source: u32, target: u32) -> u32 {
379        NamedTypesDecorator::create(self, source, target)
380    }
381
382    fn ensure_created(&mut self, id: u32) -> u32 {
383        NamedTypesDecorator::ensure_created(self, id)
384    }
385
386    fn get_link(&mut self, id: u32) -> Option<Link> {
387        self.get(id).copied()
388    }
389
390    fn exists(&mut self, id: u32) -> bool {
391        NamedTypesDecorator::exists(self, id)
392    }
393
394    fn update_observed(
395        &mut self,
396        id: u32,
397        source: u32,
398        target: u32,
399        observer: ChangeObserver<'_>,
400    ) -> Result<Link> {
401        NamedTypesDecorator::update_observed(self, id, source, target, observer)
402    }
403
404    fn delete_observed(&mut self, id: u32, observer: ChangeObserver<'_>) -> Result<Link> {
405        NamedTypesDecorator::delete_observed(self, id, observer)
406    }
407
408    fn all_links(&mut self) -> Vec<Link> {
409        self.all().into_iter().copied().collect()
410    }
411
412    fn query_links(
413        &mut self,
414        index: Option<u32>,
415        source: Option<u32>,
416        target: Option<u32>,
417    ) -> Vec<Link> {
418        let mut links: Vec<Link> = NamedTypesDecorator::query(self, index, source, target)
419            .into_iter()
420            .copied()
421            .collect();
422        links.sort_unstable_by_key(|link| link.index);
423        links
424    }
425
426    fn search(&mut self, source: u32, target: u32) -> Option<u32> {
427        NamedTypesDecorator::search(self, source, target)
428    }
429
430    fn get_or_create(&mut self, source: u32, target: u32) -> u32 {
431        NamedTypesDecorator::get_or_create(self, source, target)
432    }
433
434    fn get_name(&mut self, id: u32) -> Result<Option<String>> {
435        NamedTypes::get_name(self, id)
436    }
437
438    fn set_name(&mut self, id: u32, name: &str) -> Result<u32> {
439        NamedTypes::set_name(self, id, name)
440    }
441
442    fn get_by_name(&mut self, name: &str) -> Result<Option<u32>> {
443        NamedTypes::get_by_name(self, name)
444    }
445
446    fn remove_name(&mut self, id: u32) -> Result<()> {
447        NamedTypes::remove_name(self, id)
448    }
449
450    fn save(&mut self) -> Result<()> {
451        NamedTypesDecorator::save(self)
452    }
453}
454
455impl<L: crate::transactions::TransitionLogStore> NamedTypeLinks
456    for crate::transactions::GenericTransactionsDecorator<u32, NamedTypesDecorator, L>
457{
458    fn reserve_query_allocations(
459        &mut self,
460        processor: &crate::QueryProcessor,
461        restriction: &crate::lino_link::LinoLink,
462        substitution: &crate::lino_link::LinoLink,
463    ) -> Result<Option<HashSet<u32>>> {
464        self.ensure_write_allowed()?;
465        self.inner_mut()
466            .reserve_query_allocations(processor, restriction, substitution)
467    }
468
469    fn try_get_or_create(&mut self, source: u32, target: u32) -> Result<u32> {
470        Ok(crate::transactions::GenericTransactionsDecorator::get_or_create(self, source, target)?)
471    }
472
473    fn try_create(&mut self, source: u32, target: u32) -> Result<u32> {
474        Ok(crate::transactions::GenericTransactionsDecorator::create(
475            self, source, target,
476        )?)
477    }
478
479    fn try_ensure_created(&mut self, id: u32) -> Result<u32> {
480        validate_ensure_address(id)?;
481        Ok(crate::transactions::GenericTransactionsDecorator::ensure_created(self, id)?)
482    }
483
484    fn create(&mut self, source: u32, target: u32) -> u32 {
485        crate::transactions::GenericTransactionsDecorator::create(self, source, target)
486            .expect("TransactionsDecorator::create failed in NamedTypeLinks bridge")
487    }
488
489    fn ensure_created(&mut self, id: u32) -> u32 {
490        crate::transactions::GenericTransactionsDecorator::ensure_created(self, id)
491            .expect("TransactionsDecorator::ensure_created failed in NamedTypeLinks bridge")
492    }
493
494    fn get_link(&mut self, id: u32) -> Option<Link> {
495        self.get(id).copied()
496    }
497
498    fn exists(&mut self, id: u32) -> bool {
499        crate::transactions::GenericTransactionsDecorator::exists(self, id)
500    }
501
502    fn update_observed(
503        &mut self,
504        id: u32,
505        source: u32,
506        target: u32,
507        observer: ChangeObserver<'_>,
508    ) -> Result<Link> {
509        Ok(
510            crate::transactions::GenericTransactionsDecorator::update_observed(
511                self, id, source, target, observer,
512            )?,
513        )
514    }
515
516    fn delete_observed(&mut self, id: u32, observer: ChangeObserver<'_>) -> Result<Link> {
517        Ok(crate::transactions::GenericTransactionsDecorator::delete_observed(self, id, observer)?)
518    }
519
520    fn all_links(&mut self) -> Vec<Link> {
521        self.all().into_iter().copied().collect()
522    }
523
524    fn search(&mut self, source: u32, target: u32) -> Option<u32> {
525        crate::transactions::GenericTransactionsDecorator::search(self, source, target)
526    }
527
528    fn get_or_create(&mut self, source: u32, target: u32) -> u32 {
529        crate::transactions::GenericTransactionsDecorator::get_or_create(self, source, target)
530            .expect("TransactionsDecorator::get_or_create failed in NamedTypeLinks bridge")
531    }
532
533    fn get_name(&mut self, id: u32) -> Result<Option<String>> {
534        NamedTypes::get_name(self.inner_mut(), id)
535    }
536
537    fn set_name(&mut self, id: u32, name: &str) -> Result<u32> {
538        Ok(crate::transactions::GenericTransactionsDecorator::set_name(
539            self, id, name,
540        )?)
541    }
542
543    fn get_by_name(&mut self, name: &str) -> Result<Option<u32>> {
544        NamedTypes::get_by_name(self.inner_mut(), name)
545    }
546
547    fn remove_name(&mut self, id: u32) -> Result<()> {
548        Ok(crate::transactions::GenericTransactionsDecorator::remove_name(self, id)?)
549    }
550
551    fn save(&mut self) -> Result<()> {
552        Ok(crate::transactions::GenericTransactionsDecorator::save(
553            self,
554        )?)
555    }
556}
557
558impl<B: NamedTypeLinks, L: crate::transactions::TransitionLogStore> NamedTypeLinks
559    for crate::version_control::GenericVersionControlDecorator<B, L>
560{
561    fn reserve_query_allocations(
562        &mut self,
563        processor: &crate::QueryProcessor,
564        restriction: &crate::lino_link::LinoLink,
565        substitution: &crate::lino_link::LinoLink,
566    ) -> Result<Option<HashSet<u32>>> {
567        self.ensure_write_allowed()?;
568        self.transactions_mut()
569            .reserve_query_allocations(processor, restriction, substitution)
570    }
571
572    fn try_get_or_create(&mut self, source: u32, target: u32) -> Result<u32> {
573        crate::version_control::GenericVersionControlDecorator::get_or_create(self, source, target)
574    }
575
576    fn try_create(&mut self, source: u32, target: u32) -> Result<u32> {
577        crate::version_control::GenericVersionControlDecorator::create(self, source, target)
578    }
579
580    fn try_ensure_created(&mut self, id: u32) -> Result<u32> {
581        validate_ensure_address(id)?;
582        crate::version_control::GenericVersionControlDecorator::try_ensure_created(self, id)
583    }
584
585    fn create(&mut self, source: u32, target: u32) -> u32 {
586        crate::version_control::GenericVersionControlDecorator::create(self, source, target)
587            .expect("VersionControlDecorator::create failed in NamedTypeLinks bridge")
588    }
589
590    fn ensure_created(&mut self, id: u32) -> u32 {
591        crate::version_control::GenericVersionControlDecorator::ensure_created(self, id)
592    }
593
594    fn get_link(&mut self, id: u32) -> Option<Link> {
595        self.get(id).copied()
596    }
597
598    fn exists(&mut self, id: u32) -> bool {
599        crate::version_control::GenericVersionControlDecorator::exists(self, id)
600    }
601
602    fn update_observed(
603        &mut self,
604        id: u32,
605        source: u32,
606        target: u32,
607        observer: ChangeObserver<'_>,
608    ) -> Result<Link> {
609        crate::version_control::GenericVersionControlDecorator::update_observed(
610            self, id, source, target, observer,
611        )
612    }
613
614    fn delete_observed(&mut self, id: u32, observer: ChangeObserver<'_>) -> Result<Link> {
615        crate::version_control::GenericVersionControlDecorator::delete_observed(self, id, observer)
616    }
617
618    fn all_links(&mut self) -> Vec<Link> {
619        self.all().into_iter().copied().collect()
620    }
621
622    fn search(&mut self, source: u32, target: u32) -> Option<u32> {
623        crate::version_control::GenericVersionControlDecorator::search(self, source, target)
624    }
625
626    fn get_or_create(&mut self, source: u32, target: u32) -> u32 {
627        crate::version_control::GenericVersionControlDecorator::get_or_create(self, source, target)
628            .expect("VersionControlDecorator::get_or_create failed in NamedTypeLinks bridge")
629    }
630
631    fn get_name(&mut self, id: u32) -> Result<Option<String>> {
632        NamedTypes::get_name(self.transactions_mut().inner_mut(), id)
633    }
634
635    fn set_name(&mut self, id: u32, name: &str) -> Result<u32> {
636        crate::version_control::GenericVersionControlDecorator::set_name(self, id, name)
637    }
638
639    fn get_by_name(&mut self, name: &str) -> Result<Option<u32>> {
640        NamedTypes::get_by_name(self.transactions_mut().inner_mut(), name)
641    }
642
643    fn remove_name(&mut self, id: u32) -> Result<()> {
644        crate::version_control::GenericVersionControlDecorator::remove_name(self, id)
645    }
646
647    fn save(&mut self) -> Result<()> {
648        crate::version_control::GenericVersionControlDecorator::save(self)
649    }
650}
651
652fn preview_recycling_query(
653    mut scratch: impl NamedTypeLinks,
654    processor: &crate::QueryProcessor,
655    restriction: &crate::lino_link::LinoLink,
656    substitution: &crate::lino_link::LinoLink,
657) -> Result<Option<HashSet<u32>>> {
658    processor.preview(&mut scratch, restriction, substitution)?;
659    Ok(Some(
660        scratch
661            .all_links()
662            .into_iter()
663            .map(|row| row.index)
664            .collect(),
665    ))
666}
667
668/// A borrowed store is a store: a layer can wrap `&mut store` and leave the
669/// store with its owner, as the permission policy does when it reads the
670/// types of the data through a [`TypesDecorator`](crate::types::TypesDecorator).
671impl<T: QueryAddress, L: NamedTypeLinks<T> + ?Sized> NamedTypeLinks<T> for &mut L {
672    fn query_limits(&self) -> Option<crate::query_options::QueryLimits> {
673        (**self).query_limits()
674    }
675    fn reserve_query_allocations(
676        &mut self,
677        processor: &crate::query_processor::GenericQueryProcessor<T>,
678        restriction: &crate::lino_link::LinoLink,
679        substitution: &crate::lino_link::LinoLink,
680    ) -> Result<Option<HashSet<T>>> {
681        (**self).reserve_query_allocations(processor, restriction, substitution)
682    }
683    fn create(&mut self, source: T, target: T) -> T {
684        (**self).create(source, target)
685    }
686
687    fn ensure_created(&mut self, id: T) -> T {
688        (**self).ensure_created(id)
689    }
690
691    fn try_ensure_created(&mut self, id: T) -> Result<T> {
692        (**self).try_ensure_created(id)
693    }
694
695    fn get_link(&mut self, id: T) -> Option<GenericLink<T>> {
696        (**self).get_link(id)
697    }
698
699    fn exists(&mut self, id: T) -> bool {
700        (**self).exists(id)
701    }
702
703    fn try_create(&mut self, source: T, target: T) -> Result<T> {
704        (**self).try_create(source, target)
705    }
706
707    fn try_get_or_create(&mut self, source: T, target: T) -> Result<T> {
708        (**self).try_get_or_create(source, target)
709    }
710
711    fn reference_exists(&mut self, id: T) -> bool {
712        (**self).reference_exists(id)
713    }
714
715    fn claims(&mut self, id: T) -> Option<bool> {
716        (**self).claims(id)
717    }
718
719    fn next_created_addresses(&mut self, count: usize) -> Result<Vec<T>> {
720        (**self).next_created_addresses(count)
721    }
722
723    fn update_observed(
724        &mut self,
725        id: T,
726        source: T,
727        target: T,
728        observer: ChangeObserver<'_, T>,
729    ) -> Result<GenericLink<T>> {
730        (**self).update_observed(id, source, target, observer)
731    }
732
733    fn delete_observed(
734        &mut self,
735        id: T,
736        observer: ChangeObserver<'_, T>,
737    ) -> Result<GenericLink<T>> {
738        (**self).delete_observed(id, observer)
739    }
740
741    fn all_links(&mut self) -> Vec<GenericLink<T>> {
742        (**self).all_links()
743    }
744
745    fn query_links(
746        &mut self,
747        index: Option<T>,
748        source: Option<T>,
749        target: Option<T>,
750    ) -> Vec<GenericLink<T>> {
751        (**self).query_links(index, source, target)
752    }
753
754    fn search(&mut self, source: T, target: T) -> Option<T> {
755        (**self).search(source, target)
756    }
757
758    fn get_or_create(&mut self, source: T, target: T) -> T {
759        (**self).get_or_create(source, target)
760    }
761
762    fn get_name(&mut self, id: T) -> Result<Option<String>> {
763        (**self).get_name(id)
764    }
765
766    fn set_name(&mut self, id: T, name: &str) -> Result<T> {
767        (**self).set_name(id, name)
768    }
769
770    fn get_by_name(&mut self, name: &str) -> Result<Option<T>> {
771        (**self).get_by_name(name)
772    }
773
774    fn remove_name(&mut self, id: T) -> Result<()> {
775        (**self).remove_name(id)
776    }
777
778    fn save(&mut self) -> Result<()> {
779        (**self).save()
780    }
781
782    fn get_or_create_named(&mut self, name: &str) -> Result<T> {
783        (**self).get_or_create_named(name)
784    }
785}
786
787/// The text of a name used as a reference. The notation decides how to delimit
788/// it, except for digits that would otherwise read as an address and for mixed
789/// quotes, whose literal form the C# port has always written.
790pub fn escape_lino_reference(reference: &str) -> String {
791    if reference.is_empty() {
792        return String::new();
793    }
794    if reference.parse::<u32>().is_ok() {
795        return format!("'{reference}'");
796    }
797    if reference.contains('\'') && reference.contains('"') {
798        return links_notation::encode_reference_literal(reference);
799    }
800    links_notation::LiNo::Ref(reference).to_string()
801}