in Sources/markdown-tool/Commands/FormatCommand.swift [149:189]
func checkStructuralEquivalence(between original: Document,
and formatted: Document,
source: String) throws {
struct FileHandlerOutputStream: TextOutputStream {
private let fileHandle: FileHandle
let encoding: String.Encoding
init(_ fileHandle: FileHandle, encoding: String.Encoding = .utf8) {
self.fileHandle = fileHandle
self.encoding = encoding
}
mutating func write(_ string: String) {
if let data = string.data(using: encoding) {
fileHandle.write(data)
}
}
}
var standardError = FileHandlerOutputStream(FileHandle.standardError, encoding: .utf8)
guard preferredMaximumLineLength == nil else {
print("Skipping structural equivalence check because --preferred-maximum-line-length was used, which can intentionally change document structure by inserting soft or hard line breaks.", to: &standardError)
return
}
if !original.hasSameStructure(as: formatted) {
print("Error: Formatted markup tree had different structure from the original!", to: &standardError)
print("Please file a bug with the following information:", to: &standardError)
print("Original source:", to: &standardError)
print("```markdown", to: &standardError)
print(source, to: &standardError)
print("```", to: &standardError)
print("Original structure:", to: &standardError)
print(original.debugDescription(), to: &standardError)
print("----------------", to: &standardError)
print("Structure after formatting:", to: &standardError)
print(formatted.debugDescription(), to: &standardError)
throw Error.formattedOutputHasDifferentStructures
}
}