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
use crate::{PanduckError, Result};
use notedown_ast::ASTNode;
use std::{
cmp::Ordering,
collections::{BTreeMap, BTreeSet},
fs::read_to_string,
path::Path,
rc::Rc,
};
pub struct ExtensionRegistrar {
arena: Vec<Rc<ExtensionHandler>>,
inner: BTreeMap<String, BTreeSet<Rc<ExtensionHandler>>>,
}
pub struct ExtensionHandler {
pub name: String,
pub parser: fn(&str) -> Result<ASTNode>,
pub try_extension: BTreeSet<String>,
}
impl ExtensionRegistrar {
pub fn insert(&mut self, h: ExtensionHandler) {
let handler = Rc::new(h);
self.arena.push(Rc::clone(&handler));
for i in &handler.try_extension {
let new = Rc::clone(&handler);
let ptr = self.inner.get_mut(i.as_str());
match ptr {
Some(s) => {
s.insert(new);
}
None => {
let mut s = BTreeSet::new();
s.insert(new);
self.inner.insert(i.to_owned(), s);
}
}
}
}
#[inline]
pub fn get(&self, ext: &str) -> BTreeSet<Rc<ExtensionHandler>> {
match self.inner.get(ext) {
Some(s) => s.to_owned(),
None => BTreeSet::new(),
}
}
pub fn parse_by_ext(&self, file: impl AsRef<Path>) -> Result<ASTNode> {
let ext = file.as_ref().extension().and_then(|t| t.to_str()).ok_or(())?;
let input = &read_to_string(file.as_ref())?;
for t in self.get(ext) {
let parse = t.parser;
match parse(input) {
Ok(s) => return Ok(s),
Err(_) => continue,
}
}
let error = PanduckError::unsupported_file(ext).set_path(file);
return Err(error);
}
}
impl Eq for ExtensionHandler {}
impl PartialEq<Self> for ExtensionHandler {
fn eq(&self, other: &ExtensionHandler) -> bool {
self.name.eq(&other.name)
}
}
impl PartialOrd<Self> for ExtensionHandler {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.name.partial_cmp(&other.name)
}
}
impl Ord for ExtensionHandler {
fn cmp(&self, other: &Self) -> Ordering {
self.name.cmp(&other.name)
}
}