Skip to content

Commit

Permalink
ref(api): Separate Deploy struct into separate submodule (#2161)
Browse files Browse the repository at this point in the history
Also, use `Cow` in the struct to avoid allocations
  • Loading branch information
szokeasaurusrex authored Sep 20, 2024
1 parent 099919a commit 19d08fb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
29 changes: 29 additions & 0 deletions src/api/data_types/deploy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! The `Deploy` data type.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Deploy<'d> {
#[serde(rename = "environment")]
pub env: Cow<'d, str>,
pub name: Option<Cow<'d, str>>,
pub url: Option<Cow<'d, str>>,
#[serde(rename = "dateStarted")]
pub started: Option<DateTime<Utc>>,
#[serde(rename = "dateFinished")]
pub finished: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub projects: Option<Vec<Cow<'d, str>>>,
}

impl<'d> Deploy<'d> {
/// Returns the name of this deploy, defaulting to `"unnamed"`.
pub fn name(&self) -> &str {
match self.name.as_deref() {
Some("") | None => "unnamed",
Some(name) => name,
}
}
}
4 changes: 4 additions & 0 deletions src/api/data_types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//! Data types used in the api module

mod chunking;
mod deploy;

pub use self::chunking::*;
pub use self::deploy::*;
24 changes: 0 additions & 24 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2359,30 +2359,6 @@ impl fmt::Display for Repo {
}
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Deploy<'d> {
#[serde(rename = "environment")]
pub env: String,
pub name: Option<String>,
pub url: Option<String>,
#[serde(rename = "dateStarted")]
pub started: Option<DateTime<Utc>>,
#[serde(rename = "dateFinished")]
pub finished: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub projects: Option<Vec<Cow<'d, str>>>,
}

impl<'d> Deploy<'d> {
/// Returns the name of this deploy, defaulting to `"unnamed"`.
pub fn name(&self) -> &str {
match self.name.as_deref() {
Some("") | None => "unnamed",
Some(name) => name,
}
}
}

#[derive(Debug, Serialize, Clone)]
pub struct PatchSet {
pub path: String,
Expand Down
6 changes: 3 additions & 3 deletions src/commands/deploys/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
let api = Api::current();
let version = config.get_release_with_legacy_fallback(matches)?;
let mut deploy = Deploy {
env: matches.get_one::<String>("env").unwrap().to_string(),
name: matches.get_one::<String>("name").cloned(),
url: matches.get_one::<String>("url").cloned(),
env: matches.get_one::<String>("env").unwrap().into(),
name: matches.get_one::<String>("name").map(|n| n.into()),
url: matches.get_one::<String>("url").map(|u| u.into()),
projects: matches.get_many::<String>("project").map(|x| x.into_iter().map(|x| x.into()).collect()),
..Default::default()
};
Expand Down

0 comments on commit 19d08fb

Please sign in to comment.