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 NamedTypeLinks for crate::transactions::TransactionsDecorator {
456    fn reserve_query_allocations(
457        &mut self,
458        processor: &crate::QueryProcessor,
459        restriction: &crate::lino_link::LinoLink,
460        substitution: &crate::lino_link::LinoLink,
461    ) -> Result<Option<HashSet<u32>>> {
462        self.ensure_write_allowed()?;
463        self.inner_mut()
464            .reserve_query_allocations(processor, restriction, substitution)
465    }
466
467    fn try_get_or_create(&mut self, source: u32, target: u32) -> Result<u32> {
468        Ok(crate::transactions::TransactionsDecorator::get_or_create(
469            self, source, target,
470        )?)
471    }
472
473    fn try_create(&mut self, source: u32, target: u32) -> Result<u32> {
474        Ok(crate::transactions::TransactionsDecorator::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::TransactionsDecorator::ensure_created(
482            self, id,
483        )?)
484    }
485
486    fn create(&mut self, source: u32, target: u32) -> u32 {
487        crate::transactions::TransactionsDecorator::create(self, source, target)
488            .expect("TransactionsDecorator::create failed in NamedTypeLinks bridge")
489    }
490
491    fn ensure_created(&mut self, id: u32) -> u32 {
492        crate::transactions::TransactionsDecorator::ensure_created(self, id)
493            .expect("TransactionsDecorator::ensure_created failed in NamedTypeLinks bridge")
494    }
495
496    fn get_link(&mut self, id: u32) -> Option<Link> {
497        self.get(id).copied()
498    }
499
500    fn exists(&mut self, id: u32) -> bool {
501        crate::transactions::TransactionsDecorator::exists(self, id)
502    }
503
504    fn update_observed(
505        &mut self,
506        id: u32,
507        source: u32,
508        target: u32,
509        observer: ChangeObserver<'_>,
510    ) -> Result<Link> {
511        Ok(crate::transactions::TransactionsDecorator::update_observed(
512            self, id, source, target, observer,
513        )?)
514    }
515
516    fn delete_observed(&mut self, id: u32, observer: ChangeObserver<'_>) -> Result<Link> {
517        Ok(crate::transactions::TransactionsDecorator::delete_observed(
518            self, id, observer,
519        )?)
520    }
521
522    fn all_links(&mut self) -> Vec<Link> {
523        self.all().into_iter().copied().collect()
524    }
525
526    fn search(&mut self, source: u32, target: u32) -> Option<u32> {
527        crate::transactions::TransactionsDecorator::search(self, source, target)
528    }
529
530    fn get_or_create(&mut self, source: u32, target: u32) -> u32 {
531        crate::transactions::TransactionsDecorator::get_or_create(self, source, target)
532            .expect("TransactionsDecorator::get_or_create failed in NamedTypeLinks bridge")
533    }
534
535    fn get_name(&mut self, id: u32) -> Result<Option<String>> {
536        NamedTypes::get_name(self.inner_mut(), id)
537    }
538
539    fn set_name(&mut self, id: u32, name: &str) -> Result<u32> {
540        Ok(crate::transactions::TransactionsDecorator::set_name(
541            self, id, name,
542        )?)
543    }
544
545    fn get_by_name(&mut self, name: &str) -> Result<Option<u32>> {
546        NamedTypes::get_by_name(self.inner_mut(), name)
547    }
548
549    fn remove_name(&mut self, id: u32) -> Result<()> {
550        Ok(crate::transactions::TransactionsDecorator::remove_name(
551            self, id,
552        )?)
553    }
554
555    fn save(&mut self) -> Result<()> {
556        Ok(crate::transactions::TransactionsDecorator::save(self)?)
557    }
558}
559
560impl<B: NamedTypeLinks> NamedTypeLinks
561    for crate::version_control::GenericVersionControlDecorator<B>
562{
563    fn reserve_query_allocations(
564        &mut self,
565        processor: &crate::QueryProcessor,
566        restriction: &crate::lino_link::LinoLink,
567        substitution: &crate::lino_link::LinoLink,
568    ) -> Result<Option<HashSet<u32>>> {
569        self.ensure_write_allowed()?;
570        self.transactions_mut()
571            .reserve_query_allocations(processor, restriction, substitution)
572    }
573
574    fn try_get_or_create(&mut self, source: u32, target: u32) -> Result<u32> {
575        crate::version_control::GenericVersionControlDecorator::get_or_create(self, source, target)
576    }
577
578    fn try_create(&mut self, source: u32, target: u32) -> Result<u32> {
579        crate::version_control::GenericVersionControlDecorator::create(self, source, target)
580    }
581
582    fn try_ensure_created(&mut self, id: u32) -> Result<u32> {
583        validate_ensure_address(id)?;
584        crate::version_control::GenericVersionControlDecorator::try_ensure_created(self, id)
585    }
586
587    fn create(&mut self, source: u32, target: u32) -> u32 {
588        crate::version_control::GenericVersionControlDecorator::create(self, source, target)
589            .expect("VersionControlDecorator::create failed in NamedTypeLinks bridge")
590    }
591
592    fn ensure_created(&mut self, id: u32) -> u32 {
593        crate::version_control::GenericVersionControlDecorator::ensure_created(self, id)
594    }
595
596    fn get_link(&mut self, id: u32) -> Option<Link> {
597        self.get(id).copied()
598    }
599
600    fn exists(&mut self, id: u32) -> bool {
601        crate::version_control::GenericVersionControlDecorator::exists(self, id)
602    }
603
604    fn update_observed(
605        &mut self,
606        id: u32,
607        source: u32,
608        target: u32,
609        observer: ChangeObserver<'_>,
610    ) -> Result<Link> {
611        crate::version_control::GenericVersionControlDecorator::update_observed(
612            self, id, source, target, observer,
613        )
614    }
615
616    fn delete_observed(&mut self, id: u32, observer: ChangeObserver<'_>) -> Result<Link> {
617        crate::version_control::GenericVersionControlDecorator::delete_observed(self, id, observer)
618    }
619
620    fn all_links(&mut self) -> Vec<Link> {
621        self.all().into_iter().copied().collect()
622    }
623
624    fn search(&mut self, source: u32, target: u32) -> Option<u32> {
625        crate::version_control::GenericVersionControlDecorator::search(self, source, target)
626    }
627
628    fn get_or_create(&mut self, source: u32, target: u32) -> u32 {
629        crate::version_control::GenericVersionControlDecorator::get_or_create(self, source, target)
630            .expect("VersionControlDecorator::get_or_create failed in NamedTypeLinks bridge")
631    }
632
633    fn get_name(&mut self, id: u32) -> Result<Option<String>> {
634        NamedTypes::get_name(self.transactions_mut().inner_mut(), id)
635    }
636
637    fn set_name(&mut self, id: u32, name: &str) -> Result<u32> {
638        crate::version_control::GenericVersionControlDecorator::set_name(self, id, name)
639    }
640
641    fn get_by_name(&mut self, name: &str) -> Result<Option<u32>> {
642        NamedTypes::get_by_name(self.transactions_mut().inner_mut(), name)
643    }
644
645    fn remove_name(&mut self, id: u32) -> Result<()> {
646        crate::version_control::GenericVersionControlDecorator::remove_name(self, id)
647    }
648
649    fn save(&mut self) -> Result<()> {
650        crate::version_control::GenericVersionControlDecorator::save(self)
651    }
652}
653
654fn preview_recycling_query(
655    mut scratch: impl NamedTypeLinks,
656    processor: &crate::QueryProcessor,
657    restriction: &crate::lino_link::LinoLink,
658    substitution: &crate::lino_link::LinoLink,
659) -> Result<Option<HashSet<u32>>> {
660    processor.preview(&mut scratch, restriction, substitution)?;
661    Ok(Some(
662        scratch
663            .all_links()
664            .into_iter()
665            .map(|row| row.index)
666            .collect(),
667    ))
668}
669
670/// A borrowed store is a store: a layer can wrap `&mut store` and leave the
671/// store with its owner, as the permission policy does when it reads the
672/// types of the data through a [`TypesDecorator`](crate::types::TypesDecorator).
673impl<T: QueryAddress, L: NamedTypeLinks<T> + ?Sized> NamedTypeLinks<T> for &mut L {
674    fn query_limits(&self) -> Option<crate::query_options::QueryLimits> {
675        (**self).query_limits()
676    }
677    fn reserve_query_allocations(
678        &mut self,
679        processor: &crate::query_processor::GenericQueryProcessor<T>,
680        restriction: &crate::lino_link::LinoLink,
681        substitution: &crate::lino_link::LinoLink,
682    ) -> Result<Option<HashSet<T>>> {
683        (**self).reserve_query_allocations(processor, restriction, substitution)
684    }
685    fn create(&mut self, source: T, target: T) -> T {
686        (**self).create(source, target)
687    }
688
689    fn ensure_created(&mut self, id: T) -> T {
690        (**self).ensure_created(id)
691    }
692
693    fn try_ensure_created(&mut self, id: T) -> Result<T> {
694        (**self).try_ensure_created(id)
695    }
696
697    fn get_link(&mut self, id: T) -> Option<GenericLink<T>> {
698        (**self).get_link(id)
699    }
700
701    fn exists(&mut self, id: T) -> bool {
702        (**self).exists(id)
703    }
704
705    fn try_create(&mut self, source: T, target: T) -> Result<T> {
706        (**self).try_create(source, target)
707    }
708
709    fn try_get_or_create(&mut self, source: T, target: T) -> Result<T> {
710        (**self).try_get_or_create(source, target)
711    }
712
713    fn reference_exists(&mut self, id: T) -> bool {
714        (**self).reference_exists(id)
715    }
716
717    fn claims(&mut self, id: T) -> Option<bool> {
718        (**self).claims(id)
719    }
720
721    fn next_created_addresses(&mut self, count: usize) -> Result<Vec<T>> {
722        (**self).next_created_addresses(count)
723    }
724
725    fn update_observed(
726        &mut self,
727        id: T,
728        source: T,
729        target: T,
730        observer: ChangeObserver<'_, T>,
731    ) -> Result<GenericLink<T>> {
732        (**self).update_observed(id, source, target, observer)
733    }
734
735    fn delete_observed(
736        &mut self,
737        id: T,
738        observer: ChangeObserver<'_, T>,
739    ) -> Result<GenericLink<T>> {
740        (**self).delete_observed(id, observer)
741    }
742
743    fn all_links(&mut self) -> Vec<GenericLink<T>> {
744        (**self).all_links()
745    }
746
747    fn query_links(
748        &mut self,
749        index: Option<T>,
750        source: Option<T>,
751        target: Option<T>,
752    ) -> Vec<GenericLink<T>> {
753        (**self).query_links(index, source, target)
754    }
755
756    fn search(&mut self, source: T, target: T) -> Option<T> {
757        (**self).search(source, target)
758    }
759
760    fn get_or_create(&mut self, source: T, target: T) -> T {
761        (**self).get_or_create(source, target)
762    }
763
764    fn get_name(&mut self, id: T) -> Result<Option<String>> {
765        (**self).get_name(id)
766    }
767
768    fn set_name(&mut self, id: T, name: &str) -> Result<T> {
769        (**self).set_name(id, name)
770    }
771
772    fn get_by_name(&mut self, name: &str) -> Result<Option<T>> {
773        (**self).get_by_name(name)
774    }
775
776    fn remove_name(&mut self, id: T) -> Result<()> {
777        (**self).remove_name(id)
778    }
779
780    fn save(&mut self) -> Result<()> {
781        (**self).save()
782    }
783
784    fn get_or_create_named(&mut self, name: &str) -> Result<T> {
785        (**self).get_or_create_named(name)
786    }
787}
788
789/// The text of a name used as a reference. The notation decides how to delimit
790/// it, except for digits that would otherwise read as an address and for mixed
791/// quotes, whose literal form the C# port has always written.
792pub fn escape_lino_reference(reference: &str) -> String {
793    if reference.is_empty() {
794        return String::new();
795    }
796    if reference.parse::<u32>().is_ok() {
797        return format!("'{reference}'");
798    }
799    if reference.contains('\'') && reference.contains('"') {
800        return links_notation::encode_reference_literal(reference);
801    }
802    links_notation::LiNo::Ref(reference).to_string()
803}