OCaml library for JSONfeed parsing and creation
1(*---------------------------------------------------------------------------
2 Copyright (c) 2024 Anil Madhavapeddy. All rights reserved.
3 SPDX-License-Identifier: ISC
4 ---------------------------------------------------------------------------*)
5
6(** Attachments for JSON Feed items.
7
8 An attachment represents an external resource related to a feed item,
9 such as audio files for podcasts, video files, or other downloadable content.
10 Attachments with identical titles indicate alternate formats of the same resource.
11
12 @see <https://www.jsonfeed.org/version/1.1/> JSON Feed Specification *)
13
14
15(** The type representing an attachment. *)
16type t
17
18
19(** {1 Unknown Fields} *)
20
21module Unknown : sig
22 type t = (string * Jsont.json) list
23 (** Unknown/unrecognized JSON object members.
24 Useful for preserving fields from custom extensions or future spec versions. *)
25
26 val empty : t
27 (** [empty] is the empty list of unknown fields. *)
28
29 val is_empty : t -> bool
30 (** [is_empty u] returns [true] if there are no unknown fields. *)
31end
32
33
34(** {1 Jsont Type} *)
35
36val jsont : t Jsont.t
37(** Declarative JSON type for attachments.
38
39 Maps JSON objects with "url" (required), "mime_type" (required),
40 and optional "title", "size_in_bytes", "duration_in_seconds" fields. *)
41
42
43(** {1 Construction} *)
44
45val create :
46 url:string ->
47 mime_type:string ->
48 ?title:string ->
49 ?size_in_bytes:int64 ->
50 ?duration_in_seconds:int ->
51 ?unknown:Unknown.t ->
52 unit ->
53 t
54(** [create ~url ~mime_type ?title ?size_in_bytes ?duration_in_seconds ?unknown ()]
55 creates an attachment object.
56
57 @param url The location of the attachment (required)
58 @param mime_type The MIME type of the attachment, e.g. ["audio/mpeg"] (required)
59 @param title The name of the attachment; identical titles indicate alternate formats
60 of the same resource
61 @param size_in_bytes The size of the attachment file in bytes
62 @param duration_in_seconds The duration of the attachment in seconds (for audio/video)
63 @param unknown Unknown/custom fields for extensions (default: empty)
64
65 {b Examples:}
66 {[
67 (* Simple attachment *)
68 let att = Attachment.create
69 ~url:"https://example.com/episode.mp3"
70 ~mime_type:"audio/mpeg" ()
71
72 (* Podcast episode with metadata *)
73 let att = Attachment.create
74 ~url:"https://example.com/episode.mp3"
75 ~mime_type:"audio/mpeg"
76 ~title:"Episode 42"
77 ~size_in_bytes:15_728_640L
78 ~duration_in_seconds:1800 ()
79 ]} *)
80
81
82(** {1 Accessors} *)
83
84val url : t -> string
85(** [url t] returns the attachment's URL. *)
86
87val mime_type : t -> string
88(** [mime_type t] returns the attachment's MIME type. *)
89
90val title : t -> string option
91(** [title t] returns the attachment's title, if set. *)
92
93val size_in_bytes : t -> int64 option
94(** [size_in_bytes t] returns the attachment's size in bytes, if set. *)
95
96val duration_in_seconds : t -> int option
97(** [duration_in_seconds t] returns the attachment's duration, if set. *)
98
99val unknown : t -> Unknown.t
100(** [unknown t] returns unrecognized fields from the JSON. *)
101
102
103(** {1 Comparison} *)
104
105val equal : t -> t -> bool
106(** [equal a b] tests equality between two attachments. *)
107
108
109(** {1 Pretty Printing} *)
110
111val pp : Format.formatter -> t -> unit
112(** [pp ppf t] pretty prints an attachment to the formatter.
113
114 The output is human-readable and suitable for debugging.
115
116 {b Example output:}
117 {v episode.mp3 (audio/mpeg, 15.0 MB, 30m0s) v} *)