1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use std::{path::PathBuf, str::FromStr};

use borsh::BorshSerialize;
use iroh_base::hash::Hash;
use leaf_protocol_types::*;
use proc_macro::TokenStream;
use quote::{format_ident, quote, quote_spanned, spanned::Spanned};
use unsynn::{Parse, TokenTree};

/// Helper macro to bail out of the macro with a compile error.
macro_rules! throw {
    ($hasSpan:expr, $err:literal) => {
        let span = $hasSpan.__span();
        return quote_spanned!(span =>
            compile_error!($err);
        ).into();
    };
}

type KeyValueAttribute = unsynn::Cons<
    unsynn::Ident,
    Option<
        unsynn::Cons<
            unsynn::Assign,
            unsynn::Either<unsynn::LiteralString, unsynn::PathSepDelimitedVec<unsynn::Ident>>,
        >,
    >,
>;

/// Derive macro for the `Component` trait.
///
/// ```ignore
/// #[derive(BorshSerialize, BorshDeserialize, HasBorshSchema, Component)]
/// #[component(
///     specification = "examples/schemas/ExampleData",
///     schema_id = "ehlbg4aesvav6x4wt4bcdocci323a5cb2jnhyhiizj3qmbaqkk4a"
/// )]
/// struct ExampleData {
///     name: String,
///     age: u8,
///     tags: Vec<String>,
/// }
/// ```
///
/// The attribute options for the `#[component()]` attribute are:
///
/// - `name = "ComponentName"` - Allows you to set the component name in the schema.
/// - `schema_id = "ehlbg4aesvav6x4wt4bcdocci323a5cb2jnhyhiizj3qmbaqkk4a"` - Lets you add an
///   assertion that the resulting schema digest matches the expected value.
/// - `specification = "path/to/schema"` - Lets you specify the path to a directory containing the
///   specification components.
///
/// ## Specification
///
/// The specification is made up of a list of components. The specification directory must contain 1
/// file for each component you want to add to the specification.
///
/// The name of each component must contain the base32 encoded schema ID of the component. It may
/// optionally be prefixed with an identifier ending with an underscore ( `_` ) before the schema
/// ID. It may optionally be suffixed with a `.` and a file extension that will be ignored.
///
/// The macro will ignore any file in the directory starting with a `.` or with `README`.
///
/// The contents of each component file must be in the Borsh format associated to the component's
/// schema ID.
#[proc_macro_derive(Component, attributes(component))]
pub fn derive_component(input: TokenStream) -> TokenStream {
    let input = venial::parse_item(input.into()).unwrap();

    let mut attr_name: Option<String> = None;
    let mut attr_schema_id: Option<String> = None;
    let mut attr_no_check_schema_id = false;
    let mut attr_no_compute_schema_id = false;
    let mut attr_specification: Option<String> = None;

    for attr in input.attributes() {
        if attr.path.len() != 1 {
            continue;
        }
        let TokenTree::Ident(name) = &attr.path[0] else {
            continue;
        };
        if name != &format_ident!("component") {
            continue;
        }

        let mut value =
            unsynn::TokenStream::from_iter(attr.value.get_value_tokens().iter().cloned())
                .into_iter();
        let Ok(key_value_attributes) =
            unsynn::CommaDelimitedVec::<KeyValueAttribute>::parse(&mut value)
        else {
            throw!(attr.value, "Cannot parse attribute");
        };

        let mut ids = Vec::new();
        for key_value in key_value_attributes.0 {
            let key_value = key_value.value;
            let ident = key_value.first;
            let eq_value = key_value.second;

            ids.push(ident.clone());

            if ident == format_ident!("name") {
                if let Some(eq_value) = eq_value {
                    if let unsynn::Either::First(n) = eq_value.second {
                        attr_name = Some(n.as_str().into());
                    } else {
                        throw!(ident, "name should be a string.");
                    }
                } else {
                    throw!(ident, "name requires a value");
                }
            } else if ident == format_ident!("specification") {
                if let Some(eq_value) = eq_value {
                    if let unsynn::Either::First(s) = eq_value.second {
                        attr_specification = Some(s.as_str().into());
                    } else {
                        throw!(ident, "specification should be a string.");
                    }
                } else {
                    throw!(ident, "specification needs a value.");
                }
            } else if ident == "schema_id" {
                if let Some(eq_value) = eq_value {
                    if let unsynn::Either::First(s) = eq_value.second {
                        attr_schema_id = Some(s.as_str().into());
                    } else {
                        throw!(ident, "schema_id should be a string.");
                    }
                } else {
                    throw!(ident, "schema_id needs a value.");
                }
            } else if ident == "no_check_schema_id" {
                if eq_value.is_none() {
                    attr_no_check_schema_id = true;
                } else {
                    throw!(ident, "no_check_schema_id takes no value");
                }
            } else if ident == "no_compute_schema_id" {
                if eq_value.is_none() {
                    attr_no_compute_schema_id = true;
                } else {
                    throw!(ident, "no_compute_schema_id takes no value");
                }
            } else {
                throw!(ident, "unrecognized setting");
            }
        }
    }

    let name = input.name();
    let component_name = if let Some(component_name) = attr_name {
        component_name
    } else {
        name.clone().unwrap().to_string()
    };

    let mut spec_files = Vec::new();
    if let Some(specification) = &attr_specification {
        let cargo_workspace_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
        let specification_dir = cargo_workspace_dir.join(specification);
        let specification_dir_read = std::fs::read_dir(specification_dir).unwrap();
        for entry in specification_dir_read {
            let entry = entry.unwrap();
            let filename = entry.file_name().into_string().unwrap();
            if entry.file_type().unwrap().is_file()
                && !filename.starts_with('.')
                && !filename.starts_with("README")
            {
                spec_files.push(entry.path());
            }
        }
    }

    let expected_schema_id = attr_schema_id.map(|x| Digest(Hash::from_str(&x).unwrap()));
    let schema_id = if !attr_no_compute_schema_id {
        let spec_hash: Digest = {
            let components = spec_files
                .into_iter()
                .map(|path| {
                    let schema_id_str = path.file_name().unwrap().to_str().unwrap();
                    let schema_id_str = if let Some((_prefix, id)) = schema_id_str.rsplit_once('_')
                    {
                        if let Some((id, _suffix)) = id.split_once('.') {
                            id
                        } else {
                            id
                        }
                    } else {
                        schema_id_str
                    };
                    let schema_id = Digest(Hash::from_str(schema_id_str).unwrap());
                    let mut buf = Vec::new();
                    let data = std::fs::read(&path).unwrap();
                    ComponentKind::Unencrypted(ComponentData {
                        schema: schema_id,
                        data,
                    })
                    .serialize(&mut buf)
                    .unwrap();
                    let component_id = Digest(Hash::from(iroh_blake3::hash(&buf)));

                    ComponentEntry {
                        schema_id: Some(schema_id),
                        component_id,
                    }
                })
                .collect::<Vec<_>>();
            let mut entity = Entity { components };
            entity.sort_components();
            entity.compute_digest()
        };

        let mut schema_bytes = Vec::new();
        (&component_name, spec_hash)
            .serialize(&mut schema_bytes)
            .unwrap();

        Digest::new(&schema_bytes)
    } else if let Some(expected) = expected_schema_id {
        expected
    } else {
        throw!(
            name,
            "You must either provide a schema ID with a `no_compute_schema_id` flag,\
            or add a `no_check_schema_id` and allow it to be computed"
        );
    };
    let schema_id_bytes = *schema_id.0.as_bytes();

    if !attr_no_check_schema_id && !attr_no_compute_schema_id {
        let expected = expected_schema_id.unwrap();
        if schema_id != expected {
            panic!(
                "Computed schema ID does not match expected:\
                \ncomputed:{schema_id}\nexpected:{expected}"
            )
        }
    }

    quote! {
        impl Component for #name {
            fn schema_id() -> Digest {
                Digest::from_bytes([#(#schema_id_bytes),*])
            }
        }
    }
    .into()
}

/// Derive macro fro the `HasBorshSchema` trait.
///
/// [`HasBorshSchema`] is required to implement [`Component`], and returns the borsh schema for the
/// Rust type that can be used for the component specification.
#[proc_macro_derive(HasBorshSchema)]
pub fn derive_has_borsh_schema(input: TokenStream) -> TokenStream {
    let input = venial::parse_item(input.into()).unwrap();

    let Some(name) = input.name() else {
        throw!(input, "Missing struct/enum name.");
    };

    fn fields_schema_expr(fields: &venial::Fields) -> proc_macro2::TokenStream {
        match fields {
            venial::Fields::Unit => {
                quote! {
                    BorshSchema::Null
                }
            }
            venial::Fields::Tuple(fields) => {
                if fields.fields.len() != 1 {
                    throw!(
                        fields,
                        "Only tuples with one field may be used in BorshSchemas, \
                        and the type of the field in the schema will \
                        be that of the inner type in that case."
                    );
                }
                let (field, _punct) = &fields.fields[0];
                let ty = &field.ty;
                quote! { <#ty>::borsh_schema() }
            }
            venial::Fields::Named(fields) => {
                let mut field_exprs = Vec::new();
                for field in fields.fields.items() {
                    let name = &field.name;
                    let ty = &field.ty;
                    field_exprs.push(quote! {
                       (stringify!(#name).to_string(), <#ty>::borsh_schema())
                    });
                }
                quote! { BorshSchema::Struct { fields: vec![#(#field_exprs),*] } }
            }
        }
    }

    let schema_expr = match input {
        venial::Item::Struct(s) => fields_schema_expr(&s.fields),
        venial::Item::Enum(e) => {
            let mut variant_exprs = Vec::new();
            for variant in e.variants.items() {
                let name = &variant.name;
                let fields_schema = fields_schema_expr(&variant.fields);
                variant_exprs.push(quote! { ( stringify!(#name).to_string(), #fields_schema) });
            }
            quote! { BorshSchema::Enum { variants: vec![#(#variant_exprs),*] } }
        }
        _ => {
            throw!(
                name,
                "You may only derive HasBorshSchema on Structs, and Enums"
            );
        }
    };

    quote! {
        impl HasBorshSchema for #name {
            fn borsh_schema() -> BorshSchema {
                #schema_expr
            }
        }
    }
    .into()
}