@@ -191,6 +191,17 @@ impl ImagePlatform {
191191 format ! ( "{}/{}" , self . os, self . architecture)
192192 }
193193 }
194+
195+ /// Returns a string that can be used in codegen to represent this platform
196+ pub fn to_codegen_string ( & self ) -> Option < & ' static str > {
197+ match self . target {
198+ TargetTriple :: X86_64UnknownLinuxGnu => Some ( "ImagePlatform::X86_64_UNKNOWN_LINUX_GNU" ) ,
199+ TargetTriple :: Aarch64UnknownLinuxGnu => {
200+ Some ( "ImagePlatform::AARCH64_UNKNOWN_LINUX_GNU" )
201+ }
202+ _ => None ,
203+ }
204+ }
194205}
195206
196207impl Default for ImagePlatform {
@@ -221,6 +232,14 @@ impl std::str::FromStr for ImagePlatform {
221232 value:: { Error as SerdeError , StrDeserializer } ,
222233 IntoDeserializer ,
223234 } ;
235+
236+ // Try to match the docker platform string first
237+ match s {
238+ "linux/amd64" => return Ok ( Self :: X86_64_UNKNOWN_LINUX_GNU ) ,
239+ "linux/arm64" | "linux/arm64/v8" => return Ok ( Self :: AARCH64_UNKNOWN_LINUX_GNU ) ,
240+ _ => { }
241+ } ;
242+
224243 if let Some ( ( platform, toolchain) ) = s. split_once ( '=' ) {
225244 let image_toolchain = toolchain. into ( ) ;
226245 let ( os, arch, variant) = if let Some ( ( os, rest) ) = platform. split_once ( '/' ) {
@@ -505,4 +524,24 @@ pub mod tests {
505524 assert_eq ! ( Os :: from_target( & t!( "x86_64-pc-windows-msvc" ) ) ?, Os :: Windows ) ;
506525 Ok ( ( ) )
507526 }
527+
528+ #[ test]
529+ fn image_platform_from_docker_platform_str ( ) -> Result < ( ) > {
530+ assert_eq ! (
531+ "linux/amd64" . parse:: <ImagePlatform >( ) ?,
532+ ImagePlatform :: X86_64_UNKNOWN_LINUX_GNU
533+ ) ;
534+
535+ assert_eq ! (
536+ "linux/arm64" . parse:: <ImagePlatform >( ) ?,
537+ ImagePlatform :: AARCH64_UNKNOWN_LINUX_GNU
538+ ) ;
539+
540+ assert_eq ! (
541+ "linux/arm64/v8" . parse:: <ImagePlatform >( ) ?,
542+ ImagePlatform :: AARCH64_UNKNOWN_LINUX_GNU
543+ ) ;
544+
545+ Ok ( ( ) )
546+ }
508547}
0 commit comments