From 75372892952c36d7bee42a5e849c69014184d596 Mon Sep 17 00:00:00 2001 From: Kyle Date: Thu, 20 Feb 2025 13:36:36 +0800 Subject: [PATCH] Add Graph+Debug API --- Sources/OpenGraphShims/Graph+Debug.swift | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Sources/OpenGraphShims/Graph+Debug.swift diff --git a/Sources/OpenGraphShims/Graph+Debug.swift b/Sources/OpenGraphShims/Graph+Debug.swift new file mode 100644 index 00000000..dbb5a625 --- /dev/null +++ b/Sources/OpenGraphShims/Graph+Debug.swift @@ -0,0 +1,41 @@ +// +// Graph+Debug.swift +// OpenGraphShims + +// FIXME: Update name in OpenGraph +public typealias Graph = OGGraph +public typealias Subgraph = OGSubgraph + +#if canImport(Darwin) + +import Foundation + +@_spi(Debug) +extension Graph { + public var dict: [String: Any]? { + let options = ["format": "graph/dict"] as NSDictionary + guard let description = Graph.description(nil, options: options) else { + return nil + } + guard let dictionary = description.takeUnretainedValue() as? NSDictionary else { + return nil + } + return dictionary as? [String: Any] + } + + // style: + // - bold: empty input/output edge + // - dashed: indirect or has no value + // color: + // - red: is_changed + public var dot: String? { + let options = ["format": "graph/dot"] as NSDictionary + guard let description = Graph.description(self, options: options) + else { + return nil + } + return description.takeUnretainedValue() as? String + } +} + +#endif