-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathmain.rs
More file actions
156 lines (129 loc) · 4.73 KB
/
main.rs
File metadata and controls
156 lines (129 loc) · 4.73 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
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
// Copyright 2025 Oxide Computer Company
// Include the generated code to make sure it compiles.
include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
fn main() {}
#[test]
fn test_with_set() {
// Validate that a set is currently represented as a Vec
// See type_entry.rs
let _ = WithSet { set: Vec::new() };
}
#[test]
fn test_ipnetwork() {
// ipnetwork::IpNetwork is a moderately complex type for us to handle. In
// particular it's a oneOf with both variants as strings, but with mutually
// incompatible patterns. This tests that our generated Deserialize impl
// does the right thing.
assert!(Ipv4Network::try_from("192.168.0.0/24").is_ok());
assert!(Ipv6Network::try_from("192.168.0.0/24").is_err());
assert!(Ipv6Network::try_from("fc00::/7").is_ok());
assert!(Ipv4Network::try_from("fc00::/7").is_err());
let v4: IpNetwork = serde_json::from_str(r#""192.168.0.0/24""#).unwrap();
assert!(matches!(v4, IpNetwork::V4(_)));
let v6: IpNetwork = serde_json::from_str(r#""fc00::/7""#).unwrap();
assert!(matches!(v6, IpNetwork::V6(_)));
let v4 = IpNetwork::try_from("192.168.0.0/24").unwrap();
assert!(matches!(v4, IpNetwork::V4(_)));
let v6 = IpNetwork::try_from("fc00::/7").unwrap();
assert!(matches!(v6, IpNetwork::V6(_)));
}
#[test]
fn test_string_constraints() {
assert!(LoginName::try_from("").is_err());
assert!(LoginName::try_from("abcdefghi").is_err());
assert!(LoginName::try_from("offby1").is_err());
assert!(LoginName::try_from("ahl").is_ok());
}
#[test]
fn test_string_constraints_for_non_ascii_chars() {
assert!(NonAsciiChars::try_from("🍔🍔🍔🍔🍔🍔🍔🍔").is_ok());
assert!(NonAsciiChars::try_from("🍔").is_err());
}
#[test]
fn test_integer_bounds_try_from() {
// BoundedUint: u8 in [1, 63].
assert!(BoundedUint::try_from(0u8).is_err());
assert!(BoundedUint::try_from(1u8).is_ok());
assert!(BoundedUint::try_from(63u8).is_ok());
assert!(BoundedUint::try_from(64u8).is_err());
assert!(BoundedUint::try_from(255u8).is_err());
// Verify the inner value round-trips.
let v = BoundedUint::try_from(42u8).unwrap();
assert_eq!(*v, 42u8);
assert_eq!(u8::from(v), 42u8);
}
#[test]
fn test_integer_bounds_negative() {
// BoundedNegative: i16 in [-50, -10].
assert!(BoundedNegative::try_from(-51i16).is_err());
assert!(BoundedNegative::try_from(-50i16).is_ok());
assert!(BoundedNegative::try_from(-10i16).is_ok());
assert!(BoundedNegative::try_from(-9i16).is_err());
assert!(BoundedNegative::try_from(0i16).is_err());
}
#[test]
fn test_integer_bounds_inferred() {
// InferredUint: no format hint, [0, 63] -> inferred u8.
assert!(InferredUint::try_from(0u8).is_ok());
assert!(InferredUint::try_from(63u8).is_ok());
assert!(InferredUint::try_from(64u8).is_err());
}
#[test]
fn test_integer_bounds_serde() {
// Deserialization should enforce bounds.
let ok: Result<BoundedUint, _> = serde_json::from_str("42");
assert!(ok.is_ok());
assert_eq!(*ok.unwrap(), 42u8);
let too_high: Result<BoundedUint, _> = serde_json::from_str("64");
assert!(too_high.is_err());
let too_low: Result<BoundedUint, _> = serde_json::from_str("0");
assert!(too_low.is_err());
// Serialization should produce the bare integer.
let v = BoundedUint::try_from(42u8).unwrap();
assert_eq!(serde_json::to_string(&v).unwrap(), "42");
// Negative bounds.
let ok: Result<BoundedNegative, _> = serde_json::from_str("-25");
assert!(ok.is_ok());
let too_low: Result<BoundedNegative, _> = serde_json::from_str("-51");
assert!(too_low.is_err());
let too_high: Result<BoundedNegative, _> = serde_json::from_str("-9");
assert!(too_high.is_err());
}
#[test]
fn test_unknown_format() {
// An unknown format string should just render as a string.
let _ = UnknownFormat {
pancakes: String::new(),
};
}
mod hashmap {
#![allow(dead_code)]
include!(concat!(env!("OUT_DIR"), "/codegen_hashmap.rs"));
#[test]
fn test_with_map() {
// Validate that a map is currently represented as a HashMap by default.
let _ = WithMap {
map: std::collections::HashMap::new(),
};
}
}
mod custom_map {
#![allow(dead_code)]
#[allow(private_interfaces)]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CustomMap<K, V> {
key: K,
value: V,
}
include!(concat!(env!("OUT_DIR"), "/codegen_custommap.rs"));
#[test]
fn test_with_map() {
// Validate that a map is represented as an CustomMap when requested.
let _ = WithMap {
map: CustomMap {
key: String::new(),
value: String::new(),
},
};
}
}