diskann_benchmark_runner/input.rs
1/*
2 * Copyright (c) Microsoft Corporation.
3 * Licensed under the MIT license.
4 */
5
6use crate::{Any, Checker};
7
8pub trait Input {
9 /// Return the discriminant associated with this type.
10 ///
11 /// This is used to map inputs types to their respective parsers.
12 ///
13 /// Well formed implementations should always return the same result.
14 fn tag() -> &'static str;
15
16 /// Attempt to deserialize an opaque object from the raw `serialized` representation.
17 ///
18 /// Deserialized values can be constructed and returned via [`Checker::any`],
19 /// [`Any::new`] or [`Any::raw`].
20 ///
21 /// If using the [`Any`] constructors directly, implementations should associate
22 /// [`Self::tag`] with the returned `Any`. If [`Checker::any`] is used - this will
23 /// happen automatically.
24 ///
25 /// Implementations are **strongly** encouraged to implement
26 /// [`CheckDeserialization`](crate::CheckDeserialization) and use this API to ensure
27 /// shared resources (like input files or output files) are correctly resolved and
28 /// properly shared among all jobs in a benchmark run.
29 fn try_deserialize(
30 serialized: &serde_json::Value,
31 checker: &mut Checker,
32 ) -> anyhow::Result<Any>;
33
34 /// Print an example JSON representation of objects this input is expected to parse.
35 ///
36 /// Well-formed implementations should ensure that passing the returned
37 /// [`serde_json::Value`] back to [`Self::try_deserialize`] correctly deserializes,
38 /// though it need not necessarily pass
39 /// [`CheckDeserialization`](crate::CheckDeserialization).
40 fn example() -> anyhow::Result<serde_json::Value>;
41}
42
43/// A registered input. See [`crate::registry::Inputs::get`].
44#[derive(Clone, Copy)]
45pub struct Registered<'a>(pub(crate) &'a dyn DynInput);
46
47impl Registered<'_> {
48 /// Return the input tag of the registered input.
49 ///
50 /// See: [`Input::tag`].
51 pub fn tag(&self) -> &'static str {
52 self.0.tag()
53 }
54
55 /// Try to deserialize raw JSON into the dynamic type of the input.
56 ///
57 /// See: [`Input::try_deserialize`].
58 pub fn try_deserialize(
59 &self,
60 serialized: &serde_json::Value,
61 checker: &mut Checker,
62 ) -> anyhow::Result<Any> {
63 self.0.try_deserialize(serialized, checker)
64 }
65
66 /// Return an example JSON for the dynamic type of the input.
67 ///
68 /// See: [`Input::example`].
69 pub fn example(&self) -> anyhow::Result<serde_json::Value> {
70 self.0.example()
71 }
72}
73
74impl std::fmt::Debug for Registered<'_> {
75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76 f.debug_struct("input::Registered")
77 .field("tag", &self.tag())
78 .finish()
79 }
80}
81
82//////////////
83// Internal //
84//////////////
85
86#[derive(Debug)]
87pub(crate) struct Wrapper<T>(std::marker::PhantomData<T>);
88
89impl<T> Wrapper<T> {
90 pub(crate) const INSTANCE: Self = Self::new();
91
92 pub(crate) const fn new() -> Self {
93 Self(std::marker::PhantomData)
94 }
95}
96
97impl<T> Clone for Wrapper<T> {
98 fn clone(&self) -> Self {
99 *self
100 }
101}
102
103impl<T> Copy for Wrapper<T> {}
104
105pub(crate) trait DynInput {
106 fn tag(&self) -> &'static str;
107 fn try_deserialize(
108 &self,
109 serialized: &serde_json::Value,
110 checker: &mut Checker,
111 ) -> anyhow::Result<Any>;
112 fn example(&self) -> anyhow::Result<serde_json::Value>;
113}
114
115impl<T> DynInput for Wrapper<T>
116where
117 T: Input,
118{
119 fn tag(&self) -> &'static str {
120 T::tag()
121 }
122 fn try_deserialize(
123 &self,
124 serialized: &serde_json::Value,
125 checker: &mut Checker,
126 ) -> anyhow::Result<Any> {
127 T::try_deserialize(serialized, checker)
128 }
129 fn example(&self) -> anyhow::Result<serde_json::Value> {
130 T::example()
131 }
132}