Skip to content

Commit 95645ff

Browse files
committed
Implement IntoProto<T> for Into<T>
1 parent 8747100 commit 95645ff

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mullvad-proc-macro/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ proc-macro = true
1212
[dependencies]
1313
proc-macro2 = "1"
1414
syn = { version= "2", features = ["extra-traits"] }
15-
quote = "1"
15+
quote = "1"
16+
chrono.workspace = true

mullvad-proc-macro/tests/struct.rs

+41-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![allow(unused)]
2+
3+
use chrono::DateTime;
24
use mullvad_proc_macro::{IntoProto, UnwrapProto};
35

46
#[derive(Debug)]
@@ -69,40 +71,55 @@ pub struct AppVersionInfo {
6971
pub suggested_upgrade: Option<String>,
7072
}
7173

74+
#[derive(IntoProto, Debug)]
75+
pub struct Device {
76+
pub created: chrono::DateTime<chrono::Utc>,
77+
}
78+
79+
impl IntoProto<proto::Timestamp> for chrono::DateTime<chrono::Utc> {
80+
fn into_proto(self) -> proto::Timestamp {
81+
proto::Timestamp {
82+
seconds: self.timestamp(),
83+
nanos: 0,
84+
}
85+
}
86+
}
87+
7288
pub type AppVersion = String;
7389

7490
mod proto {
91+
use mullvad_proc_macro::IntoProto;
92+
7593
#[derive(Debug)]
7694
pub struct AppVersionInfo {
7795
pub supported: bool,
7896
pub latest_stable: String,
7997
pub latest_beta: String,
8098
pub suggested_upgrade: Option<String>,
8199
}
100+
101+
#[derive(Debug)]
102+
pub struct Device {
103+
pub created: Timestamp,
104+
}
105+
106+
#[derive(Debug)]
107+
pub struct Timestamp {
108+
pub seconds: i64,
109+
pub nanos: i32,
110+
}
82111
}
83112

84113
trait IntoProto<T> {
85114
fn into_proto(self) -> T;
86115
}
87-
impl<T: IntoProto<S>, S> IntoProto<Option<S>> for Option<T> {
88-
fn into_proto(self) -> Option<S> {
89-
self.map(|val| val.into_proto())
90-
}
91-
}
92116

93-
macro_rules! impl_into_proto_for_value_type {
94-
($ty:ty) => {
95-
impl IntoProto<$ty> for $ty {
96-
fn into_proto(self) -> $ty {
97-
self
98-
}
99-
}
100-
};
117+
impl<T: Into<S>, S> IntoProto<S> for T {
118+
fn into_proto(self) -> S {
119+
self.into()
120+
}
101121
}
102122

103-
impl_into_proto_for_value_type!(bool);
104-
impl_into_proto_for_value_type!(String);
105-
106123
#[test]
107124
fn test_generate_into_proto() {
108125
let settings_proto = AppVersionInfo {
@@ -112,6 +129,13 @@ fn test_generate_into_proto() {
112129
supported: false,
113130
};
114131

115-
let settings = settings_proto.into_proto();
132+
let settings: proto::AppVersionInfo = settings_proto.into_proto();
133+
dbg!(settings);
134+
135+
let device_proto = Device {
136+
created: DateTime::default(),
137+
};
138+
139+
let settings: proto::Device = device_proto.into_proto();
116140
dbg!(settings);
117141
}

0 commit comments

Comments
 (0)