asimov-module 25.1.0

ASIMOV Software Development Kit (SDK) for Rust
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
// This is free and unencumbered software released into the public domain.

use crate::{ModuleManifest, resolve::error::InsertManifestError};
use alloc::{
    collections::{btree_map::BTreeMap, btree_set::BTreeSet},
    rc::Rc,
    string::{String, ToString},
    vec::Vec,
};
use core::{borrow::Borrow, convert::Infallible};
use error::UrlParseError;

pub mod error;

#[derive(Clone, Debug, Default)]
pub struct Resolver {
    modules: BTreeMap<String, Rc<Module>>,
    file_extensions: BTreeMap<String, Vec<Rc<Module>>>,
    content_types: BTreeMap<mime::Mime, Vec<Rc<Module>>>,
    nodes: slab::Slab<Node>,
    roots: BTreeMap<Sect, usize>,
}

impl Resolver {
    pub fn new() -> Self {
        Resolver::default()
    }

    pub fn resolve(&self, url: &str) -> Result<Vec<Rc<Module>>, UrlParseError> {
        let input = split_url(url)?;

        // `results` is a set of `(path_length, Module)` items.
        // `path_length` first so they get sorted by the length of matching path.
        // We reverse iter this later so that longer paths are returned first.
        let mut results: BTreeSet<(usize, Rc<Module>)> = BTreeSet::new();

        if matches!(input.first(), Some(Sect::Protocol(proto)) if proto == "file") {
            if let Some(Sect::Path(filename)) = input.last() {
                if let Some((_, ext)) = filename.split_once(".") {
                    self.file_extensions
                        .get(ext)
                        .into_iter()
                        .flatten()
                        .for_each(|module| {
                            results.insert((0, module.clone()));
                        });
                }
            }
        }

        let with_freemove = |node_idx: usize| {
            // Return the node ID
            core::iter::once(node_idx)
                // And the destination ID after following a `FreeMove` path from the node
                .chain(self.nodes[node_idx].paths.get(&Sect::FreeMove).copied())
        };

        // Initialize with start states that match the first input
        let start_states: BTreeSet<usize> = self
            .roots
            .iter()
            .filter_map(|(path, &node_idx)| path.matches_input(&input[0]).then_some(node_idx))
            .collect();

        let final_states = if input.len() == 1 {
            // There is no further input, just get free moves from the start_states
            start_states.into_iter().flat_map(with_freemove).collect()
        } else {
            // Process remaining input
            input[1..].iter().fold(start_states, |states, sect| {
                states
                    .into_iter()
                    .flat_map(|node_idx| &self.nodes[node_idx].paths)
                    .filter_map(|(path, &node_idx)| path.matches_input(sect).then_some(node_idx))
                    .flat_map(with_freemove)
                    .collect()
            })
        };

        // Collect all modules from final states
        for node in final_states.iter().map(|&idx| &self.nodes[idx]) {
            for module in &node.modules {
                results.insert((node.path_length, module.clone()));
            }
        }

        Ok(results
            .into_iter()
             // The `results` set is sorted by path_length.
             // Reverse to prefer longer matches.
            .rev()
            .map(|(_, module)| module)
            .collect())
    }

    pub fn resolve_content_type(&self, content_type: &mime::Mime) -> Vec<Rc<Module>> {
        let mut modules: BTreeSet<Rc<Module>> = BTreeSet::new();

        let exact = self.content_types.get(content_type);
        modules.extend(exact.into_iter().flatten().cloned());

        let toptype = content_type.type_();
        let star_sub = alloc::format!("{toptype}/*");
        if let Ok(star_sub) = star_sub.parse() {
            let matches = self.content_types.get(&star_sub);
            modules.extend(matches.into_iter().flatten().cloned());
        }

        let starstar = self.content_types.get(&mime::STAR_STAR);
        modules.extend(starstar.into_iter().flatten().cloned());

        modules.into_iter().collect()
    }

    pub fn insert_manifest(
        &mut self,
        manifest: &ModuleManifest,
    ) -> Result<(), InsertManifestError> {
        for protocol in &manifest.handles.url_protocols {
            self.insert_protocol(&manifest.name, protocol).ok();
        }
        for prefix in &manifest.handles.url_prefixes {
            self.insert_prefix(&manifest.name, prefix)?;
        }
        for pattern in &manifest.handles.url_patterns {
            self.insert_pattern(&manifest.name, pattern)?;
        }
        for file_extension in &manifest.handles.file_extensions {
            self.insert_file_extension(&manifest.name, file_extension)
                .ok();
        }
        for content_type in &manifest.handles.content_types {
            let content_type = content_type.parse()?;
            self.insert_content_type(&manifest.name, content_type).ok();
        }
        Ok(())
    }

    pub fn insert_content_type(
        &mut self,
        module: &str,
        content_type: mime::Mime,
    ) -> Result<(), Infallible> {
        let module = self.add_module(module);

        self.content_types
            .entry(content_type)
            .or_default()
            .push(module);

        Ok(())
    }
    pub fn insert_file_extension(
        &mut self,
        module: &str,
        file_extension: &str,
    ) -> Result<(), Infallible> {
        let module = self.add_module(module);

        let ext = file_extension
            .strip_prefix(".")
            .unwrap_or(file_extension)
            .to_string();

        self.file_extensions.entry(ext).or_default().push(module);

        Ok(())
    }
    pub fn insert_protocol(&mut self, module: &str, protocol: &str) -> Result<(), Infallible> {
        let path = &[Sect::Protocol(protocol.to_string()), Sect::FreeMove];
        let module = self.add_module(module);
        let node_idx = self.get_or_create_node(path);

        // Add a free move back to itself from the `FreeMove` node. (represents a protocol as a prefix):
        self.nodes[node_idx].paths.insert(Sect::FreeMove, node_idx);
        self.nodes[node_idx].modules.insert(module);

        Ok(())
    }
    pub fn insert_prefix(&mut self, module: &str, prefix: &str) -> Result<(), UrlParseError> {
        let mut path = split_url(prefix)?;
        // Add a `FreeMove` node at the end of the path to separate the prefix from
        // patterns at the same node
        path.push(Sect::FreeMove);
        let module = self.add_module(module);
        let node_idx = self.get_or_create_node(&path);

        // Add a free move back to itself from the `FreeMove` node. Enables matching
        // zero-or-more of anything:
        self.nodes[node_idx].paths.insert(Sect::FreeMove, node_idx);
        self.nodes[node_idx].modules.insert(module);

        Ok(())
    }
    pub fn insert_pattern(&mut self, module: &str, pattern: &str) -> Result<(), UrlParseError> {
        let path: Vec<Sect> = split_url(pattern)?
            .into_iter()
            .map(Sect::into_pattern)
            .collect();
        let module = self.add_module(module);
        let node_idx = self.get_or_create_node(&path);

        self.nodes[node_idx].modules.insert(module);

        Ok(())
    }

    #[cfg(all(feature = "std", feature = "serde"))]
    pub fn try_from_dir(path: impl AsRef<std::path::Path>) -> Result<Self, error::FromDirError> {
        use error::FromDirError;

        let path = path.as_ref();

        let dir = std::fs::read_dir(path).map_err(|source| FromDirError::ManifestDirIo {
            path: path.into(),
            source,
        })?;

        let mut resolver = Resolver::new();

        for entry in dir {
            let entry = entry.map_err(|source| FromDirError::ManifestDirIo {
                path: path.into(),
                source,
            })?;
            if !entry.file_type().map(|ft| ft.is_file()).unwrap_or(false) {
                continue;
            }
            let filename = entry.file_name();
            let filename = filename.to_string_lossy();
            if !filename.ends_with(".yaml") && !filename.ends_with(".yml") {
                continue;
            }
            let path = entry.path();
            let file = std::fs::File::open(&path).map_err(|source| FromDirError::ManifestIo {
                path: path.clone(),
                source,
            })?;

            let manifest =
                serde_yaml_ng::from_reader(file).map_err(|source| FromDirError::Parse {
                    path: path.clone(),
                    source,
                })?;
            resolver
                .insert_manifest(&manifest)
                .map_err(|source| FromDirError::Insert {
                    path: path.clone(),
                    source,
                })?;
        }

        Ok(resolver)
    }

    pub fn try_from_iter<I, T>(mut iter: I) -> Result<Self, InsertManifestError>
    where
        I: Iterator<Item = T>,
        T: Borrow<ModuleManifest>,
    {
        iter.try_fold(Resolver::default(), |mut r, m| {
            r.insert_manifest(m.borrow())?;
            Ok(r)
        })
    }

    fn get_or_create_node(&mut self, path: &[Sect]) -> usize {
        // Get or create the root node
        let root_idx = *self
            .roots
            .entry(path[0].clone())
            .or_insert_with(|| self.nodes.insert(Node::default()));

        path.iter()
            .enumerate()
            .skip(1) // first one was already inserted to `self.roots`
            .fold(root_idx, |cur_idx, (path_length, sect)| {
                match (self.nodes[cur_idx].paths.get(sect), sect) {
                    (Some(&idx), _sect) => idx,
                    (None, Sect::WildcardDomain) => {
                        // If the sect is a wildcard domain add a link to self, this will also match multiple subdomains.
                        self.nodes[cur_idx].paths.insert(sect.clone(), cur_idx);
                        cur_idx
                    },
                    (None, sect) => {
                        // Create a new node
                        let new_node_idx = self.nodes.insert(Node {
                            path_length,
                            ..Default::default()
                        });

                        // Add the transition from current node to new node
                        self.nodes[cur_idx].paths.insert(sect.clone(), new_node_idx);
                        new_node_idx
                    },
                }
            })
    }

    fn add_module(&mut self, name: &str) -> Rc<Module> {
        let name = name.to_string();
        self.modules
            .entry(name.clone())
            .or_insert_with(|| Rc::new(Module { name }))
            .clone()
    }
}

impl TryFrom<&[ModuleManifest]> for Resolver {
    type Error = InsertManifestError;

    fn try_from(value: &[ModuleManifest]) -> Result<Self, Self::Error> {
        Resolver::try_from_iter(value.iter())
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Module {
    pub name: String,
}

#[derive(Clone, Debug, Default)]
struct Node {
    paths: BTreeMap<Sect, usize>,
    modules: BTreeSet<Rc<Module>>,
    path_length: usize,
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
enum Sect {
    /// `https` from `https://example.org/`, matches the protocol (a.k.a. scheme) of an URL
    Protocol(String),
    /// `org` and `example` from `https://example.org/`, matches a single literal subdomain
    Domain(String),
    /// `*` from `https://*.example.org/`, matches zero-or-more subdomains
    WildcardDomain,
    /// `file` and `path` from `https://example.org/file/path`, match literal path segments
    Path(String),
    /// `:name` from `https://example.org/file/:name`, matches any single path segment
    WildcardPath,
    /// `q` from `https://example.org/?q=example`, matches a parameter name
    QueryParamName(String),
    /// `example` from `https://example.org/?q=example`, matches a literal parameter value
    QueryParamValue(String),
    /// `:query` from `https://example.org/?q=:query`, matches any query param value
    WildcardQueryParamValue,
    /// Matches a single section of any kind
    FreeMove,
}

impl Sect {
    /// Transform a sect that matches a pattern format to a wildcard.
    /// - If a domain section is "*", make it a wildcard domain pattern
    /// - If a path section begins with ":" ("/:foo/:bar"), make it a wildcard path pattern
    /// - If the value of a query parameter begins with ":" ("q=:query"), make it a wildcard query param pattern
    pub fn into_pattern(self) -> Self {
        match self {
            Sect::Domain(p) if p == "*" => Sect::WildcardDomain,
            Sect::Path(p) if p.starts_with(':') => Sect::WildcardPath,
            Sect::QueryParamValue(p) if p.starts_with(':') => Sect::WildcardQueryParamValue,
            _ => self,
        }
    }

    fn matches_input(&self, input: &Self) -> bool {
        use Sect::*;
        match (self, input) {
            (a, b) if a == b => true,
            (WildcardDomain, Domain(_)) => true,
            (WildcardPath, Path(_)) => true,
            (WildcardQueryParamValue, QueryParamValue(_)) => true,
            // As a special case if the path section is a `FreeMove` then always accept it.
            (FreeMove, _) => true,
            _ => false,
        }
    }
}

/// Split and URL into sections that we care about. This is effectively a tokenizer.
fn split_url(url: &str) -> Result<Vec<Sect>, UrlParseError> {
    if url.is_empty() {
        return Err(UrlParseError::EmptyUrl);
    }

    let mut res = Vec::new();

    if !url.contains(':') {
        res.push(Sect::Protocol(url.into()));
        return Ok(res);
    }

    let url: url::Url = url.parse().map_err(|e| UrlParseError::InvalidUrl {
        url: url.to_string(),
        source: e,
    })?;

    let proto = url.scheme();
    res.push(Sect::Protocol(proto.into()));

    if let Some(host) = url.host_str() {
        let mut host_parts: Vec<&str> = host.split('.').rev().collect();

        if (proto == "http" || proto == "https")
            && host_parts.last().is_some_and(|last| *last == "www")
        {
            // ignore a "www." at the beginning of the domain. The domain has been reversed so we're popping the last element
            let _www = host_parts.pop();
        }

        for part in host_parts {
            res.push(Sect::Domain(part.into()));
        }
    }

    if url.cannot_be_a_base() {
        res.push(Sect::Path(url.path().into()))
    } else if let Some(path_parts) = url.path_segments() {
        for part in path_parts {
            if part.is_empty() {
                continue;
            }
            res.push(Sect::Path(part.into()));
        }
    }

    for (k, v) in url.query_pairs() {
        res.push(Sect::QueryParamName(k.into()));
        if !v.is_empty() {
            res.push(Sect::QueryParamValue(v.into()));
        }
    }

    Ok(res)
}

#[cfg(test)]
mod test {
    use super::*;

    extern crate std;
    use std::{eprintln, vec};

    #[test]
    fn matching() {
        let mut resolver = Resolver::default();

        resolver.insert_protocol("near", "near").unwrap();
        resolver
            .insert_pattern("near-account", "near://account/:id")
            .unwrap();
        resolver.insert_pattern("near-tx", "near://tx/:id").unwrap();
        resolver
            .insert_prefix("google", "https://google.com/search?q=")
            .unwrap();
        resolver.insert_prefix("x", "https://x.com/").unwrap();
        resolver
            .insert_pattern("linkedin", "https://*.linkedin.com/in/:account/test")
            .unwrap();
        resolver
            .insert_pattern("youtube", "https://youtube.com/watch?v=:v")
            .unwrap();
        resolver
            .insert_pattern("subdomains", "https://*.baz.com/")
            .unwrap();
        resolver.insert_prefix("data", "data:text/plain").unwrap();
        resolver.insert_prefix("fs", "file://").unwrap();
        resolver.insert_prefix("fs2", "file:///2").unwrap();
        resolver.insert_file_extension("txt-ext", "txt").unwrap();
        resolver.insert_file_extension("zip-ext", ".zip").unwrap(); // leading dot should work just as well
        resolver.insert_file_extension("tar-ext", "tar.gz").unwrap();

        eprintln!("{resolver:#?}");

        let tests = vec![
            ("near", "near"),
            ("near://tx/1234", "near-tx"),
            ("near://account/1234", "near-account"),
            ("near://other/1234", "near"),
            ("https://google.com/search?q=foobar", "google"),
            ("https://x.com/foobar", "x"),
            ("https://www.linkedin.com/in/foobar/test", "linkedin"),
            ("https://youtube.com/watch?v=foobar", "youtube"),
            ("https://multiple.subdomains.foo.bar.baz.com/", "subdomains"),
            ("data:text/plain?Hello+World", "data"),
            ("file:///foo/bar/baz", "fs"),
            ("file:/archive.zip", "zip-ext"),
            ("file:///2/foo", "fs2"),
            ("file:///foobar.txt", "txt-ext"),
            ("file:///foobar.tar.gz", "tar-ext"),
        ];

        for (input, want) in tests {
            assert_eq!(
                resolver
                    .resolve(input)
                    .expect("resolve succeeds")
                    .iter()
                    .find(|out| out.name == want)
                    .unwrap_or_else(|| panic!(
                        "the wanted result should be returned, input={input} want={want}"
                    ))
                    .name,
                want
            );
        }
    }

    #[test]
    fn content_type() {
        let mut resolver = Resolver::default();
        resolver
            .insert_content_type("starstar", mime::STAR_STAR)
            .unwrap();
        resolver
            .insert_content_type("textstar", mime::TEXT_STAR)
            .unwrap();
        resolver
            .insert_content_type("textplain", mime::TEXT_PLAIN)
            .unwrap();

        let results = resolver.resolve_content_type(&mime::TEXT_PLAIN);

        assert!(
            results.iter().any(|out| out.name == "starstar"),
            "*/* should match"
        );
        assert!(
            results.iter().any(|out| out.name == "textstar"),
            "text/* should match"
        );
        assert!(
            results.iter().any(|out| out.name == "textplain"),
            "text/plain should match"
        );

        let results = resolver.resolve_content_type(&mime::APPLICATION_JSON);
        assert!(
            results.iter().any(|out| out.name == "starstar"),
            "*/* should match"
        );
        assert!(
            !results.iter().any(|out| out.name == "textstar"),
            "text/* shouldn't match"
        );
        assert!(
            !results.iter().any(|out| out.name == "textplain"),
            "text/plain shouldn't match"
        );
    }

    #[test]
    fn prefix_doesnt_turn_pattern_to_prefix() {
        let mut resolver = Resolver::new();

        resolver
            .insert_pattern("pattern", "https://foobar.com/")
            .unwrap();
        eprintln!("{resolver:#?}");

        let results = resolver.resolve("https://foobar.com/").unwrap();
        eprintln!("{results:?}");
        assert!(
            results
                .first()
                .is_some_and(|module| module.name == "pattern"),
            "the pattern should match"
        );

        let results = resolver.resolve("https://foobar.com/more").unwrap();
        eprintln!("{results:?}");
        assert!(results.is_empty(), "the pattern shouldn't be a prefix");

        resolver
            .insert_prefix("prefix", "https://foobar.com/")
            .unwrap();
        eprintln!("{resolver:#?}");

        let results = resolver.resolve("https://foobar.com/").unwrap();
        eprintln!("{results:?}");
        assert!(results.len() == 2, "both items should match");

        let results = resolver.resolve("https://foobar.com/more").unwrap();
        eprintln!("{results:?}");
        assert!(results.len() == 1, "only the prefix should match");
        assert!(
            results
                .first()
                .is_some_and(|module| module.name == "prefix"),
            "only the prefix should match"
        );
    }

    #[test]
    fn longer_matches_are_returned_first() {
        let mut resolver = Resolver::new();
        resolver.insert_prefix("fs", "file://").unwrap();
        resolver.insert_prefix("fs2", "file:///path").unwrap();
        resolver.insert_prefix("fs3", "file:///path/to").unwrap();
        resolver.insert_file_extension("txt-ext", "txt").unwrap();

        let mut it = resolver
            .resolve("file:///path/to/file.txt")
            .unwrap()
            .into_iter();
        assert_eq!("fs3", it.next().unwrap().name);
        assert_eq!("fs2", it.next().unwrap().name);
        assert_eq!("fs", it.next().unwrap().name);
        assert_eq!("txt-ext", it.next().unwrap().name);
        assert_eq!(None, it.next());

        let mut it = resolver.resolve("file:///file.txt").unwrap().into_iter();
        assert_eq!("fs", it.next().unwrap().name);
        assert_eq!("txt-ext", it.next().unwrap().name);
        assert_eq!(None, it.next());

        let mut it = resolver.resolve("https://example.org").unwrap().into_iter();
        assert_eq!(None, it.next());
    }
}