link-cli 0.2.10

A CLI tool and reusable library for links manipulation backed by a LiNo-notation doublet storage engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Unicode string and name storage backed by doublet links.
//!
//! This mirrors the C# `UnicodeStringStorage<uint>` constructor pipeline:
//! pinned types, `BalancedVariantConverter`, target matchers, Unicode symbol
//! converters, string/sequence converters, right-sequence walking, and
//! `NamedLinks`.

use std::cell::RefCell;

use anyhow::{bail, Result};

use crate::hybrid_reference::{external_reference, external_reference_value};
use crate::link_storage::LinkStorage;
use crate::named_links::NamedLinks;
use crate::pinned_types::PinnedTypes;
use crate::sequences::{
    AddressToRawNumberConverter, BalancedVariantConverter, CachingConverterDecorator,
    CharToUnicodeSymbolConverter, RawNumberToAddressConverter, RightSequenceWalker,
    StringToUnicodeSequenceConverter, TargetMatcher, UnicodeSequenceToStringConverter,
    UnicodeSymbolToCharConverter,
};

/// Link-backed Unicode string storage with C# pinned type layout.
pub struct UnicodeStringStorage<'a> {
    links: &'a mut LinkStorage,
    type_type: u32,
    unicode_symbol_type: u32,
    unicode_sequence_type: u32,
    string_type: u32,
    empty_string_type: u32,
    name_type: u32,
    address_to_number_converter: AddressToRawNumberConverter,
    number_to_address_converter: RawNumberToAddressConverter,
    balanced_variant_converter: BalancedVariantConverter,
    unicode_symbol_criterion_matcher: TargetMatcher,
    unicode_sequence_criterion_matcher: TargetMatcher,
    char_to_unicode_symbol_converter: CharToUnicodeSymbolConverter,
    unicode_symbol_to_char_converter: UnicodeSymbolToCharConverter,
    string_to_unicode_sequence_converter: StringToUnicodeSequenceConverter,
    sequence_walker: RightSequenceWalker,
    unicode_sequence_to_string_converter: UnicodeSequenceToStringConverter,
    string_to_unicode_sequence_cache: CachingConverterDecorator<String, u32>,
    unicode_sequence_to_string_cache: RefCell<CachingConverterDecorator<u32, String>>,
}

impl<'a> UnicodeStringStorage<'a> {
    pub fn new(links: &'a mut LinkStorage) -> Result<Self> {
        let (
            type_type,
            unicode_symbol_type,
            unicode_sequence_type,
            string_type,
            empty_string_type,
            name_type,
        ) = {
            let mut pinned_types = PinnedTypes::new(links);
            (
                pinned_types.next_type()?,
                pinned_types.next_type()?,
                pinned_types.next_type()?,
                pinned_types.next_type()?,
                pinned_types.next_type()?,
                pinned_types.next_type()?,
            )
        };

        let address_to_number_converter = AddressToRawNumberConverter::new();
        let number_to_address_converter = RawNumberToAddressConverter::new();
        let balanced_variant_converter = BalancedVariantConverter::new();
        let unicode_symbol_criterion_matcher = TargetMatcher::new(unicode_symbol_type);
        let unicode_sequence_criterion_matcher = TargetMatcher::new(unicode_sequence_type);
        let char_to_unicode_symbol_converter =
            CharToUnicodeSymbolConverter::new(address_to_number_converter, unicode_symbol_type);
        let unicode_symbol_to_char_converter = UnicodeSymbolToCharConverter::new(
            number_to_address_converter,
            unicode_symbol_criterion_matcher,
        );
        let string_to_unicode_sequence_converter = StringToUnicodeSequenceConverter::new(
            char_to_unicode_symbol_converter,
            balanced_variant_converter,
            unicode_sequence_type,
        );
        let sequence_walker = RightSequenceWalker::new(unicode_symbol_criterion_matcher);
        let unicode_sequence_to_string_converter = UnicodeSequenceToStringConverter::new(
            unicode_sequence_criterion_matcher,
            sequence_walker,
            unicode_symbol_to_char_converter,
            unicode_sequence_type,
        );

        let mut storage = Self {
            links,
            type_type,
            unicode_symbol_type,
            unicode_sequence_type,
            string_type,
            empty_string_type,
            name_type,
            address_to_number_converter,
            number_to_address_converter,
            balanced_variant_converter,
            unicode_symbol_criterion_matcher,
            unicode_sequence_criterion_matcher,
            char_to_unicode_symbol_converter,
            unicode_symbol_to_char_converter,
            string_to_unicode_sequence_converter,
            sequence_walker,
            unicode_sequence_to_string_converter,
            string_to_unicode_sequence_cache: CachingConverterDecorator::new(),
            unicode_sequence_to_string_cache: RefCell::new(CachingConverterDecorator::new()),
        };

        storage.set_name(type_type, "Type")?;
        storage.set_name(unicode_symbol_type, "UnicodeSymbol")?;
        storage.set_name(unicode_sequence_type, "UnicodeSequence")?;
        storage.set_name(string_type, "String")?;
        storage.set_name(empty_string_type, "EmptyString")?;
        storage.set_name(name_type, "Name")?;

        Ok(storage)
    }

    pub fn links_mut(&mut self) -> &mut LinkStorage {
        self.links
    }

    pub fn into_named_links(self) -> NamedLinks<'a> {
        NamedLinks::from_storage(self)
    }

    pub fn type_type(&self) -> u32 {
        self.type_type
    }

    pub fn unicode_symbol_type(&self) -> u32 {
        self.unicode_symbol_type
    }

    pub fn unicode_sequence_type(&self) -> u32 {
        self.unicode_sequence_type
    }

    pub fn string_type(&self) -> u32 {
        self.string_type
    }

    pub fn empty_string_type(&self) -> u32 {
        self.empty_string_type
    }

    pub fn name_type(&self) -> u32 {
        self.name_type
    }

    pub fn address_to_number_converter(&self) -> AddressToRawNumberConverter {
        self.address_to_number_converter
    }

    pub fn number_to_address_converter(&self) -> RawNumberToAddressConverter {
        self.number_to_address_converter
    }

    pub fn balanced_variant_converter(&self) -> BalancedVariantConverter {
        self.balanced_variant_converter
    }

    pub fn unicode_symbol_criterion_matcher(&self) -> TargetMatcher {
        self.unicode_symbol_criterion_matcher
    }

    pub fn unicode_sequence_criterion_matcher(&self) -> TargetMatcher {
        self.unicode_sequence_criterion_matcher
    }

    pub fn char_to_unicode_symbol_converter(&self) -> CharToUnicodeSymbolConverter {
        self.char_to_unicode_symbol_converter
    }

    pub fn unicode_symbol_to_char_converter(&self) -> UnicodeSymbolToCharConverter {
        self.unicode_symbol_to_char_converter
    }

    pub fn string_to_unicode_sequence_converter(&self) -> StringToUnicodeSequenceConverter {
        self.string_to_unicode_sequence_converter
    }

    pub fn sequence_walker(&self) -> RightSequenceWalker {
        self.sequence_walker
    }

    pub fn unicode_sequence_to_string_converter(&self) -> UnicodeSequenceToStringConverter {
        self.unicode_sequence_to_string_converter
    }

    pub fn create_string(&mut self, content: &str) -> Result<u32> {
        let string_sequence = self.get_string_sequence(content);
        Ok(self.links.get_or_create(self.string_type, string_sequence))
    }

    pub fn get_string(&self, string_value: u32) -> Result<String> {
        let mut current = string_value;
        for _ in 0..3 {
            let Some(link) = self.links.get(current) else {
                break;
            };
            if link.source == self.string_type {
                return if link.target == self.empty_string_type {
                    Ok(String::new())
                } else {
                    self.unicode_sequence_to_string(link.target)
                };
            }
            current = link.target;
        }
        bail!("The passed link does not contain a string.")
    }

    pub fn unicode_sequence_code_units(&self, string_value: u32) -> Result<Vec<u16>> {
        let sequence = self.unwrap_string_sequence(string_value)?;
        if sequence == self.empty_string_type {
            return Ok(Vec::new());
        }
        if !self
            .unicode_sequence_criterion_matcher
            .is_matched(self.links, sequence)
        {
            bail!("Link {sequence} is not a Unicode sequence.");
        }
        let unicode_sequence = self
            .links
            .get(sequence)
            .ok_or_else(|| anyhow::anyhow!("Unicode sequence link {sequence} does not exist."))?;

        self.sequence_walker
            .walk(self.links, unicode_sequence.source)
            .into_iter()
            .map(|symbol| {
                self.unicode_symbol_to_char_converter
                    .convert(self.links, symbol)
            })
            .collect()
    }

    pub fn get_types(&self) -> Vec<u32> {
        self.links
            .query(None, Some(self.type_type), None)
            .into_iter()
            .map(|link| link.index)
            .collect()
    }

    pub fn is_type(&self, address: u32) -> bool {
        self.links
            .get(address)
            .is_some_and(|link| link.source == self.type_type)
    }

    pub fn get_or_create_type(&mut self, name: &str) -> Result<u32> {
        if let Some(existing) = self.get_by_name(name)? {
            return Ok(existing);
        }

        let type_link = self.links.create(0, 0);
        self.links.update(type_link, self.type_type, type_link)?;
        self.set_name(type_link, name)?;
        Ok(type_link)
    }

    pub fn set_name_for_external_reference(&mut self, link: u32, name: &str) -> Result<u32> {
        self.set_name(external_reference(link), name)
    }

    pub fn get_name_by_external_reference(&self, link: u32) -> Result<Option<String>> {
        self.get_name(external_reference(link))
    }

    /// The external reference named `name`, if any.
    ///
    /// A name link can be shared by several holders, so this looks for the
    /// external reference among *all* of them instead of only inspecting the
    /// first: [`UnicodeStringStorage::new`] names the pinned types, so a user
    /// link named `Type`, `Name`, `String`, `UnicodeSymbol`,
    /// `UnicodeSequence` or `EmptyString` always shares its name link with a
    /// pinned type.
    pub fn get_external_reference_by_name(&mut self, name: &str) -> Result<Option<u32>> {
        Ok(self
            .name_holders(name)?
            .into_iter()
            .find_map(external_reference_value))
    }

    pub fn remove_name_by_external_reference(&mut self, external_reference_id: u32) -> Result<()> {
        self.remove_name(external_reference(external_reference_id))
    }

    pub fn set_name(&mut self, link: u32, name: &str) -> Result<u32> {
        let name_sequence = self.create_string(name)?;
        let name_link = self.links.get_or_create(self.name_type, name_sequence);
        Ok(self.links.get_or_create(link, name_link))
    }

    /// The name of `link`, or `None` when it has none.
    ///
    /// Name pairs are visited by address so that a link carrying more than one
    /// name always reports the same one.
    pub fn get_name(&self, link: u32) -> Result<Option<String>> {
        for (_, name_candidate) in self.name_pairs_of(link) {
            let Some(candidate) = self.links.get(name_candidate) else {
                continue;
            };
            if candidate.source == self.name_type {
                return self.get_string(candidate.target).map(Some);
            }
        }
        Ok(None)
    }

    /// The lowest-addressed link named `name`, or `None` when the name is
    /// unused. See [`Self::get_external_reference_by_name`] for the
    /// external-reference variant.
    pub fn get_by_name(&mut self, name: &str) -> Result<Option<u32>> {
        Ok(self.name_holders(name)?.into_iter().next())
    }

    /// Every link that carries `name`, ordered by the address of the name pair
    /// that binds it.
    ///
    /// Nothing stops two links from sharing one name link, and the pinned type
    /// names created by [`UnicodeStringStorage::new`] make that the normal case
    /// for a handful of reserved names. Reading only the first match out of
    /// [`LinkStorage::query`] — which iterates a hash map — made the answer
    /// depend on hash order; ordering by address makes it reproducible, exactly
    /// like [`LinkStorage::search`] does for duplicate doublets.
    fn name_holders(&mut self, name: &str) -> Result<Vec<u32>> {
        let name_sequence = self.create_string(name)?;
        let Some(name_link) = self.links.search(self.name_type, name_sequence) else {
            return Ok(Vec::new());
        };

        let mut holders = self
            .links
            .query(None, None, Some(name_link))
            .into_iter()
            .map(|link| (link.index, link.source))
            .collect::<Vec<_>>();
        holders.sort_unstable();
        Ok(holders.into_iter().map(|(_, source)| source).collect())
    }

    /// The `(name pair address, name candidate)` pairs anchored at `link`,
    /// ordered by address.
    fn name_pairs_of(&self, link: u32) -> Vec<(u32, u32)> {
        let mut pairs = self
            .links
            .query(None, Some(link), None)
            .into_iter()
            .map(|pair| (pair.index, pair.target))
            .collect::<Vec<_>>();
        pairs.sort_unstable();
        pairs
    }

    pub fn remove_name(&mut self, link: u32) -> Result<()> {
        let name_pairs = self.name_pairs_of(link);

        for (name_pair, name_candidate) in name_pairs {
            let Some(candidate) = self.links.get(name_candidate).copied() else {
                continue;
            };
            if candidate.source != self.name_type {
                continue;
            }

            if self.links.exists(name_pair) {
                self.links.delete(name_pair)?;
            }

            let still_used = self
                .links
                .query(None, None, Some(name_candidate))
                .into_iter()
                .any(|usage| usage.index != name_pair);
            if !still_used && self.links.exists(name_candidate) {
                self.links.delete(name_candidate)?;
            }
        }

        Ok(())
    }

    fn get_string_sequence(&mut self, content: &str) -> u32 {
        if content.is_empty() {
            self.empty_string_type
        } else {
            self.string_to_unicode_sequence(content)
        }
    }

    fn string_to_unicode_sequence(&mut self, content: &str) -> u32 {
        let input = content.to_string();
        if let Some(cached) = self.string_to_unicode_sequence_cache.get(&input) {
            return cached;
        }

        let converter = self.string_to_unicode_sequence_converter;
        let sequence = converter.convert(self.links, content);
        self.string_to_unicode_sequence_cache
            .insert(input, sequence)
    }

    fn unicode_sequence_to_string(&self, sequence: u32) -> Result<String> {
        if let Some(cached) = self
            .unicode_sequence_to_string_cache
            .borrow()
            .get(&sequence)
        {
            return Ok(cached);
        }

        let output = self
            .unicode_sequence_to_string_converter
            .convert(self.links, sequence)?;
        self.unicode_sequence_to_string_cache
            .borrow_mut()
            .insert(sequence, output.clone());
        Ok(output)
    }

    fn unwrap_string_sequence(&self, string_value: u32) -> Result<u32> {
        let mut current = string_value;
        for _ in 0..3 {
            let Some(link) = self.links.get(current) else {
                break;
            };
            if link.source == self.string_type {
                return Ok(link.target);
            }
            current = link.target;
        }
        bail!("The passed link does not contain a string.")
    }
}