Skip to main content

oxiproto_codegen/
wkt_map.rs

1#![forbid(unsafe_code)]
2
3//! Mapping from proto well-known type FQNs to Rust type paths.
4
5/// Returns the Rust type path for a well-known type proto FQN, if known.
6///
7/// Both leading-dot and non-leading-dot forms are accepted.
8///
9/// # Examples
10///
11/// ```
12/// use oxiproto_codegen::wkt_map::wkt_rust_type;
13///
14/// assert_eq!(
15///     wkt_rust_type("google.protobuf.Timestamp"),
16///     Some("::oxiproto_wkt::Timestamp")
17/// );
18/// assert_eq!(
19///     wkt_rust_type(".google.protobuf.Timestamp"),
20///     Some("::oxiproto_wkt::Timestamp")
21/// );
22/// assert_eq!(wkt_rust_type("google.protobuf.Unknown"), None);
23/// ```
24pub fn wkt_rust_type(proto_fqn: &str) -> Option<&'static str> {
25    match proto_fqn {
26        ".google.protobuf.Timestamp" | "google.protobuf.Timestamp" => {
27            Some("::oxiproto_wkt::Timestamp")
28        }
29        ".google.protobuf.Duration" | "google.protobuf.Duration" => {
30            Some("::oxiproto_wkt::Duration")
31        }
32        ".google.protobuf.Any" | "google.protobuf.Any" => Some("::oxiproto_wkt::Any"),
33        ".google.protobuf.Empty" | "google.protobuf.Empty" => Some("::oxiproto_wkt::Empty"),
34        ".google.protobuf.FieldMask" | "google.protobuf.FieldMask" => {
35            Some("::oxiproto_wkt::FieldMask")
36        }
37        ".google.protobuf.Struct" | "google.protobuf.Struct" => Some("::prost_types::Struct"),
38        ".google.protobuf.Value" | "google.protobuf.Value" => Some("::prost_types::Value"),
39        ".google.protobuf.ListValue" | "google.protobuf.ListValue" => {
40            Some("::prost_types::ListValue")
41        }
42        // Wrapper types — map to Option<inner>
43        ".google.protobuf.StringValue" | "google.protobuf.StringValue" => {
44            Some("::core::option::Option<::std::string::String>")
45        }
46        ".google.protobuf.BytesValue" | "google.protobuf.BytesValue" => {
47            Some("::core::option::Option<::std::vec::Vec<u8>>")
48        }
49        ".google.protobuf.BoolValue" | "google.protobuf.BoolValue" => {
50            Some("::core::option::Option<bool>")
51        }
52        ".google.protobuf.Int32Value" | "google.protobuf.Int32Value" => {
53            Some("::core::option::Option<i32>")
54        }
55        ".google.protobuf.Int64Value" | "google.protobuf.Int64Value" => {
56            Some("::core::option::Option<i64>")
57        }
58        ".google.protobuf.UInt32Value" | "google.protobuf.UInt32Value" => {
59            Some("::core::option::Option<u32>")
60        }
61        ".google.protobuf.UInt64Value" | "google.protobuf.UInt64Value" => {
62            Some("::core::option::Option<u64>")
63        }
64        ".google.protobuf.FloatValue" | "google.protobuf.FloatValue" => {
65            Some("::core::option::Option<f32>")
66        }
67        ".google.protobuf.DoubleValue" | "google.protobuf.DoubleValue" => {
68            Some("::core::option::Option<f64>")
69        }
70        _ => None,
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn timestamp_both_forms() {
80        assert_eq!(
81            wkt_rust_type("google.protobuf.Timestamp"),
82            Some("::oxiproto_wkt::Timestamp")
83        );
84        assert_eq!(
85            wkt_rust_type(".google.protobuf.Timestamp"),
86            Some("::oxiproto_wkt::Timestamp")
87        );
88    }
89
90    #[test]
91    fn unknown_returns_none() {
92        assert_eq!(wkt_rust_type("google.protobuf.Unknown"), None);
93        assert_eq!(wkt_rust_type("my.custom.Type"), None);
94    }
95
96    #[test]
97    fn wrapper_types() {
98        assert_eq!(
99            wkt_rust_type("google.protobuf.StringValue"),
100            Some("::core::option::Option<::std::string::String>")
101        );
102        assert_eq!(
103            wkt_rust_type("google.protobuf.BoolValue"),
104            Some("::core::option::Option<bool>")
105        );
106    }
107}