Skip to content

Commit c4f0df8

Browse files
authored
refactor(auth-ref): make AuthEntryReference typed (#389)
1 parent a98fba4 commit c4f0df8

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

src/base/spec.rs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,63 @@ pub struct SimpleSemanticsQueryHandlerSpec {
296296
pub default_similarity_metric: VectorSimilarityMetric,
297297
}
298298

299-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
300-
pub struct AuthEntryReference {
299+
pub struct AuthEntryReference<T> {
301300
pub key: String,
301+
_phantom: std::marker::PhantomData<T>,
302+
}
303+
304+
impl<T> std::fmt::Debug for AuthEntryReference<T> {
305+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
306+
write!(f, "AuthEntryReference({})", self.key)
307+
}
308+
}
309+
310+
impl<T> std::fmt::Display for AuthEntryReference<T> {
311+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
312+
write!(f, "AuthEntryReference({})", self.key)
313+
}
314+
}
315+
316+
impl<T> Clone for AuthEntryReference<T> {
317+
fn clone(&self) -> Self {
318+
Self {
319+
key: self.key.clone(),
320+
_phantom: std::marker::PhantomData,
321+
}
322+
}
323+
}
324+
325+
impl<T> Serialize for AuthEntryReference<T> {
326+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
327+
where
328+
S: serde::Serializer,
329+
{
330+
self.key.serialize(serializer)
331+
}
332+
}
333+
334+
impl<'de, T> Deserialize<'de> for AuthEntryReference<T> {
335+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
336+
where
337+
D: serde::Deserializer<'de>,
338+
{
339+
Ok(Self {
340+
key: String::deserialize(deserializer)?,
341+
_phantom: std::marker::PhantomData,
342+
})
343+
}
344+
}
345+
346+
impl<T> PartialEq for AuthEntryReference<T> {
347+
fn eq(&self, other: &Self) -> bool {
348+
self.key == other.key
349+
}
350+
}
351+
352+
impl<T> Eq for AuthEntryReference<T> {}
353+
354+
impl<T> std::hash::Hash for AuthEntryReference<T> {
355+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
356+
self.key.hash(state);
357+
}
302358
}

src/ops/storages/neo4j.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ pub struct ConnectionSpec {
2424

2525
#[derive(Debug, Deserialize)]
2626
pub struct Spec {
27-
connection: spec::AuthEntryReference,
27+
connection: spec::AuthEntryReference<ConnectionSpec>,
2828
mapping: GraphElementMapping,
2929
}
3030

3131
#[derive(Debug, Deserialize)]
3232
pub struct Declaration {
33-
connection: spec::AuthEntryReference,
33+
connection: spec::AuthEntryReference<ConnectionSpec>,
3434
#[serde(flatten)]
3535
decl: GraphDeclarations,
3636
}
@@ -92,7 +92,7 @@ impl std::fmt::Display for ElementType {
9292

9393
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
9494
pub struct GraphElement {
95-
connection: AuthEntryReference,
95+
connection: AuthEntryReference<ConnectionSpec>,
9696
typ: ElementType,
9797
}
9898

@@ -156,7 +156,7 @@ struct AnalyzedNodeLabelInfo {
156156
}
157157

158158
pub struct ExportContext {
159-
connection_ref: AuthEntryReference,
159+
connection_ref: AuthEntryReference<ConnectionSpec>,
160160
graph: Arc<Graph>,
161161

162162
create_order: u8,

src/setup/auth_registry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl AuthRegistry {
3232
Ok(())
3333
}
3434

35-
pub fn get<T: DeserializeOwned>(&self, entry_ref: &spec::AuthEntryReference) -> Result<T> {
35+
pub fn get<T: DeserializeOwned>(&self, entry_ref: &spec::AuthEntryReference<T>) -> Result<T> {
3636
let entries = self.entries.read().unwrap();
3737
match entries.get(&entry_ref.key) {
3838
Some(value) => Ok(serde_json::from_value(value.clone())?),

0 commit comments

Comments
 (0)