Skip to content

Commit e5ebab6

Browse files
committed
Revise interface to use keywords
1 parent 987f6c7 commit e5ebab6

File tree

2 files changed

+83
-47
lines changed

2 files changed

+83
-47
lines changed

src/BlockScalars.jl

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,14 @@ module BlockScalars
33
export @blk_str
44

55
const ETX = '\x03' # ASCII control character: End of Text
6+
const DEFAULT_STYLE = :folded
7+
const DEFAULT_CHOMP = :strip
68

7-
const DEFAULT_STYLE = 'f'
8-
const DEFAULT_CHOMP = 's'
9-
10-
function block(str::AbstractString, block_scalar::AbstractString="")
11-
block_scalar_len = length(block_scalar)
12-
block_scalar_len > 2 && throw(ArgumentError("Too many indicators provided"))
13-
14-
style, chomp = if block_scalar_len == 2
15-
block_scalar
16-
elseif block_scalar_len == 1
17-
ind = block_scalar[1]
18-
if ind in "fl"
19-
ind, DEFAULT_CHOMP
20-
else
21-
DEFAULT_STYLE, ind
22-
end
23-
else
24-
DEFAULT_STYLE, DEFAULT_CHOMP
25-
end
9+
function block(str::AbstractString; style=DEFAULT_STYLE, chomp=DEFAULT_CHOMP)
10+
block(str, style, chomp)
11+
end
2612

13+
function block(str::AbstractString, style::Symbol, chomp::Symbol=DEFAULT_CHOMP)
2714
# Append an additional, non-space, character to force one more iteration of the style
2815
# loop
2916
str *= ETX
@@ -33,7 +20,7 @@ function block(str::AbstractString, block_scalar::AbstractString="")
3320
prev = curr = '\0'
3421

3522
# Replace newlines with spaces (folded)
36-
if style == 'f'
23+
if style === :folded
3724
# The code below is equivalent to these two regexes:
3825
# ```
3926
# str = replace(str, r"(?<=\S)\n(?=\S)" => " ")
@@ -69,7 +56,7 @@ function block(str::AbstractString, block_scalar::AbstractString="")
6956
end
7057

7158
# Keep newlines (literal)
72-
elseif style == 'l'
59+
elseif style === :literal
7360
for next in str
7461
if curr == '\n'
7562
num_newlines += 1
@@ -86,29 +73,68 @@ function block(str::AbstractString, block_scalar::AbstractString="")
8673
curr = next
8774
end
8875
else
89-
throw(ArgumentError("Unknown block style indicator: $(repr(style))"))
76+
throw(ArgumentError("Unknown block style indicator: $style"))
9077
end
9178

9279
# Single newline at end (clip)
93-
if chomp == 'c'
80+
if chomp === :clip
9481
num_newlines > 0 && write(out, '\n')
9582

9683
# No newline at end (strip)
97-
elseif chomp == 's'
84+
elseif chomp === :strip
9885
# no-op
9986

10087
# All newlines from end (keep)
101-
elseif chomp == 'k'
88+
elseif chomp === :keep
10289
write(out, "\n" ^ num_newlines)
10390
else
104-
throw(ArgumentError("Unknown block chomping indicator: $(repr(chomp))"))
91+
throw(ArgumentError("Unknown block chomping indicator: $chomp"))
10592
end
10693

10794
return String(take!(out))
10895
end
10996

110-
macro blk_str(str::AbstractString, suffix::AbstractString="")
111-
block(str, suffix)
97+
macro blk_str(str::AbstractString, indicators::AbstractString="")
98+
indicators_len = length(indicators)
99+
indicators_len > 2 && throw(ArgumentError("Too many indicators provided"))
100+
101+
# Note: Using '\0` to indicate undefined
102+
style_char, chomp_char = if indicators_len == 2
103+
indicators
104+
elseif indicators_len == 1
105+
ind = indicators[1]
106+
if ind in "fl"
107+
ind, '\0'
108+
else
109+
'\0', ind
110+
end
111+
else
112+
'\0', '\0'
113+
end
114+
115+
style = if style_char == 'f'
116+
:folded
117+
elseif style_char == 'l'
118+
:literal
119+
elseif style_char == '\0'
120+
DEFAULT_STYLE
121+
else
122+
throw(ArgumentError("Unknown block style indicator: $(repr(style_char))"))
123+
end
124+
125+
chomp = if chomp_char == 'c'
126+
:clip
127+
elseif chomp_char == 's'
128+
:strip
129+
elseif chomp_char == 'k'
130+
:keep
131+
elseif chomp_char == '\0'
132+
DEFAULT_CHOMP
133+
else
134+
throw(ArgumentError("Unknown block chomping indicator: $(repr(chomp_char))"))
135+
end
136+
137+
return block(str, style, chomp)
112138
end
113139

114140
end

test/runtests.jl

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,48 @@ end
3333
@testset "BlockScalars.jl" begin
3434
@testset "block: $test" for (test, str) in TEST_STRINGS
3535
@testset "literal" begin
36-
@test block(str, "lk") == yaml_block(str, "|+")
37-
@test block(str, "lc") == yaml_block(str, "|")
38-
@test block(str, "ls") == yaml_block(str, "|-")
36+
@test block(str, :literal, :keep) == yaml_block(str, "|+")
37+
@test block(str, :literal, :clip) == yaml_block(str, "|")
38+
@test block(str, :literal, :strip) == yaml_block(str, "|-")
3939
end
4040

4141
@testset "folding" begin
42-
@test block(str, "fk") == yaml_block(str, ">+")
43-
@test block(str, "fc") == yaml_block(str, ">")
44-
@test block(str, "fs") == yaml_block(str, ">-")
42+
@test block(str, :folded, :keep) == yaml_block(str, ">+")
43+
@test block(str, :folded, :clip) == yaml_block(str, ">")
44+
@test block(str, :folded, :strip) == yaml_block(str, ">-")
4545
end
4646

47-
@testset "default style" begin
48-
@test block(str, "k") == yaml_block(str, ">+")
49-
@test block(str, "c") == yaml_block(str, ">")
50-
@test block(str, "s") == yaml_block(str, ">-")
47+
@testset "default chomp" begin
48+
@test block(str, :literal) == yaml_block(str, "|-")
49+
@test block(str, :folded) == yaml_block(str, ">-")
5150
end
5251

53-
@testset "default chomp" begin
54-
@test block(str, "l") == yaml_block(str, "|-")
55-
@test block(str, "f") == yaml_block(str, ">-")
52+
@testset "default style" begin
53+
@test block(str, chomp=:keep) == yaml_block(str, ">+")
54+
@test block(str, chomp=:clip) == yaml_block(str, ">")
55+
@test block(str, chomp=:strip) == yaml_block(str, ">-")
5656
end
5757

5858
@testset "default" begin
5959
@test block(str) == yaml_block(str, ">-")
6060
end
6161
end
6262

63-
@testset "block invalid indicator" begin
64-
@test_throws ArgumentError block("", "fs_") # Too many indicators
65-
@test_throws ArgumentError block("", "sf") # Order matters
66-
@test_throws ArgumentError block("", "_s") # Invalid style
67-
@test_throws ArgumentError block("", "f_") # Invalid chomp
68-
@test_throws ArgumentError block("", "_") # Invalid style/chomp
63+
# @testset "block invalid indicator" begin
64+
# @test_throws ArgumentError block("", "fs_") # Too many indicators
65+
# @test_throws ArgumentError block("", "sf") # Order matters
66+
# @test_throws ArgumentError block("", "_s") # Invalid style
67+
# @test_throws ArgumentError block("", "f_") # Invalid chomp
68+
# @test_throws ArgumentError block("", "_") # Invalid style/chomp
69+
# end
70+
71+
@testset "@blk_str" begin
72+
@testset "invalid indicators" begin
73+
@test_throws LoadError macroexpand(@__MODULE__, :(@blk_str "" "fs_")) # Too many indicators
74+
@test_throws LoadError macroexpand(@__MODULE__, :(@blk_str "" "sf")) # Order matters
75+
@test_throws LoadError macroexpand(@__MODULE__, :(@blk_str "" "_s")) # Invalid style
76+
@test_throws LoadError macroexpand(@__MODULE__, :(@blk_str "" "f_")) # Invalid chomp
77+
@test_throws LoadError macroexpand(@__MODULE__, :(@blk_str "" "_")) # Invalid style/chomp
78+
end
6979
end
7080
end

0 commit comments

Comments
 (0)