feat(defs): iter_files and small fixes
This commit is contained in:
parent
b00bf8535d
commit
fdbe837a5d
@ -242,10 +242,10 @@ op -=(String, char);
|
|||||||
op +=(char, String);
|
op +=(char, String);
|
||||||
op +=(char, char);
|
op +=(char, char);
|
||||||
|
|
||||||
op +=(arraArray, item: Array);
|
op +=(arraArray, Array);
|
||||||
op +=(arraArray, item: ?);
|
op +=(arraArray, ?);
|
||||||
|
|
||||||
op +=(Blob, Blob);
|
op +=(Blob, Blob);
|
||||||
op +=(Blob, value: i64);
|
op +=(Blob, i64);
|
||||||
op +=(Blob, char);
|
op +=(Blob, char);
|
||||||
op +=(Blob, String);
|
op +=(Blob, String);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
module::FuncInfo, plugin::*, tokenizer::is_valid_function_name, Engine, Module, Scope,
|
module::FuncInfo, plugin::*, tokenizer::is_valid_function_name, Engine, Module, Scope,
|
||||||
};
|
};
|
||||||
use core::fmt;
|
use core::{fmt, iter};
|
||||||
|
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
@ -71,13 +71,10 @@ pub struct Definitions<'e> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'e> Definitions<'e> {
|
impl<'e> Definitions<'e> {
|
||||||
/// Write all the definition files to a directory.
|
/// Write all the definition files returned from [`iter_files`] to a directory.
|
||||||
///
|
///
|
||||||
/// The following separate definition files are generated:
|
/// This function will create the directory path if it does not yet exist,
|
||||||
///
|
/// it will also override any existing files as needed.
|
||||||
/// - `__static__.d.rhai`: globally available items of the engine
|
|
||||||
/// - `__scope__.d.rhai`: items in the given scope, if any
|
|
||||||
/// - a separate file for each registered module
|
|
||||||
#[cfg(not(feature = "no_std"))]
|
#[cfg(not(feature = "no_std"))]
|
||||||
pub fn write_to_dir(&self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
|
pub fn write_to_dir(&self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
|
||||||
use std::fs;
|
use std::fs;
|
||||||
@ -101,7 +98,6 @@ impl<'e> Definitions<'e> {
|
|||||||
fs::write(path.join("__scope__.d.rhai"), self.scope())?;
|
fs::write(path.join("__scope__.d.rhai"), self.scope())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_module"))]
|
|
||||||
for (name, decl) in self.modules() {
|
for (name, decl) in self.modules() {
|
||||||
fs::write(path.join(format!("{name}.d.rhai")), decl)?;
|
fs::write(path.join(format!("{name}.d.rhai")), decl)?;
|
||||||
}
|
}
|
||||||
@ -109,6 +105,34 @@ impl<'e> Definitions<'e> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Iterate over the generated definition files.
|
||||||
|
///
|
||||||
|
/// The returned iterator yields all the definition files as (filename, content) pairs.
|
||||||
|
pub fn iter_files(&self) -> impl Iterator<Item = (String, String)> + '_ {
|
||||||
|
IntoIterator::into_iter([
|
||||||
|
(
|
||||||
|
"__builtin__.d.rhai".to_string(),
|
||||||
|
include_str!("builtin.d.rhai").to_string(),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"__builtin-operators__.d.rhai".to_string(),
|
||||||
|
include_str!("builtin-operators.d.rhai").to_string(),
|
||||||
|
),
|
||||||
|
("__static__.d.rhai".to_string(), self.static_module()),
|
||||||
|
])
|
||||||
|
.chain(iter::from_fn(move || {
|
||||||
|
if self.scope.is_some() {
|
||||||
|
Some(("__scope__.d.rhai".to_string(), self.scope()))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
.chain(
|
||||||
|
self.modules()
|
||||||
|
.map(|(name, def)| (format!("{name}.d.rhai"), def)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Return the definitions for the globally available
|
/// Return the definitions for the globally available
|
||||||
/// items of the engine.
|
/// items of the engine.
|
||||||
///
|
///
|
||||||
@ -148,8 +172,10 @@ impl<'e> Definitions<'e> {
|
|||||||
/// Return name and definition pairs for each registered module.
|
/// Return name and definition pairs for each registered module.
|
||||||
///
|
///
|
||||||
/// The definitions will always start with `module <module name>;`.
|
/// The definitions will always start with `module <module name>;`.
|
||||||
#[cfg(not(feature = "no_module"))]
|
///
|
||||||
|
/// If the feature `no_module` is enabled, this will yield no elements.
|
||||||
pub fn modules(&self) -> impl Iterator<Item = (String, String)> + '_ {
|
pub fn modules(&self) -> impl Iterator<Item = (String, String)> + '_ {
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
let mut m = self
|
let mut m = self
|
||||||
.engine
|
.engine
|
||||||
.global_sub_modules
|
.global_sub_modules
|
||||||
@ -162,6 +188,9 @@ impl<'e> Definitions<'e> {
|
|||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
#[cfg(feature = "no_module")]
|
||||||
|
let mut m = Vec::new();
|
||||||
|
|
||||||
m.sort_by(|(name1, _), (name2, _)| name1.cmp(name2));
|
m.sort_by(|(name1, _), (name2, _)| name1.cmp(name2));
|
||||||
|
|
||||||
m.into_iter()
|
m.into_iter()
|
||||||
|
Loading…
Reference in New Issue
Block a user