From f61e6aa610dcdbf51e34c335b511abb74a657df3 Mon Sep 17 00:00:00 2001 From: cm-ayf Date: Sat, 31 Aug 2024 18:59:41 +0900 Subject: [PATCH] refactor: build.rs (#944) --- build.rs | 47 ++++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/build.rs b/build.rs index 0171f3b11..c661fd95f 100644 --- a/build.rs +++ b/build.rs @@ -1,30 +1,23 @@ -use git2::{DescribeFormatOptions, DescribeOptions, Repository}; +use git2::{DescribeFormatOptions, DescribeOptions, Error, Repository}; fn main() { - let Ok(repo) = Repository::open(".").map_err(|e| { - println!("cargo::warning={}", e); - e - }) else { - return; - }; - let Ok(git_describe_result) = repo - .describe( - DescribeOptions::new() - .describe_tags() - .show_commit_oid_as_fallback(true), - ) - .and_then(|describe| { - describe.format(Some( - DescribeFormatOptions::new() - .always_use_long_format(true) - .dirty_suffix("-dirty"), - )) - }).map_err(|e| { - println!("cargo::warning={}", e); - e - }) - else { - return; - }; - println!("cargo::rustc-env=GIT_DESCRIBE={git_describe_result}") + match get_git_describe_result() { + Ok(result) => println!("cargo::rustc-env=GIT_DESCRIBE={result}"), + Err(e) => println!("cargo::warning={}", e), + } +} + +fn get_git_describe_result() -> Result { + let repo = Repository::open(".")?; + let describe = repo.describe( + DescribeOptions::new() + .describe_tags() + .show_commit_oid_as_fallback(true), + )?; + let formatted = describe.format(Some( + DescribeFormatOptions::new() + .always_use_long_format(true) + .dirty_suffix("-dirty"), + ))?; + Ok(formatted) }