very elegant extension API and parts of it used in std as POC

This commit is contained in:
2024-07-01 20:11:22 +02:00
parent 93867e40c6
commit fc8441f080
63 changed files with 2040 additions and 925 deletions

View File

@@ -1,47 +1,30 @@
use std::sync::Arc;
use std::num::NonZeroU16;
use hashbrown::HashMap;
use orchid_api::error::ProjResult;
use orchid_api::vfs::{EagerVfs, Loaded, VfsId};
use orchid_base::intern::{intern, Token};
use orchid_base::interner::intern;
use orchid_base::name::PathSlice;
use substack::Substack;
use trait_set::trait_set;
pub trait VirtFS: Send + Sync + 'static {
fn load(&self, path: &PathSlice) -> ProjResult<Loaded>;
}
trait_set! {
pub trait RecFsHandler<E> = FnMut(Substack<Token<String>>, &Arc<dyn VirtFS>) -> Result<(), E>;
}
pub enum DeclFs {
Lazy(Arc<dyn VirtFS>),
Mod(HashMap<Token<String>, DeclFs>),
Lazy(&'static dyn VirtFS),
Mod(&'static [(&'static str, DeclFs)]),
}
impl DeclFs {
pub fn module(entries: impl IntoIterator<Item = (&'static str, Self)>) -> Self {
Self::Mod(entries.into_iter().map(|(k, v)| (intern(k), v)).collect())
}
fn rec<E>(&self, path: Substack<Token<String>>, f: &mut impl RecFsHandler<E>) -> Result<(), E> {
match self {
DeclFs::Lazy(fs) => f(path, fs),
DeclFs::Mod(entries) => entries.iter().try_for_each(|(k, v)| v.rec(path.push(k.clone()), f)),
}
}
pub fn recurse<E>(&self, f: &mut impl RecFsHandler<E>) -> Result<(), E> {
self.rec(Substack::Bottom, f)
}
pub fn to_api_rec(&self, vfses: &mut HashMap<VfsId, Arc<dyn VirtFS>>) -> EagerVfs {
pub fn to_api_rec(&self, vfses: &mut HashMap<VfsId, &'static dyn VirtFS>) -> EagerVfs {
match self {
DeclFs::Lazy(fs) => {
let id = vfses.len() as VfsId;
vfses.insert(id, fs.clone());
let vfsc: u16 = vfses.len().try_into().expect("too many vfses (more than u16::MAX)");
let id: VfsId = NonZeroU16::new(vfsc + 1).unwrap();
vfses.insert(id, *fs);
EagerVfs::Lazy(id)
},
DeclFs::Mod(children) => EagerVfs::Eager(
children.into_iter().map(|(k, v)| (k.marker(), v.to_api_rec(vfses))).collect(),
children.iter().map(|(k, v)| (intern(*k).marker(), v.to_api_rec(vfses))).collect(),
),
}
}