77
88import struct Foundation. Data
99import struct Foundation. UUID
10- import DOT
1110import GraphViz
1211
1312public protocol GraphVizNodeRepresentable {
1413 func graphVizNodeDescription( ) -> String
1514}
1615
1716extension GraphVizNodeRepresentable {
18- internal func graphVizNode( ) -> GraphViz . Node {
17+ func graphVizNode( ) -> GraphViz . Node {
1918 . init( graphVizNodeDescription ( ) )
2019 }
2120}
2221
2322extension String : GraphVizNodeRepresentable {
2423 public func graphVizNodeDescription( ) -> String { self }
2524}
25+
2626extension Int : GraphVizNodeRepresentable {
2727 public func graphVizNodeDescription( ) -> String { " \( self ) " }
2828}
29+
2930extension UInt : GraphVizNodeRepresentable {
3031 public func graphVizNodeDescription( ) -> String { " \( self ) " }
3132}
33+
3234extension UInt8 : GraphVizNodeRepresentable {
3335 public func graphVizNodeDescription( ) -> String { " \( self ) " }
3436}
@@ -38,32 +40,52 @@ extension UUID: GraphVizNodeRepresentable {
3840}
3941
4042public protocol GraphVizRenderable {
41- func renderGraph( as format: Format ) -> Data ?
43+ func renderGraph( as format: Format , completion: @escaping ( Result < Data , Swift . Error > ) -> Void )
44+ }
45+
46+ public enum ImageError : Swift . Error {
47+ case failedToCreateImage( _ from: Data )
4248}
4349
4450#if canImport(AppKit)
45- import class AppKit. NSImage
46- public typealias Image = NSImage
47- extension GraphVizRenderable {
48- public func renderGraphAsImage( ) -> Image ? {
49- guard let data = renderGraph ( as: . png) else {
50- return nil
51- }
51+ import class AppKit. NSImage
52+ public typealias Image = NSImage
53+ public extension GraphVizRenderable {
54+ func renderGraphAsImage( completion: @escaping ( Result < Image , Swift . Error > ) -> Void ) {
55+ renderGraph ( as: . png) { result in
56+ switch result {
57+ case let . success( data) :
58+ if let image = Image ( data: data) {
59+ completion ( . success( image) )
60+ } else {
61+ completion ( . failure( ImageError . failedToCreateImage ( data) ) )
62+ }
5263
53- return Image ( data: data)
64+ case let . failure( failure) :
65+ completion ( . failure( failure) )
66+ }
67+ }
68+ }
5469 }
55- }
5670
5771#elseif canImport(UIKit)
58- import class UIKit. UIImage
59- public typealias Image = UIImage
60- extension GraphVizRenderable {
61- public final func renderGraphAsImage( ) -> Image ? {
62- guard let data = renderGraph ( as: . png) else {
63- return nil
64- }
72+ import class UIKit. UIImage
73+ public typealias Image = UIImage
74+ public extension GraphVizRenderable {
75+ func renderGraphAsImage( completion: @escaping ( Result < Image , Swift . Error > ) -> Void ) {
76+ renderGraph ( as: . png) { result in
77+ switch result {
78+ case let . success( data) :
79+ if let image = Image ( data: data) {
80+ completion ( . success( image) )
81+ } else {
82+ completion ( . failure( ImageError . failedToCreateImage ( data) ) )
83+ }
6584
66- return Image ( data: data)
85+ case let . failure( failure) :
86+ completion ( . failure( failure) )
87+ }
88+ }
89+ }
6790 }
68- }
6991#endif
0 commit comments