mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 22:28:10 +08:00
Fold attribute names through monitors and skip coercing name inputs
This commit is contained in:
@@ -1,11 +1,42 @@
|
||||
use crate::document::NodeNetwork;
|
||||
use crate::proto::{ProtoNetwork, Registry};
|
||||
use crate::proto::{GraphErrors, ProtoNetwork, Registry};
|
||||
use std::error::Error;
|
||||
|
||||
/// Why a network failed to compile: a structural message, or errors the
|
||||
/// editor can pin to their nodes in the graph.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum CompileError {
|
||||
Message(String),
|
||||
Graph(GraphErrors),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for CompileError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
CompileError::Message(message) => write!(f, "{message}"),
|
||||
CompileError::Graph(errors) => write!(f, "{errors:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for CompileError {}
|
||||
|
||||
impl From<String> for CompileError {
|
||||
fn from(message: String) -> Self {
|
||||
CompileError::Message(message)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CompileError> for String {
|
||||
fn from(error: CompileError) -> Self {
|
||||
error.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Compiler {}
|
||||
|
||||
impl Compiler {
|
||||
pub fn compile<'r>(&self, mut network: NodeNetwork, registry: &'r Registry) -> impl Iterator<Item = Result<ProtoNetwork, String>> + 'r {
|
||||
pub fn compile<'r>(&self, mut network: NodeNetwork, registry: &'r Registry) -> impl Iterator<Item = Result<ProtoNetwork, CompileError>> + 'r {
|
||||
network.resolve_scope_inputs();
|
||||
network.generate_node_paths(&[]);
|
||||
let node_ids = network.nodes.keys().copied().collect::<Vec<_>>();
|
||||
@@ -20,17 +51,15 @@ impl Compiler {
|
||||
proto_networks.map(move |mut proto_network| {
|
||||
proto_network.insert_context_nullification_nodes()?;
|
||||
let _ = proto_network.resolve_types(registry);
|
||||
proto_network
|
||||
.compute_layouts()
|
||||
.map_err(|errors| errors.iter().map(|error| format!("{:?}", error.error)).collect::<Vec<_>>().join("\n"))?;
|
||||
proto_network.compute_layouts().map_err(CompileError::Graph)?;
|
||||
proto_network.generate_stable_node_ids();
|
||||
Ok(proto_network)
|
||||
})
|
||||
}
|
||||
pub fn compile_single(&self, network: NodeNetwork, registry: &Registry) -> Result<ProtoNetwork, String> {
|
||||
pub fn compile_single(&self, network: NodeNetwork, registry: &Registry) -> Result<ProtoNetwork, CompileError> {
|
||||
assert_eq!(network.exports.len(), 1, "Graph with multiple outputs not yet handled");
|
||||
let Some(proto_network) = self.compile(network, registry).next() else {
|
||||
return Err("Failed to convert graph into proto graph".to_string());
|
||||
return Err(CompileError::Message("Failed to convert graph into proto graph".to_string()));
|
||||
};
|
||||
proto_network
|
||||
}
|
||||
|
||||
@@ -424,6 +424,27 @@ impl ProtoNetwork {
|
||||
errors.is_empty().then_some(()).ok_or(errors)
|
||||
}
|
||||
|
||||
/// The constant a node serves, seen through the wrappers that pass a value
|
||||
/// on unchanged (the editor's monitors, memo and nullification nodes, and
|
||||
/// passthroughs); a node that computes its value names itself instead.
|
||||
fn constant_behind(&self, id: NodeId) -> Result<&MemoHash<TaggedValue>, &str> {
|
||||
let transparent = [
|
||||
graphene_core::memo::monitor::IDENTIFIER,
|
||||
graphene_core::memo::memoize::IDENTIFIER,
|
||||
graphene_core::memo::frame_memo::IDENTIFIER,
|
||||
graphene_core::context_modification::context_modification::IDENTIFIER,
|
||||
graphene_core::ops::passthrough::IDENTIFIER,
|
||||
];
|
||||
let mut node = &self.nodes[id.0 as usize].1;
|
||||
loop {
|
||||
match &node.construction_args {
|
||||
ConstructionArgs::Value(value) => return Ok(value),
|
||||
ConstructionArgs::Nodes(items) if transparent.contains(&node.identifier) && !items.is_empty() => node = &self.nodes[items[0].0 as usize].1,
|
||||
_ => return Err(node.identifier.as_str()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolves every name-from-input write against the constant its name
|
||||
/// input carries, leaving each node's meta indistinguishable from a marker
|
||||
/// node's. A name input that is not a constant is refused here, which is
|
||||
@@ -449,18 +470,17 @@ impl ProtoNetwork {
|
||||
.collect();
|
||||
let names: Vec<Result<&'static str, GraphErrorType>> = sources
|
||||
.iter()
|
||||
.map(
|
||||
|&(name_input, _)| match inputs.get(name_input as usize).map(|input| &self.nodes[input.0 as usize].1.construction_args) {
|
||||
Some(ConstructionArgs::Value(value)) => match &**value {
|
||||
value::TaggedValue::String(name) => Ok(core_types::attribute::intern_name(name)),
|
||||
other => Err(GraphErrorType::AttributeName(format!("an attribute name must be text, but input {} is {}", name_input + 1, other.ty()))),
|
||||
},
|
||||
_ => Err(GraphErrorType::AttributeName(format!(
|
||||
"input {} must be a constant, since attribute names resolve when the graph compiles rather than when it runs",
|
||||
name_input + 1
|
||||
))),
|
||||
.map(|&(name_input, _)| match inputs.get(name_input as usize).map(|input| self.constant_behind(*input)) {
|
||||
Some(Ok(value)) => match &**value {
|
||||
value::TaggedValue::String(name) => Ok(core_types::attribute::intern_name(name)),
|
||||
other => Err(GraphErrorType::AttributeName(format!("an attribute name must be text, but input {} is {}", name_input + 1, other.ty()))),
|
||||
},
|
||||
)
|
||||
Some(Err(computed_by)) => Err(GraphErrorType::AttributeName(format!(
|
||||
"input {} must be a constant, since attribute names resolve when the graph compiles rather than when it runs, but it is computed by {computed_by}",
|
||||
name_input + 1
|
||||
))),
|
||||
None => Err(GraphErrorType::AttributeName(format!("input {} must be a constant, but the node has no such input", name_input + 1))),
|
||||
})
|
||||
.collect();
|
||||
let node = &self.nodes[index].1;
|
||||
let mut folded: Vec<(&'static str, std::any::TypeId)> = Vec::new();
|
||||
|
||||
Reference in New Issue
Block a user