use std::fmt;
use std::rc::Rc;
use crate::State;
use super::Goal;
#[derive(Clone)]
pub struct Custom(Rc<dyn Fn(State) -> Option<State>>);
impl Goal for Custom {
fn apply(&self, state: State) -> Option<State> {
(self.0)(state)
}
}
pub fn custom<F>(func: F) -> Custom
where
F: Fn(State) -> Option<State> + 'static,
{
Custom(Rc::new(func))
}
impl fmt::Debug for Custom {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Custom(<fn>)")
}
}
#[cfg(test)]
mod tests {
use super::custom;
use crate::{LVar, Query};
#[test]
fn succeeds() {
let x = LVar::new();
let goal = custom(move |s| s.unify(&x.into(), &1.into()));
let results: Vec<_> = goal.query(x).collect();
assert_eq!(results, vec![1]);
}
#[test]
fn debug_impl() {
let goal = custom(|_| None);
assert_eq!(format!("{goal:?}"), "Custom(<fn>)");
}
}