use std::ffi::{CStr, CString};
use std::marker::PhantomData;
use std::ptr::NonNull;
use crate::{Defmodule, Environment, ffi};
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ModuleError {
InteriorNul,
NotFound(String),
}
impl std::fmt::Display for ModuleError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InteriorNul => formatter.write_str("module name contains a NUL byte"),
Self::NotFound(name) => write!(formatter, "defmodule {name:?} was not found"),
}
}
}
impl std::error::Error for ModuleError {}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FocusEntry<'env> {
name: String,
marker: PhantomData<&'env Environment>,
}
impl FocusEntry<'_> {
pub fn name(&self) -> &str {
&self.name
}
}
pub struct FocusStack<'env> {
environment: &'env Environment,
current: *mut ffi::FocalModule,
}
impl<'env> Iterator for FocusStack<'env> {
type Item = FocusEntry<'env>;
fn next(&mut self) -> Option<Self::Item> {
self.current = unsafe { ffi::GetNextFocus(self.environment.raw.as_ptr(), self.current) };
NonNull::new(self.current).map(|current| {
let name = unsafe { CStr::from_ptr(ffi::FocalModuleName(current.as_ptr())) }
.to_string_lossy()
.into_owned();
FocusEntry {
name,
marker: PhantomData,
}
})
}
}
impl Environment {
pub fn current_module(&self) -> Defmodule<'_> {
let raw = unsafe { ffi::GetCurrentModule(self.raw.as_ptr()) };
Defmodule::from_raw(
self,
NonNull::new(raw).expect("CLIPS returned no current module"),
)
}
pub fn set_current_module(&mut self, name: &str) -> Result<Defmodule<'_>, ModuleError> {
let c_name = CString::new(name).map_err(|_| ModuleError::InteriorNul)?;
let module = unsafe { ffi::FindDefmodule(self.raw.as_ptr(), c_name.as_ptr()) };
let module = NonNull::new(module).ok_or_else(|| ModuleError::NotFound(name.to_owned()))?;
let previous = unsafe { ffi::SetCurrentModule(self.raw.as_ptr(), module.as_ptr()) };
Ok(Defmodule::from_raw(
self,
NonNull::new(previous).expect("CLIPS returned no previous current module"),
))
}
pub fn module_count(&self) -> usize {
unsafe { ffi::GetNumberOfDefmodules(self.raw.as_ptr()) as usize }
}
pub fn focus(&mut self, name: &str) -> Result<(), ModuleError> {
let c_name = CString::new(name).map_err(|_| ModuleError::InteriorNul)?;
let module = unsafe { ffi::FindDefmodule(self.raw.as_ptr(), c_name.as_ptr()) };
let module = NonNull::new(module).ok_or_else(|| ModuleError::NotFound(name.to_owned()))?;
unsafe { ffi::Focus(module.as_ptr()) };
Ok(())
}
pub fn pop_focus(&mut self) -> Option<Defmodule<'_>> {
NonNull::new(unsafe { ffi::PopFocus(self.raw.as_ptr()) })
.map(|raw| Defmodule::from_raw(self, raw))
}
pub fn current_focus(&self) -> Option<Defmodule<'_>> {
NonNull::new(unsafe { ffi::GetFocus(self.raw.as_ptr()) })
.map(|raw| Defmodule::from_raw(self, raw))
}
pub fn focus_changed(&self) -> bool {
unsafe { ffi::GetFocusChanged(self.raw.as_ptr()) }
}
pub fn set_focus_changed(&self, changed: bool) {
unsafe { ffi::SetFocusChanged(self.raw.as_ptr(), changed) }
}
pub fn module_names(&self) -> Vec<String> {
let mut raw = ffi::CLIPSValue {
value: std::ptr::null_mut(),
};
unsafe { ffi::GetDefmoduleList(self.raw.as_ptr(), &mut raw) };
let names = crate::Multifield::from_value(crate::Value::retaining(self, raw));
names
.iter()
.filter_map(|name| name.as_str().map(str::to_owned))
.collect()
}
pub fn focus_stack(&self) -> FocusStack<'_> {
FocusStack {
environment: self,
current: std::ptr::null_mut(),
}
}
}