Skip to content

Commit ac7656a

Browse files
authored
refactor(updater): strong type for the pub_date field, ref #4162 (#4218)
1 parent 2badbd2 commit ac7656a

File tree

8 files changed

+45
-23
lines changed

8 files changed

+45
-23
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
**Breaking change:** The `tauri::UpdaterEvent::UpdateEvent` date field is now an `Option<time::OffsetDateTime>`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
**Breaking change:** The updater response `pub_date` now must be a valid RFC 3339 string.

core/tauri/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ os_pipe = { version = "1.0", optional = true }
8484
rfd = { version = "0.8", optional = true }
8585
raw-window-handle = "0.4.2"
8686
minisign-verify = { version = "0.2", optional = true }
87+
time = { version = "0.3", features = ["parsing", "formatting"], optional = true }
8788
os_info = { version = "3.2.0", optional = true }
8889
futures-lite = "1.12"
8990
regex = { version = "1.5.5", optional = true }
@@ -138,6 +139,7 @@ isolation = [ "tauri-utils/isolation", "tauri-macros/isolation" ]
138139
custom-protocol = [ "tauri-macros/custom-protocol" ]
139140
updater = [
140141
"minisign-verify",
142+
"time",
141143
"base64",
142144
"http-api",
143145
"dialog-ask",

core/tauri/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ pub enum UpdaterEvent {
265265
/// The update body.
266266
body: String,
267267
/// The update release date.
268-
date: String,
268+
date: Option<time::OffsetDateTime>,
269269
/// The update version.
270270
version: String,
271271
},

core/tauri/src/updater/core.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use minisign_verify::{PublicKey, Signature};
1818
use semver::Version;
1919
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize};
2020
use tauri_utils::{platform::current_exe, Env};
21+
use time::OffsetDateTime;
2122
use url::Url;
2223

2324
#[cfg(feature = "updater")]
@@ -57,16 +58,15 @@ pub enum RemoteReleaseInner {
5758
/// Information about a release returned by the remote update server.
5859
///
5960
/// This type can have one of two shapes: Server Format (Dynamic Format) and Static Format.
60-
#[derive(Debug, Serialize)]
61+
#[derive(Debug)]
6162
pub struct RemoteRelease {
6263
/// Version to install.
6364
version: Version,
6465
/// Release notes.
6566
notes: Option<String>,
6667
/// Release date.
67-
pub_date: String,
68+
pub_date: Option<OffsetDateTime>,
6869
/// Release data.
69-
#[serde(flatten)]
7070
data: RemoteReleaseInner,
7171
}
7272

@@ -75,17 +75,12 @@ impl<'de> Deserialize<'de> for RemoteRelease {
7575
where
7676
D: Deserializer<'de>,
7777
{
78-
fn default_pub_date() -> String {
79-
"N/A".to_string()
80-
}
81-
8278
#[derive(Deserialize)]
8379
struct InnerRemoteRelease {
8480
#[serde(alias = "name", deserialize_with = "parse_version")]
8581
version: Version,
8682
notes: Option<String>,
87-
#[serde(default = "default_pub_date")]
88-
pub_date: String,
83+
pub_date: Option<String>,
8984
platforms: Option<HashMap<String, ReleaseManifestPlatform>>,
9085
// dynamic platform response
9186
url: Option<Url>,
@@ -97,10 +92,19 @@ impl<'de> Deserialize<'de> for RemoteRelease {
9792

9893
let release = InnerRemoteRelease::deserialize(deserializer)?;
9994

95+
let pub_date = if let Some(date) = release.pub_date {
96+
Some(
97+
OffsetDateTime::parse(&date, &time::format_description::well_known::Rfc3339)
98+
.map_err(|e| DeError::custom(format!("invalid value for `pub_date`: {}", e)))?,
99+
)
100+
} else {
101+
None
102+
};
103+
100104
Ok(RemoteRelease {
101105
version: release.version,
102106
notes: release.notes,
103-
pub_date: release.pub_date,
107+
pub_date,
104108
data: if let Some(platforms) = release.platforms {
105109
RemoteReleaseInner::Static { platforms }
106110
} else {
@@ -152,8 +156,8 @@ impl RemoteRelease {
152156
}
153157

154158
/// The release date.
155-
pub fn pub_date(&self) -> &String {
156-
&self.pub_date
159+
pub fn pub_date(&self) -> Option<&OffsetDateTime> {
160+
self.pub_date.as_ref()
157161
}
158162

159163
/// The release's download URL for the given target.
@@ -431,7 +435,7 @@ impl<R: Runtime> UpdateBuilder<R> {
431435
extract_path,
432436
should_update,
433437
version: final_release.version().to_string(),
434-
date: final_release.pub_date().to_string(),
438+
date: final_release.pub_date().cloned(),
435439
current_version: self.current_version,
436440
download_url: final_release.download_url(&json_target)?.to_owned(),
437441
body: final_release.notes().cloned(),
@@ -461,7 +465,7 @@ pub struct Update<R: Runtime> {
461465
/// Running version
462466
pub current_version: Version,
463467
/// Update publish date
464-
pub date: String,
468+
pub date: Option<OffsetDateTime>,
465469
/// Target
466470
#[allow(dead_code)]
467471
target: String,
@@ -489,7 +493,7 @@ impl<R: Runtime> Clone for Update<R> {
489493
should_update: self.should_update,
490494
version: self.version.clone(),
491495
current_version: self.current_version.clone(),
492-
date: self.date.clone(),
496+
date: self.date,
493497
target: self.target.clone(),
494498
extract_path: self.extract_path.clone(),
495499
download_url: self.download_url.clone(),

core/tauri/src/updater/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
//! tauri::RunEvent::Updater(updater_event) => {
137137
//! match updater_event {
138138
//! tauri::UpdaterEvent::UpdateAvailable { body, date, version } => {
139-
//! println!("update available {} {} {}", body, date, version);
139+
//! println!("update available {} {:?} {}", body, date, version);
140140
//! }
141141
//! _ => (),
142142
//! }
@@ -246,7 +246,7 @@
246246
//! tauri::RunEvent::Updater(updater_event) => {
247247
//! match updater_event {
248248
//! tauri::UpdaterEvent::UpdateAvailable { body, date, version } => {
249-
//! println!("update available {} {} {}", body, date, version);
249+
//! println!("update available {} {:?} {}", body, date, version);
250250
//! }
251251
//! tauri::UpdaterEvent::Pending => {
252252
//! println!("update is pending!");
@@ -450,6 +450,7 @@ use std::time::Duration;
450450

451451
use http::header::{HeaderName, HeaderValue};
452452
use semver::Version;
453+
use time::OffsetDateTime;
453454

454455
pub use self::{core::RemoteRelease, error::Error};
455456
/// Alias for [`std::result::Result`] using our own [`Error`].
@@ -509,7 +510,7 @@ struct DownloadProgressEvent {
509510
#[derive(Clone, serde::Serialize)]
510511
struct UpdateManifest {
511512
version: String,
512-
date: String,
513+
date: Option<String>,
513514
body: String,
514515
}
515516

@@ -686,14 +687,14 @@ impl<R: Runtime> UpdateBuilder<R> {
686687
EVENT_UPDATE_AVAILABLE,
687688
UpdateManifest {
688689
body: body.clone(),
689-
date: update.date.clone(),
690+
date: update.date.map(|d| d.to_string()),
690691
version: update.version.clone(),
691692
},
692693
);
693694
let _ = handle.create_proxy().send_event(EventLoopMessage::Updater(
694695
UpdaterEvent::UpdateAvailable {
695696
body,
696-
date: update.date.clone(),
697+
date: update.date,
697698
version: update.version.clone(),
698699
},
699700
));
@@ -751,8 +752,8 @@ impl<R: Runtime> UpdateResponse<R> {
751752
}
752753

753754
/// The update date.
754-
pub fn date(&self) -> &str {
755-
&self.update.date
755+
pub fn date(&self) -> Option<&OffsetDateTime> {
756+
self.update.date.as_ref()
756757
}
757758

758759
/// The update description.

core/tests/app-updater/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ serde = { version = "1", features = ["derive"] }
1111
serde_json = "1"
1212
tiny_http = "0.11"
1313
tauri = { path = "../../tauri", features = ["updater"] }
14+
time = { version = "0.3", features = ["formatting"] }
1415

1516
[features]
1617
default = ["custom-protocol"]

core/tests/app-updater/tests/update.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct PlatformUpdate {
3535
#[derive(Serialize)]
3636
struct Update {
3737
version: &'static str,
38+
date: String,
3839
platforms: HashMap<String, PlatformUpdate>,
3940
}
4041

@@ -178,6 +179,9 @@ fn update_app() {
178179
);
179180
let body = serde_json::to_vec(&Update {
180181
version: "1.0.0",
182+
date: time::OffsetDateTime::now_utc()
183+
.format(&time::format_description::well_known::Rfc3339)
184+
.unwrap(),
181185
platforms,
182186
})
183187
.unwrap();

0 commit comments

Comments
 (0)