Skip to main content

conjure_cp_core/ast/
metadata.rs

1use crate::ast::ReturnType;
2use polyquine::Quine;
3use proc_macro2::TokenStream;
4use quote::quote;
5use serde::{Deserialize, Serialize};
6use std::fmt::{Debug, Display};
7use std::hash::Hash;
8use std::sync::atomic::{AtomicU64, Ordering};
9use uniplate::derive_unplateable;
10
11derive_unplateable!(Metadata);
12
13pub const NO_HASH: u64 = 0;
14
15#[derive(Debug, Deserialize, Serialize, Default)]
16pub struct Metadata {
17    pub etype: Option<ReturnType>,
18    #[serde(default, skip_serializing_if = "Option::is_none")]
19    pub span_id: Option<u32>,
20    #[serde(default, skip_serializing)]
21    pub stored_hash: AtomicU64,
22}
23
24impl Metadata {
25    pub fn new() -> Metadata {
26        Metadata {
27            etype: None,
28            span_id: None,
29            stored_hash: AtomicU64::new(NO_HASH),
30        }
31    }
32
33    pub fn with_span_id(span_id: u32) -> Metadata {
34        Metadata {
35            etype: None,
36            span_id: Some(span_id),
37            stored_hash: AtomicU64::new(NO_HASH),
38        }
39    }
40}
41
42impl Display for Metadata {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        write!(f, "Metadata")
45    }
46}
47
48impl Hash for Metadata {
49    fn hash<H: std::hash::Hasher>(&self, _state: &mut H) {
50        // Dummy method - Metadata is ignored when hashing an Expression
51    }
52}
53
54impl Clone for Metadata {
55    fn clone(&self) -> Self {
56        Metadata {
57            etype: self.etype.clone(),
58            span_id: self.span_id,
59            stored_hash: AtomicU64::new(self.stored_hash.load(Ordering::Relaxed)),
60        }
61    }
62}
63
64impl PartialEq for Metadata {
65    fn eq(&self, other: &Self) -> bool {
66        self.etype == other.etype
67    }
68}
69
70impl Eq for Metadata {}
71
72impl Quine for Metadata {
73    fn ctor_tokens(&self) -> TokenStream {
74        let etype = self.etype.ctor_tokens();
75        let span_id = self.span_id.ctor_tokens();
76        quote! {
77            conjure_cp::ast::Metadata {
78                etype: #etype,
79                span_id: #span_id,
80                stored_hash: std::sync::atomic::AtomicU64::new(0),
81            }
82        }
83    }
84}