This repository was archived by the owner on Feb 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcommon.rs
More file actions
76 lines (67 loc) · 2.39 KB
/
common.rs
File metadata and controls
76 lines (67 loc) · 2.39 KB
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
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use std::collections::BTreeMap;
/// An interface for generating serialzer and deserializers based on
/// field descriptions.
pub trait Descriptor {
/// Returns the serializer code block as a token stream.
fn generate_serializer(&self) -> proc_macro2::TokenStream;
/// Returns the deserializer code block as a token stream.
fn generate_deserializer(&self) -> proc_macro2::TokenStream;
}
/// Describes a structure and it's fields.
pub(crate) struct GenericDescriptor<T> {
// The structure type identifier.
pub ty: syn::Ident,
pub versions: BTreeMap<u64, Vec<u64>>,
pub fields: Vec<T>,
}
// A trait that defines an interface to check if a certain field
// exists at a specified version.
pub(crate) trait Exists {
fn exists_at(&self, minor: u64, patch: u64) -> bool {
let start = self.start_version();
let end = self.end_version();
let default_start = || -> bool {
if start.is_empty() {
return true;
} else if start.iter().all(|x| minor < x.minor) {
return false;
} else if start.iter().all(|x| minor > x.minor) {
return true;
}
return false;
};
let default_end = || -> bool {
if end.is_empty() {
return true;
} else if end.iter().all(|x| minor < x.minor) {
return true;
} else if end.iter().all(|x| minor > x.minor) {
return false;
}
return false;
};
start
.iter()
.find(|list| list.minor == minor)
.map_or_else(default_start, |found| patch >= found.patch)
&& end
.iter()
.find(|list| list.minor == minor)
.map_or_else(default_end, |found| patch < found.patch)
}
fn list_versions(&self) -> Vec<semver::Version> {
let mut rets = self.start_version().to_owned();
rets.append(&mut self.end_version().to_owned());
rets.sort();
rets.dedup();
rets
}
fn start_version(&self) -> &[semver::Version];
fn end_version(&self) -> &[semver::Version];
}
// A trait that defines an interface for exposing a field type.
pub(crate) trait FieldType {
fn ty(&self) -> syn::Type;
}