Skip to content

Commit

Permalink
better descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ypopovych committed Jun 22, 2023
1 parent 55026b4 commit 579f15b
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 18 deletions.
6 changes: 5 additions & 1 deletion Sources/Substrate/Block/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public extension StaticEvent {
}
}

public struct AnyEvent: Event {
public struct AnyEvent: Event, CustomStringConvertible {
public let pallet: String
public let name: String

Expand Down Expand Up @@ -137,6 +137,10 @@ public struct AnyEvent: Event {
}
return try E(params: params, info: info)
}

public var description: String {
"\(pallet).\(name)(\(params))"
}
}

public enum EventDecodingError: Error {
Expand Down
16 changes: 14 additions & 2 deletions Sources/Substrate/Block/EventRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@
import Foundation
import ScaleCodec

public enum EventPhase: Equatable, Hashable {
public enum EventPhase: Equatable, Hashable, CustomStringConvertible {
// Applying an extrinsic.
case applyExtrinsic(UInt32)
// Finalizing the block.
case finalization
// Initializing the block.
case initialization

public var description: String {
switch self {
case .applyExtrinsic(let index): return "apply #\(index)"
case .finalization: return "finalization"
case .initialization: return "initialization"
}
}
}

public protocol SomeEventRecord: ScaleRuntimeDecodable {
Expand All @@ -24,7 +32,7 @@ public protocol SomeEventRecord: ScaleRuntimeDecodable {
func typed<E: StaticEvent>(_ type: E.Type) throws -> E
}

public struct EventRecord<H: Hash>: SomeEventRecord {
public struct EventRecord<H: Hash>: SomeEventRecord, CustomStringConvertible {
public let phase: EventPhase
public let event: AnyEvent
public let topics: [H]
Expand All @@ -45,6 +53,10 @@ public struct EventRecord<H: Hash>: SomeEventRecord {
public func typed<E: StaticEvent>(_ type: E.Type) throws -> E {
try event.typed(type)
}

public var description: String {
"{phase: \(phase), event: \(event), topics: \(topics)}"
}
}

extension EventPhase: ScaleDecodable {
Expand Down
6 changes: 5 additions & 1 deletion Sources/Substrate/Block/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public extension SomeBlockEvents {
}
}

public struct BlockEvents<ER: SomeEventRecord>: SomeBlockEvents {
public struct BlockEvents<ER: SomeEventRecord>: SomeBlockEvents, CustomStringConvertible {
public typealias ER = ER

public let events: [ER]
Expand All @@ -170,5 +170,9 @@ public struct BlockEvents<ER: SomeEventRecord>: SomeBlockEvents {
events.filter { $0.extrinsicIndex.map{$0 == index} ?? false }
}

public var description: String {
events.description
}

public static var `default`: BlockEvents<ER> { Self(events: []) }
}
2 changes: 1 addition & 1 deletion Sources/Substrate/Extrinsic/ExtrinsicV4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public struct ExtrinsicV4Extra<Addr, Sig, Extra>: ExtrinsicExtra, CustomStringCo
}

public var description: String {
"{\"address\": \(address), \"signature\": \(signature), \"extra\": \(extra)}"
"{address: \(address), signature: \(signature), extra: \(extra)}"
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/Substrate/Metadata/Metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import ScaleCodec

public protocol RuntimeMetadata {
var version: UInt8 { get }
var types: [RuntimeTypeInfo] { get }

func asMetadata() throws -> Metadata

static var versions: Set<UInt32> { get }
Expand Down
56 changes: 46 additions & 10 deletions Sources/Substrate/Metadata/RuntimeTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public struct RuntimeTypeId: ScaleCodable, Hashable, Equatable,
try encoder.encode(id, .compact)
}

public var description: String {
"#\(id)"
}
public var description: String { String(id) }
}

public struct RuntimeTypeInfo: CustomStringConvertible {
Expand All @@ -54,7 +52,7 @@ public struct RuntimeTypeInfo: CustomStringConvertible {
}

public var description: String {
"\(type)\(id)"
"{id: \(id), type: \(type)}"
}
}

Expand Down Expand Up @@ -88,8 +86,14 @@ public struct RuntimeType: CustomStringConvertible {
}

public var description: String {
let params = parameters.isEmpty ? "" : "<\(parameters.map{$0.description}.joined(separator: ", "))>"
return "\(pathBasedName ?? "_")\(params){\(definition)}"
if let name = pathBasedName {
if parameters.isEmpty {
return "\(name)(\(definition))"
}
let params = parameters.map{$0.description}.joined(separator: ", ")
return "\(name)<\(params)>(\(definition))"
}
return definition.description
}
}

Expand Down Expand Up @@ -123,7 +127,7 @@ public struct RuntimeTypeParameter: CustomStringConvertible {
}

public var description: String {
"\(name)[\(type.map{$0.description} ?? "-")]"
type == nil ? name : "\(name)#\(type!)"
}
}

Expand All @@ -149,6 +153,22 @@ public enum RuntimeTypeDefinition {
case bitsequence(store: RuntimeTypeId, order: RuntimeTypeId)
}

extension RuntimeTypeDefinition: CustomStringConvertible {
public var description: String {
switch self {
case .composite(fields: let fields): return fields.description
case .variant(variants: let vars): return vars.description
case .sequence(of: let id): return "Array<#\(id)>"
case .array(count: let cnt, of: let id): return "Array<#\(id)>[\(cnt)]"
case .tuple(components: let fields):
return "(\(fields.map{"#\($0)"}.joined(separator: ", ")))"
case .primitive(is: let pr): return pr.description
case .compact(of: let id): return "Compact<#\(id)>"
case .bitsequence(store: let sid, order: let ord): return "BitSeq<#\(sid),#\(ord)>"
}
}
}

extension RuntimeTypeDefinition: ScaleCodable {
public init(from decoder: ScaleDecoder) throws {
let caseId = try decoder.decode(.enumCaseId)
Expand Down Expand Up @@ -181,7 +201,7 @@ extension RuntimeTypeDefinition: ScaleCodable {
}
}

public struct RuntimeTypeField {
public struct RuntimeTypeField: CustomStringConvertible {
public let name: String?
public let type: RuntimeTypeId
public let typeName: String?
Expand All @@ -193,6 +213,14 @@ public struct RuntimeTypeField {
self.typeName = typeName
self.docs = docs
}

public var description: String {
let typ = typeName == nil ? "#\(type)" : "(\(typeName!)#\(type))"
if let name = name {
return "\(name): \(typ)"
}
return typ
}
}

extension RuntimeTypeField: ScaleCodable {
Expand All @@ -209,7 +237,7 @@ extension RuntimeTypeField: ScaleCodable {
}
}

public struct RuntimeTypeVariantItem {
public struct RuntimeTypeVariantItem: CustomStringConvertible {
public let name: String
public let fields: [RuntimeTypeField]
public let index: UInt8
Expand All @@ -226,6 +254,13 @@ public struct RuntimeTypeVariantItem {
self.index = index
self.docs = docs
}

public var description: String {
if fields.count == 0 {
return "\(name)[\(index)]"
}
return "\(name)[\(index)](\(fields))"
}
}

extension RuntimeTypeVariantItem: ScaleCodable {
Expand All @@ -242,7 +277,7 @@ extension RuntimeTypeVariantItem: ScaleCodable {
}
}

public enum RuntimeTypePrimitive: String, CaseIterable, ScaleCodable {
public enum RuntimeTypePrimitive: String, CaseIterable, ScaleCodable, CustomStringConvertible {
case bool
case char
case str
Expand All @@ -260,4 +295,5 @@ public enum RuntimeTypePrimitive: String, CaseIterable, ScaleCodable {
case i256

public var name: String { rawValue }
public var description: String { name }
}
4 changes: 1 addition & 3 deletions Sources/Substrate/Types/Value/Value.swift
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,7 @@ extension Value.Def: Hashable where C: Hashable {}
extension Value.Variant: Hashable where C: Hashable {}

extension Value: CustomStringConvertible {
public var description: String {
C.self == Void.self ? value.description : "\(value)\(context)"
}
public var description: String { value.description }
}

extension Value.Def: CustomStringConvertible {
Expand Down

0 comments on commit 579f15b

Please sign in to comment.