Skip to content

Commit

Permalink
Merge #1183
Browse files Browse the repository at this point in the history
1183: Fix error when using certain config values in Cargo.toml r=Emilgardis a=Emilgardis

`pre-build = "file"` and `image.toolchain` had a bad serialization, this fixes that

Closes #1182


Co-authored-by: Emil Gardström <[email protected]>
  • Loading branch information
bors[bot] and Emilgardis committed Jan 6, 2023
2 parents 1d9d310 + 6a57d01 commit 14c24a8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changes/1183.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"description": "resolve issue when using `pre-build` and `image.toolchain` in `Cargo.toml`",
"type": "fixed",
"issues": [1182]
}
23 changes: 23 additions & 0 deletions src/cross_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,29 @@ mod tests {
Ok(())
}

#[test]
pub fn fully_populated_roundtrip() -> Result<()> {
let cfg = r#"
[target.a]
xargo = false
build-std = true
image.name = "local"
image.toolchain = ["x86_64-unknown-linux-gnu"]
dockerfile.file = "Dockerfile"
dockerfile.context = ".."
pre-build = ["sh"]
zig = true
[target.b]
pre-build = "sh"
zig = "2.17"
"#;

let (cfg, _) = CrossToml::parse_from_cross(cfg, &mut m!())?;
serde_json::from_value::<CrossToml>(serde_json::to_value(cfg)?)?;
Ok(())
}

#[test]
pub fn merge() -> Result<()> {
let cfg1_str = r#"
Expand Down
17 changes: 16 additions & 1 deletion src/docker/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,29 @@ pub enum Dockerfile<'a> {
},
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize)]
pub enum PreBuild {
/// A path to a file to copy or a single line to `RUN` if line comes from env
Single { line: String, env: bool },
/// Lines to execute in a single `RUN`
Lines(Vec<String>),
}

impl serde::Serialize for PreBuild {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
PreBuild::Single { line, .. } => serializer.serialize_str(line),
PreBuild::Lines(lines) => {
use serde::ser::SerializeSeq;
let mut seq = serializer.serialize_seq(Some(lines.len()))?;
for line in lines {
seq.serialize_element(line)?;
}
seq.end()
}
}
}
}
impl FromStr for PreBuild {
type Err = std::convert::Infallible;

Expand Down
14 changes: 10 additions & 4 deletions src/docker/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ impl std::fmt::Display for PossibleImage {
/// The architecture/platform to use in the image
///
/// https://github.com/containerd/containerd/blob/release/1.6/platforms/platforms.go#L63
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(try_from = "&str")]
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize)]
#[serde(try_from = "String")]
pub struct ImagePlatform {
/// CPU architecture, x86_64, aarch64 etc
pub architecture: Architecture,
Expand Down Expand Up @@ -141,14 +141,20 @@ impl Default for ImagePlatform {
}
}

impl TryFrom<&str> for ImagePlatform {
impl TryFrom<String> for ImagePlatform {
type Error = <Self as std::str::FromStr>::Err;

fn try_from(value: &str) -> Result<Self, Self::Error> {
fn try_from(value: String) -> Result<Self, Self::Error> {
value.parse()
}
}

impl Serialize for ImagePlatform {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&format!("{}={}", self.docker_platform(), self.target))
}
}

impl std::str::FromStr for ImagePlatform {
type Err = eyre::Report;
// [os/arch[/variant]=]toolchain
Expand Down
10 changes: 8 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ pub use self::rustc::{TargetList, VersionMetaExt};
pub const CROSS_LABEL_DOMAIN: &str = "org.cross-rs";

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Hash, Serialize)]
#[serde(from = "&str")]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Hash)]
#[serde(from = "&str", into = "String")]
#[serde(rename_all = "snake_case")]
pub enum TargetTriple {
Other(String),
Expand Down Expand Up @@ -261,6 +261,12 @@ impl From<String> for TargetTriple {
}
}

impl Serialize for TargetTriple {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.triple())
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize)]
#[serde(from = "String")]
pub enum Target {
Expand Down
11 changes: 7 additions & 4 deletions src/tests/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ fn toml_check() -> Result<(), Box<dyn std::error::Error>> {
text_line_no(&contents, fence.range().start),
);
let mut msg_info = crate::shell::MessageInfo::default();
assert!(if !cargo {
let toml = if !cargo {
crate::cross_toml::CrossToml::parse_from_cross(&fence_content, &mut msg_info)?
} else {
crate::cross_toml::CrossToml::parse_from_cargo(&fence_content, &mut msg_info)?
.unwrap_or_default()
}
.1
.is_empty());
};
assert!(toml.1.is_empty());

// TODO: Add serde_path_to_error
// Check if roundtrip works, needed for merging Cross.toml and Cargo.toml
serde_json::from_value::<crate::cross_toml::CrossToml>(serde_json::to_value(toml.0)?)?;
}
}
Ok(())
Expand Down

0 comments on commit 14c24a8

Please sign in to comment.