OCaml library for JSONfeed parsing and creation

switch error types

+1 -1
README.md
···
| Some title -> Printf.printf "- %s\n" title
| None -> ()
) (Jsonfeed.items feed)
-
| Error (`Msg err) ->
+
| Error err ->
Printf.eprintf "Parse error: %s\n" err
(* Parse from file *)
+2 -2
example/feed_parser.ml
···
Format.printf "✓ Round-trip successful: feeds are equal\n"
else
Format.printf "✗ Round-trip failed: feeds differ\n"
-
| Error (`Msg err) ->
+
| Error err ->
Format.eprintf "✗ Round-trip failed: %s\n" err)
-
| Error (`Msg err) ->
+
| Error err ->
Format.eprintf "Error parsing feed: %s\n" err
with
| Sys_error msg ->
+3 -3
example/feed_validator.ml
···
(match Jsonfeed.of_string invalid_json1 with
| Ok _ -> Format.printf "✗ Should have failed (missing version)\n"
-
| Error (`Msg err) ->
+
| Error err ->
Format.printf "✓ Correctly rejected invalid feed: %s\n" err);
(* Missing required title field *)
···
(match Jsonfeed.of_string invalid_json2 with
| Ok _ -> Format.printf "✗ Should have failed (missing title)\n"
-
| Error (`Msg err) ->
+
| Error err ->
Format.printf "✓ Correctly rejected invalid feed: %s\n" err);
(* Item without id *)
···
(match Jsonfeed.of_string invalid_json3 with
| Ok _ -> Format.printf "✗ Should have failed (item without id)\n"
-
| Error (`Msg err) ->
+
| Error err ->
Format.printf "✓ Correctly rejected invalid feed: %s\n" err);
Format.printf "\n"
+2 -3
lib/jsonfeed.ml
···
(* JSON parsing and serialization *)
-
type error = [ `Msg of string ]
+
type error = string
-
let error_msgf fmt = Format.kasprintf (fun s -> Error (`Msg s)) fmt
+
let error_msgf fmt = Format.kasprintf (fun s -> Error s) fmt
(* JSON parsing helpers *)
···
(* JSON serialization *)
let to_jsonm enc feed =
-
(* Simplified serialization using Jsonm *)
let enc_field name value_fn =
ignore (Jsonm.encode enc (`Lexeme (`Name name)));
value_fn ()
+8 -15
lib/jsonfeed.mli
···
type-safe parsing and serialization of JSON Feed documents. JSON Feed is a
syndication format similar to RSS and Atom, but using JSON instead of XML.
-
{b Key Features:}
-
- Type-safe construction with compile-time validation
-
- Support for all JSON Feed 1.1 fields
-
- RFC 3339 date parsing with Ptime integration
-
- Streaming parsing and serialization with Jsonm
-
- Comprehensive documentation and examples
-
{b Quick Start:}
{[
(* Create a simple feed *)
···
(* Parse from string *)
match Jsonfeed.of_string json with
| Ok feed -> Printf.printf "Feed: %s\n" (Jsonfeed.title feed)
-
| Error (`Msg err) -> Printf.eprintf "Error: %s\n" err
+
| Error err -> Printf.eprintf "Error: %s\n" err
]}
@see <https://www.jsonfeed.org/version/1.1/> JSON Feed Specification *)
···
(** {1 Parsing and Serialization} *)
(** Error type for parsing operations. *)
-
type error = [ `Msg of string ]
+
type error = string
(** [of_jsonm decoder] parses a JSON Feed from a Jsonm decoder.
···
with streaming JSON processing pipelines.
@param decoder A Jsonm decoder positioned at the start of a JSON Feed document
-
@return [Ok feed] on success, [Error (`Msg err)] on parse error
+
@return [Ok feed] on success, [Error err] on parse error
{b Example:}
{[
let decoder = Jsonm.decoder (`String json_string) in
match Jsonfeed.of_jsonm decoder with
| Ok feed -> (* process feed *)
-
| Error (`Msg err) -> (* handle error *)
+
| Error err -> (* handle error *)
]} *)
-
val of_jsonm : Jsonm.decoder -> (t, [> error]) result
+
val of_jsonm : Jsonm.decoder -> (t, error) result
(** [to_jsonm encoder feed] serializes a JSON Feed to a Jsonm encoder.
···
(** [of_string s] parses a JSON Feed from a string.
@param s A JSON string containing a JSON Feed document
-
@return [Ok feed] on success, [Error (`Msg err)] on parse error
+
@return [Ok feed] on success, [Error err] on parse error
{b Example:}
{[
···
}|} in
match Jsonfeed.of_string json with
| Ok feed -> Printf.printf "Parsed: %s\n" (Jsonfeed.title feed)
-
| Error (`Msg err) -> Printf.eprintf "Error: %s\n" err
+
| Error err -> Printf.eprintf "Error: %s\n" err
]} *)
-
val of_string : string -> (t, [> error]) result
+
val of_string : string -> (t, error) result
(** [to_string ?minify feed] serializes a JSON Feed to a string.
+4 -4
test/test_jsonfeed.ml
···
| Ok feed ->
Alcotest.(check string) "title" "Test Feed" (Jsonfeed.title feed);
Alcotest.(check int) "items" 0 (List.length (Jsonfeed.items feed))
-
| Error (`Msg err) ->
+
| Error err ->
Alcotest.fail (Printf.sprintf "Parse failed: %s" err)
let test_feed_parse_with_item () =
···
Alcotest.(check string) "item id" "https://example.com/1" (Item.id item);
Alcotest.(check (option string)) "content_html" (Some "<p>Hello</p>") (Item.content_html item)
| _ -> Alcotest.fail "Expected 1 item")
-
| Error (`Msg err) ->
+
| Error err ->
Alcotest.fail (Printf.sprintf "Parse failed: %s" err)
let test_feed_roundtrip () =
···
Alcotest.(check int) "items count"
(List.length (Jsonfeed.items feed1))
(List.length (Jsonfeed.items feed2))
-
| Error (`Msg err) ->
+
| Error err ->
Alcotest.fail (Printf.sprintf "Round-trip failed: %s" err)
let test_feed_parse_invalid_missing_content () =
···
}|} in
match Jsonfeed.of_string json with
| Ok _ -> Alcotest.fail "Should reject item without content"
-
| Error (`Msg err) ->
+
| Error err ->
Alcotest.(check bool) "has error" true
(contains_substring err "content")