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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
use anyhow::{anyhow, Context};
use crate::{
comfy::getter::{guess_node_mut, GetExt, Getter},
models::*,
};
/// Extension methods for `Prompt` to use Setters to set values on nodes.
pub trait SetterExt<T, N>
where
N: Node + 'static,
{
/// Uses a heuristic to find a `Node` and set the value on it.
///
/// # Inputs
///
/// * `value` - The value to set.
///
/// # Returns
///
/// `Ok(())` on success, or an error if the node could not be found.
fn set<S>(&mut self, value: T) -> anyhow::Result<()>
where
S: Setter<T, N>;
/// Finds a `Node` leading into the given `output_node` and sets the value on it.
///
/// # Inputs
///
/// * `output_node` - The id of the node to path from.
///
/// # Returns
///
/// `Ok(())` on success, or an error if the node could not be found.
fn set_from<S>(&mut self, output_node: &str, value: T) -> anyhow::Result<()>
where
S: Setter<T, N>;
/// Sets the value on the node with id `node`.
///
/// # Inputs
///
/// * `node` - The id of the node to set the value on.
/// * `value` - The value to set.
///
/// # Returns
///
/// `Ok(())` on success, or an error if the node could not be found.
fn set_node<S>(&mut self, node: &str, value: T) -> anyhow::Result<()>
where
S: Setter<T, N>;
}
/// Extension methods for `Prompt` to set values on nodes.
pub trait SetExt<N>
where
N: Node + 'static,
{
/// Uses a heuristic to find a `Node` and set the value on it.
///
/// # Inputs
///
/// * `f` - A function that takes a mutable reference to the node and returns a `Result`.
///
/// # Returns
///
/// `Ok(())` on success, or an error if the node could not be found.
fn set_with<F>(&mut self, f: F) -> anyhow::Result<()>
where
F: FnOnce(&mut N) -> anyhow::Result<()>;
/// Sets the value on the node with id `node`.
///
/// # Inputs
///
/// * `node` - The id of the node to set the value on.
///
/// # Returns
///
/// `Ok(())` on success, or an error if the node could not be found.
fn set_node_with<F>(&mut self, node: &str, f: F) -> anyhow::Result<()>
where
F: FnOnce(&mut N) -> anyhow::Result<()>;
}
impl<T, N: Node + 'static> SetterExt<T, N> for crate::models::Prompt {
fn set<S>(&mut self, value: T) -> anyhow::Result<()>
where
S: Setter<T, N>,
{
S::default().set(self, value)
}
fn set_from<S>(&mut self, output_node: &str, value: T) -> anyhow::Result<()>
where
S: Setter<T, N>,
{
S::default().set_from(self, output_node, value)
}
fn set_node<S>(&mut self, node: &str, value: T) -> anyhow::Result<()>
where
S: Setter<T, N>,
{
S::default().set_node(self, node, value)
}
}
impl<N: Node + 'static> SetExt<N> for crate::models::Prompt {
fn set_with<F>(&mut self, f: F) -> anyhow::Result<()>
where
F: FnOnce(&mut N) -> anyhow::Result<()>,
{
if let Some(node) = guess_node_mut::<N>(self, None) {
f(as_node_mut::<N>(node).context("Failed to cast node")?)
} else {
Err(anyhow!("Failed to find node"))
}
}
fn set_node_with<F>(&mut self, node: &str, f: F) -> anyhow::Result<()>
where
F: FnOnce(&mut N) -> anyhow::Result<()>,
{
f(self.get_typed_node_mut(node)?)
}
}
/// A trait for setting values on nodes.
///
/// This trait is implemented for types that can be used to set values on nodes.
/// Usually, this trait does not need to be implemented directly, as it is implemented
/// for all types that implement `Getter`.
pub trait Setter<T, N>
where
N: Node + 'static,
Self: Getter<T, N>,
{
/// Uses a heuristic to find a `Node` and set the value on it.
///
/// # Inputs
///
/// * `prompt` - A mutable reference to a `Prompt`.
///
/// # Returns
///
/// `Ok(())` on success, or an error if the node could not be found.
fn set(&self, prompt: &mut crate::models::Prompt, value: T) -> anyhow::Result<()> {
let node = if let Some(node) = guess_node_mut::<N>(prompt, None) {
node
} else {
return Err(anyhow!("Failed to find node"));
};
self.set_value(node, value)
}
/// Finds a `Node` leading into the given `output_node` and sets the value on it.
///
/// # Inputs
///
/// * `prompt` - A mutable reference to a `Prompt`.
///
/// # Returns
///
/// `Ok(())` on success, or an error if the node could not be found.
fn set_from(
&self,
prompt: &mut crate::models::Prompt,
output_node: &str,
value: T,
) -> anyhow::Result<()> {
let node = if let Some(node) = Self::find_node(prompt, Some(output_node)) {
prompt
.get_node_by_id_mut(&node)
.context("Failed to find node")?
} else {
return Err(anyhow!("Failed to find node"));
};
self.set_value(node, value)
}
/// Sets the value on the node with id `node`.
///
/// # Inputs
///
/// * `prompt` - A mutable reference to a `Prompt`.
/// * `node` - The id of the node to set the value on.
///
/// # Returns
///
/// `Ok(())` on success, or an error if the node could not be found.
fn set_node(
&self,
prompt: &mut crate::models::Prompt,
node: &str,
value: T,
) -> anyhow::Result<()> {
let node = prompt.get_node_by_id_mut(node).unwrap();
self.set_value(node, value)
}
/// Sets the value on the given `Node`.
///
/// # Inputs
///
/// * `node` - A mutable reference to a `Node`.
///
/// # Returns
///
/// `Ok(())` on success, or an error if the node could not be found.
fn set_value(&self, node: &mut dyn Node, value: T) -> anyhow::Result<()> {
*self.get_value_mut(node)? = value;
Ok(())
}
}
impl<G, T, N> Setter<T, N> for G
where
G: Getter<T, N>,
N: Node + 'static,
{
}