using NodePipeline.Abstractions.Interfaces.Nodes; using NodePipeline.Abstractions.Models; namespace NodePipeline.Engine.NodeFields; public static class PortConnector { /// /// Connects node input and node output /// /// /// Node output with direction = /// /// /// /// Node input with direction = /// /// /// public static void ConnectStatic(INodeField from, INodeField to) { to.BindStatic(from); } /// /// Connects node input and node output /// /// /// Node output with direction = /// /// /// /// Node input with direction = /// /// /// public static void ConnectStatic(NodeField from, NodeField to) { to.BindStatic(from); } /// /// Connects node input and node output /// /// /// Node output with direction = /// /// /// /// Node input with direction = /// /// /// public static void ConnectStatic(INodeField from, NodeField to) { to.BindStatic(from); } /// /// Connects node input and node output /// /// /// Node output with direction = /// /// /// /// Node input with direction = /// /// /// public static void ConnectStatic(NodeField from, INodeField to) { to.BindStatic(from); } public static void ConnectRuntime(INodeField from, INodeField to) where T : notnull { to.BindRuntime(() => from.Value, v => from.Value = v); } public static void ConnectRuntimeBothNullable(INodeField from, INodeField to) where T : struct { to.BindRuntime(() => from.Value, v => from.Value = v); } public static void ConnectRuntimeBothNullable(INodeField from, INodeField to) where T : class { to.BindRuntime(() => from.Value, v => from.Value = v); } public static void ConnectRuntimeToNullableStruct(INodeField from, INodeField to) where T : struct { to.BindRuntime(() => from.Value, v => to.Value = v); } public static void ConnectRuntimeToNullableClass(INodeField from, INodeField to) where T : class { to.BindRuntime(() => from.Value, v => to.Value = v); } public static void ConnectRuntimeFromNullableStruct(INodeField from, INodeField to, string currentNodeId, string inputPortName, string sourceNodeId, string outputPortName) where T : struct { to.BindRuntime( () => GetValueFromNullableStruct(from.Value, currentNodeId, inputPortName, sourceNodeId, outputPortName), v => to.Value = v); } public static void ConnectRuntimeFromNullableClass(INodeField from, INodeField to, string currentNodeId, string inputPortName, string sourceNodeId, string outputPortName) where T : class { to.BindRuntime( () => GetValueFromNullableClass(from.Value, currentNodeId, inputPortName, sourceNodeId, outputPortName), v => to.Value = v); } private static T GetValueFromNullableStruct(T? value, string currentNodeId, string inputPortName, string sourceNodeId, string outputPortName) where T : struct { if (value is not null) return (T)value; var message = string.Concat("Input port [", inputPortName, "] of node [", currentNodeId, "] does not allow a null value received from output port [", outputPortName, "] of [", sourceNodeId, "] node"); throw new Exception(message); //TODO: add new exception } private static T GetValueFromNullableClass(T? value, string currentNodeId, string inputPortName, string sourceNodeId, string outputPortName) where T : class { if (value is not null) return value; var message = string.Concat("Input port [", inputPortName, "] of node [", currentNodeId, "] does not allow a null value received from output port [", outputPortName, "] of [", sourceNodeId, "] node"); throw new Exception(message); //TODO: add new exception } }