Skip to content

Commit ebcdd4f

Browse files
committed
human readable descriptions for Markdown Block Directive errors
1 parent da2ce4c commit ebcdd4f

10 files changed

+127
-8
lines changed

Sources/MarkdownSemantics/AST/Markdown.BlockDirectiveArgumentTypeError.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,16 @@ extension Markdown
44
where Option:BlockDirectiveOption
55
{
66
let option:Option
7+
let value:String
8+
}
9+
}
10+
extension Markdown.BlockDirectiveArgumentTypeError:CustomStringConvertible
11+
{
12+
var description:String
13+
{
14+
"""
15+
could not convert argument '\(self.value)' for option '\(self.option)' to expected \
16+
type '\(Value.self)'
17+
"""
718
}
819
}

Sources/MarkdownSemantics/AST/Markdown.BlockDirectiveDuplicateOptionError.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@ extension Markdown
55
let option:Option
66
}
77
}
8+
extension Markdown.BlockDirectiveDuplicateOptionError:CustomStringConvertible
9+
{
10+
var description:String
11+
{
12+
"duplicate option '\(self.option.rawValue)'"
13+
}
14+
}

Sources/MarkdownSemantics/AST/Markdown.BlockDirectiveOption (ext).swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ extension Markdown.BlockDirectiveOption
88
let value:Value = .init(rawValue: value.string)
99
else
1010
{
11-
throw Markdown.BlockDirectiveArgumentTypeError<Self, Value>.init(option: self)
11+
throw Markdown.BlockDirectiveArgumentTypeError<Self, Value>.init(
12+
option: self,
13+
value: value.string)
1214
}
1315

1416
return value
@@ -22,7 +24,9 @@ extension Markdown.BlockDirectiveOption
2224
let value:Value = .init(value.string)
2325
else
2426
{
25-
throw Markdown.BlockDirectiveArgumentTypeError<Self, Value>.init(option: self)
27+
throw Markdown.BlockDirectiveArgumentTypeError<Self, Value>.init(
28+
option: self,
29+
value: value.string)
2630
}
2731

2832
return value

Sources/MarkdownSemantics/AST/Media/Markdown.BlockCodeFragment.PathError.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,23 @@ extension Markdown.BlockCodeFragment
66
case format(String)
77
}
88
}
9+
extension Markdown.BlockCodeFragment.PathError:CustomStringConvertible
10+
{
11+
var description:String
12+
{
13+
switch self
14+
{
15+
case .directory(let path):
16+
"""
17+
the legacy 'path' syntax requires the second path component to be 'Snippets', \
18+
found '\(path)'
19+
"""
20+
21+
case .format(let path):
22+
"""
23+
the legacy 'path' syntax requires a path of the form 'Package/Snippets/Name', \
24+
found '\(path)'
25+
"""
26+
}
27+
}
28+
}

Sources/MarkdownSemantics/AST/Media/Markdown.BlockCodeFragment.ReferenceError.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,28 @@ extension Markdown.BlockCodeFragment
66
case slice(undefined:String, available:[String])
77
}
88
}
9+
extension Markdown.BlockCodeFragment.ReferenceError:CustomStringConvertible
10+
{
11+
var description:String
12+
{
13+
switch self
14+
{
15+
case .snippet(nil, let available):
16+
"""
17+
no snippet 'id' or 'path' is defined, available snippets are: \
18+
\(available.lazy.map { "'\($0)'" } .joined(separator: ", "))
19+
"""
20+
case .snippet(let undefined?, let available):
21+
"""
22+
no such snippet '\(undefined)' exists in this package, available snippets are: \
23+
\(available.lazy.map { "'\($0)'" } .joined(separator: ", "))
24+
"""
25+
26+
case .slice(let undefined, let available):
27+
"""
28+
snippet does not contain a slice named '\(undefined)', available slices: \
29+
\(available.lazy.map { "'\($0)'" } .joined(separator: ", "))
30+
"""
31+
}
32+
}
33+
}

Sources/MarkdownSemantics/AST/Media/Markdown.BlockCodeFragment.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ extension Markdown.BlockCodeFragment:Markdown.BlockDirectiveType
101101
// OK for the path to contain additional intermediate path components, which
102102
// are just as irrelevant as the package name, because snippet names are
103103
// unique within a package.
104-
guard
105-
case "Snippets" = value[value.index(after: i)...].prefix(while: { $0 != "/" })
106-
else
104+
switch value[value.index(after: i)...].prefix(while: { $0 != "/" })
107105
{
108-
throw PathError.directory(value)
109-
}
106+
case "Snippets":
107+
self.snippet = String.init(value[value.index(after: j)...])
110108

111-
self.snippet = String.init(value[value.index(after: j)...])
109+
case let invalid:
110+
throw PathError.directory(String.init(invalid))
111+
}
112112
}
113113
}
114114
}

Sources/MarkdownSemantics/AST/Media/Markdown.BlockCodeReference.SemanticError.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@ extension Markdown.BlockCodeReference
55
case resetContradictsBase
66
}
77
}
8+
extension Markdown.BlockCodeReference.SemanticError:CustomStringConvertible
9+
{
10+
var description:String
11+
{
12+
switch self
13+
{
14+
case .resetContradictsBase:
15+
"""
16+
'reset' cannot be used with 'base' (a.k.a. 'previousFile')
17+
"""
18+
}
19+
}
20+
}

Sources/MarkdownSemantics/AST/Media/Markdown.BlockLeaf.StructuralError.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@ extension Markdown.BlockLeaf
55
case childUnexpected
66
}
77
}
8+
extension Markdown.BlockLeaf.StructuralError:CustomStringConvertible
9+
{
10+
var description:String
11+
{
12+
switch self
13+
{
14+
case .childUnexpected:
15+
"""
16+
block directive of this type cannot contain child blocks
17+
"""
18+
}
19+
}
20+
}

Sources/MarkdownSemantics/AST/Tutorials/Markdown.BlockArticle.StructuralError.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@ extension Markdown.BlockArticle
55
case intro(type:Markdown.BlockElement.Type)
66
}
77
}
8+
extension Markdown.BlockArticle.StructuralError:CustomStringConvertible
9+
{
10+
var description:String
11+
{
12+
switch self
13+
{
14+
case .intro(let type):
15+
"""
16+
the first block in an article should be an '@Intro', found '\(type)'
17+
"""
18+
}
19+
}
20+
}

Sources/MarkdownSemantics/AST/Tutorials/Markdown.BlockTopicReference.TargetError.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@ extension Markdown.BlockTopicReference
55
case doclink(String)
66
}
77
}
8+
extension Markdown.BlockTopicReference.TargetError:CustomStringConvertible
9+
{
10+
var description:String
11+
{
12+
switch self
13+
{
14+
case .doclink(let link):
15+
"""
16+
could not parse documentation link '\(link)'
17+
"""
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)