Skip to content

Commit 70135b5

Browse files
committed
toon line numbers
1 parent cd80f72 commit 70135b5

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

include/jsoncons_ext/toon/decode_toon.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ try_decode_toon(const StringViewLike& s,
3636

3737
json_decoder<T> decoder;
3838
toon_string_reader reader(s, decoder, options);
39-
std::error_code ec;
4039
auto result = reader.try_read();
4140
if (!result)
4241
{
@@ -68,7 +67,6 @@ try_decode_toon(std::istream& is,
6867
using value_type = T;
6968
using result_type = read_result<value_type>;
7069

71-
std::error_code ec;
7270
jsoncons::json_decoder<T> decoder;
7371
toon_stream_reader reader(is, decoder, options);
7472

include/jsoncons_ext/toon/toon_reader.hpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -897,12 +897,13 @@ std::size_t compute_depth_from_indent(std::size_t indent_spaces, std::size_t ind
897897
}
898898

899899
inline
900-
void read_lines(jsoncons::string_view raw,
900+
jsoncons::expected<void,jsoncons::read_error> read_lines(jsoncons::string_view raw,
901901
const toon_decode_options& options,
902902
std::vector<parsed_line>& lines,
903-
std::vector<blank_line_info>& blank_lines,
904-
std::error_code& ec)
903+
std::vector<blank_line_info>& blank_lines)
905904
{
905+
using result_type = jsoncons::expected<void,jsoncons::read_error>;
906+
906907
std::size_t indent_size = options.indent();
907908
bool strict = options.strict();
908909

@@ -922,8 +923,7 @@ void read_lines(jsoncons::string_view raw,
922923
{
923924
if (strict)
924925
{
925-
ec = toon_errc::tab_in_indentation;
926-
return;
926+
return result_type{jsoncons::unexpect, toon_errc::tab_in_indentation, i+1, 0};
927927
}
928928
else
929929
{
@@ -955,8 +955,7 @@ void read_lines(jsoncons::string_view raw,
955955
{
956956
if (strict && !is_blank_line && indent > 0 && indent % indent_size !=0)
957957
{
958-
ec = toon_errc::indent_not_multiple_of_indent_size;
959-
return;
958+
return result_type{jsoncons::unexpect, toon_errc::indent_not_multiple_of_indent_size, i+1, 0};
960959
}
961960
std::size_t depth = compute_depth_from_indent(indent, indent_size);
962961
if (is_blank_line)
@@ -975,8 +974,7 @@ void read_lines(jsoncons::string_view raw,
975974
{
976975
if (strict && !is_blank_line && indent > 0 && indent % indent_size != 0)
977976
{
978-
ec = toon_errc::indent_not_multiple_of_indent_size;
979-
return;
977+
return result_type{jsoncons::unexpect, toon_errc::indent_not_multiple_of_indent_size, i+1, 0};
980978
}
981979
std::size_t depth = compute_depth_from_indent(indent, indent_size);
982980
if (is_blank_line)
@@ -986,6 +984,7 @@ void read_lines(jsoncons::string_view raw,
986984
lines.push_back(parsed_line{depth, indent, jsoncons::string_view{raw.data()+(start+indent), i-(start+indent+trailing_blanks)}, line_num});
987985
}
988986

987+
return result_type{};
989988
}
990989

991990
inline
@@ -1674,7 +1673,6 @@ class basic_toon_reader
16741673
{
16751674
using result_type = jsoncons::expected<void, read_error>;
16761675

1677-
std::error_code ec;
16781676
std::string raw;
16791677
std::vector<parsed_line> lines;
16801678
std::vector<blank_line_info> blank_lines;
@@ -1685,10 +1683,10 @@ class basic_toon_reader
16851683
raw.append(s.data(), s.size());
16861684
}
16871685

1688-
read_lines(raw, options_, lines, blank_lines, ec);
1689-
if (ec)
1686+
auto r_lines = read_lines(raw, options_, lines, blank_lines);
1687+
if (!r_lines)
16901688
{
1691-
return result_type{jsoncons::unexpect, ec};
1689+
return result_type{jsoncons::unexpect, r_lines.error()};
16921690
}
16931691
std::vector<parsed_line> non_blank_lines;
16941692
for (const auto& ln : lines)

test/toon/src/toon_reader_tests.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ TEST_CASE("toon_reader parse_header tests")
305305
1,Blue Lake Trail,7.5,320,ana,true
306306
2,Ridge Overlook,9.2,540,luis,false
307307
3,Wildflower Loop,5.1,180,sam,true)";
308-
std::error_code ec;
309-
toon::read_lines(raw, options, lines, blank_lines, ec);
308+
auto r = toon::read_lines(raw, options, lines, blank_lines);
309+
REQUIRE(r);
310310

311311
REQUIRE(4 == lines.size());
312312

@@ -344,7 +344,6 @@ TEST_CASE("toon_reader tests")
344344
auto expected = jsoncons::ojson::parse(R"([" foo", "baz" ,"bar ",1,true,false,null])");
345345

346346
std::string data = R"([7]: " foo", baz ,"bar ",1,true,false,null)";
347-
std::error_code ec;
348347

349348
jsoncons::json_decoder<jsoncons::ojson> decoder;
350349
toon::toon_string_reader reader(data, decoder);
@@ -364,7 +363,6 @@ TEST_CASE("toon_reader tests")
364363
std::string data = R"([2]{id,name,role}:
365364
1,Alice,admin
366365
2,Bob,user)";
367-
std::error_code ec;
368366

369367
jsoncons::json_decoder<jsoncons::ojson> decoder;
370368
toon::toon_string_reader reader(data, decoder);
@@ -386,7 +384,6 @@ TEST_CASE("toon_reader tests")
386384
- 1
387385
- a: 1
388386
- text)";
389-
std::error_code ec;
390387

391388
jsoncons::json_decoder<jsoncons::ojson> decoder;
392389
toon::toon_string_reader reader(data, decoder);
@@ -406,7 +403,6 @@ TEST_CASE("toon_reader tests")
406403
std::string data = R"(task: Our favorite hikes together
407404
location: Boulder
408405
season: spring_2025)";
409-
std::error_code ec;
410406

411407
jsoncons::json_decoder<jsoncons::ojson> decoder;
412408
toon::toon_string_reader reader(data, decoder);
@@ -479,7 +475,6 @@ season: spring_2025)";
479475
THING-C,3,15
480476
total: 45
481477
status: delivered)";
482-
std::error_code ec;
483478

484479
jsoncons::json_decoder<jsoncons::ojson> decoder;
485480
toon::toon_string_reader reader(data, decoder);
@@ -497,7 +492,6 @@ season: spring_2025)";
497492
- first
498493
- second
499494
- )";
500-
std::error_code ec;
501495

502496
jsoncons::json_decoder<jsoncons::ojson> decoder;
503497
toon::toon_string_reader reader(data, decoder);
@@ -518,7 +512,6 @@ season: spring_2025)";
518512
std::string data = R"(items[1]:
519513
- name: Ada
520514
data[0]:)";
521-
std::error_code ec;
522515

523516
jsoncons::json_decoder<jsoncons::ojson> decoder;
524517
toon::toon_string_reader reader(data, decoder);
@@ -540,8 +533,6 @@ season: spring_2025)";
540533
- id: 2
541534
- status: draft)";
542535

543-
std::error_code ec;
544-
545536
jsoncons::json_decoder<jsoncons::ojson> decoder;
546537
toon::toon_string_reader reader(data, decoder);
547538
reader.read();
@@ -596,6 +587,34 @@ season: spring_2025)";
596587

597588
TEST_CASE("toon_reader errors")
598589
{
590+
SECTION("indentation errors")
591+
{
592+
std::string expected = "Indent spaces must be exact multiple of indent size at line 11";
593+
594+
std::string data = "a:\n b: 1";
595+
jsoncons::json_decoder<jsoncons::ojson> decoder;
596+
auto options = toon::toon_options{}.strict(true);
597+
toon::toon_string_reader reader(data, decoder, options);
598+
599+
auto result = reader.try_read();
600+
REQUIRE_FALSE(result);
601+
CHECK(expected == result.error().message());
602+
//std::cout << result.error().message() << "\n";
603+
}
604+
SECTION("blank lines")
605+
{
606+
std::string expected = "Blank lines not allowed inside arrays at line 3";
607+
608+
std::string data = "items[3]:\n - a\n\n - b\n - c";
609+
jsoncons::json_decoder<jsoncons::ojson> decoder;
610+
auto options = toon::toon_options{}.strict(true);
611+
toon::toon_string_reader reader(data, decoder, options);
612+
613+
auto result = reader.try_read();
614+
REQUIRE_FALSE(result);
615+
CHECK(expected == result.error().message());
616+
//std::cout << result.error().message() << "\n";
617+
}
599618
SECTION("test1")
600619
{
601620
std::string expected = "Inline array length mismatch at line 1";
@@ -605,7 +624,6 @@ TEST_CASE("toon_reader errors")
605624
auto options = toon::toon_options{}.strict(true);
606625
toon::toon_string_reader reader(data, decoder, options);
607626

608-
std::error_code ec;
609627
auto result = reader.try_read();
610628
REQUIRE_FALSE(result);
611629
CHECK(expected == result.error().message());

0 commit comments

Comments
 (0)