# HG changeset patch
# User Georges Racinet <georges.racinet@octobus.net>
# Date 1679596058 -3600
#      Thu Mar 23 19:27:38 2023 +0100
# Node ID bd9e9d777e70fc6442ab07b717848d2f17f0ef26
# Parent  9be2e5b333c4431a522c6c1fab5659da47a8636a
RHGitaly: proper version information

It was nice to use the purely stateless `ServerInfo` gRPC method
(at least with the info that HGitaly provides) as a first example
of an implementation using Tonic. Now it's time to provide the
actual information, consistent with the Python implementation.

We're using the `build_const` crate to inject the value at build time.
This is actually importing from the `constants.rs` file in the
relevant  build `out/` directory.

Also the `..Default::default()` syntax (taught us by clippy) allows
to avoid starting from the defaults to mutate a few fields.

diff --git a/rust/Cargo.lock b/rust/Cargo.lock
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -161,6 +161,12 @@
 ]
 
 [[package]]
+name = "build_const"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b4ae4235e6dac0694637c763029ecea1a2ec9e4e06ec2729bd21ba4d9c863eb7"
+
+[[package]]
 name = "byteorder"
 version = "1.4.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1248,6 +1254,7 @@
 name = "rhgitaly"
 version = "0.1.0"
 dependencies = [
+ "build_const",
  "hg-core",
  "prost",
  "prost-types",
diff --git a/rust/rhgitaly/Cargo.toml b/rust/rhgitaly/Cargo.toml
--- a/rust/rhgitaly/Cargo.toml
+++ b/rust/rhgitaly/Cargo.toml
@@ -13,6 +13,8 @@
 tokio-stream = { version = "0.1", features = ["net"] }
 tracing = "0.1.37"
 tracing-subscriber = "0.3.16"
+build_const = "0.2"
 
 [build-dependencies]
-tonic-build = "0.8"
\ No newline at end of file
+tonic-build = "0.8"
+build_const = "0.2"
\ No newline at end of file
diff --git a/rust/rhgitaly/build.rs b/rust/rhgitaly/build.rs
--- a/rust/rhgitaly/build.rs
+++ b/rust/rhgitaly/build.rs
@@ -1,4 +1,21 @@
+use std::fs::File;
+use std::io::Read;
+
+use build_const::ConstWriter;
+
+fn build_constants() -> Result<(), Box<dyn std::error::Error>> {
+    // use `for_build` in `build.rs`
+    let mut consts = ConstWriter::for_build("constants")?.finish_dependencies();
+    let mut version = String::new();
+    File::open("../../hgitaly/VERSION")?.read_to_string(&mut version)?;
+
+    // Add a value that is a result of "complex" calculations
+    consts.add_value("HGITALY_VERSION", "&str", version.trim());
+    Ok(())
+}
+
 fn main() -> Result<(), Box<dyn std::error::Error>> {
+    build_constants()?;
     tonic_build::configure()
         .build_server(true)
         .out_dir("src/generated")
diff --git a/rust/rhgitaly/src/lib.rs b/rust/rhgitaly/src/lib.rs
--- a/rust/rhgitaly/src/lib.rs
+++ b/rust/rhgitaly/src/lib.rs
@@ -3,6 +3,8 @@
 // This software may be used and distributed according to the terms of the
 // GNU General Public License version 2 or any later version.
 // SPDX-License-Identifier: GPL-2.0-or-later
+#[macro_use]
+extern crate build_const;
 
 // The generated module is derived from the Protobuf "package" name
 // Hence as soon as we start compiling the HGitaly-specific proto files,
diff --git a/rust/rhgitaly/src/service/server.rs b/rust/rhgitaly/src/service/server.rs
--- a/rust/rhgitaly/src/service/server.rs
+++ b/rust/rhgitaly/src/service/server.rs
@@ -8,6 +8,8 @@
 use tonic::{Request, Response, Status};
 use tracing::{info, instrument};
 
+build_const!("constants");
+
 #[derive(Debug, Default)]
 pub struct ServerServiceImpl {}
 
@@ -19,11 +21,10 @@
         _request: Request<ServerInfoRequest>,
     ) -> Result<Response<ServerInfoResponse>, Status> {
         info!("Processing");
-
-        let mut resp = ServerInfoResponse::default();
-        resp.server_version = "RHGitaly/Tonic 6.6.6".into();
-
-        Ok(Response::new(resp))
+        Ok(Response::new(ServerInfoResponse {
+            server_version: HGITALY_VERSION.into(),
+            ..Default::default()
+        }))
     }
 }