Subject: Re: [PATCH v2] gpg-interface: add function for converting trust level to string

From: Junio C Hamano <[email-masked]>
Date: Sat, 09 Jul 2022 13:52:18 -0700

Eric Sunshine <[email-masked]> writes:

> On Fri, Jul 8, 2022 at 7:28 AM Jaydeep Das via GitGitGadget
> <[email-masked]> wrote:
>> Add new helper function `gpg_trust_level_to_str()` which will
>> convert a given member of `enum signature_trust_level` to its
>> corresponding string(in lowercase). For example, `TRUST_ULTIMATE`
>
> s/g(/g (/
>
>> will yield the string "ultimate".
>>
>> This will abstract out some code in `pretty.c` relating to gpg
>> signature trust levels.
>>
>> Signed-off-by: Jaydeep Das <[email-masked]>
>> ---
>> diff --git a/gpg-interface.h b/gpg-interface.h
>> @@ -71,6 +71,14 @@ size_t parse_signed_buffer(const char *buf, size_t size);
>> +/*
>> + * Returns corresponding string in lowercase for a given member of
>> + * enum signature_trust_level. For example, `TRUST_ULTIMATE` will
>> + * return "ultimate".
>> + */
>> +char *gpg_trust_level_to_str(enum signature_trust_level level);
>
> It would be a good idea to update the function documentation to
> mention that the caller is responsible for freeing the returned
> string.

Given that there are small and fixed number of trust level strings,
I actually think that it would be more reasonable to return a static
string to the caller, something along the lines of the attached, so
that callers do not have to worry about freeing it.

Perhaps along the lines of ...

 gpg-interface.c | 19 ++++++++++++++++++-
 gpg-interface.h |  2 ++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git c/gpg-interface.c w/gpg-interface.c
index 947b58ad4d..4a5b9d0f3a 100644
--- c/gpg-interface.c
+++ w/gpg-interface.c
@@ -165,9 +165,10 @@ static struct {
 	{ 0, "TRUST_", GPG_STATUS_TRUST_LEVEL },
 };
 
-static struct {
+static struct sigcheck_gpg_trust_level {
 	const char *key;
 	enum signature_trust_level value;
+	const char *downcased;
 } sigcheck_gpg_trust_level[] = {
 	{ "UNDEFINED", TRUST_UNDEFINED },
 	{ "NEVER", TRUST_NEVER },
@@ -176,6 +177,22 @@ static struct {
 	{ "ULTIMATE", TRUST_ULTIMATE },
 };
 
+const char *gpg_trust_level_to_string(enum signature_trust_level level)
+{
+	struct sigcheck_gpg_trust_level *trust;
+
+	if (level < 0 || ARRAY_SIZE(sigcheck_gpg_trust_level) <= level)
+		BUG("invalid trust_level requested: %d", level);
+
+	trust = &sigcheck_gpg_trust_level[level];
+	if (trust->value != level)
+		BUG("sigcheck_gpg_trust_level[] unsorted");
+
+	if (!trust->downcased)
+		trust->downcased = xstrdup_tolower(trust->key);
+	return trust->downcased;
+}
+
 static void replace_cstring(char **field, const char *line, const char *next)
 {
 	free(*field);
diff --git c/gpg-interface.h w/gpg-interface.h
index b30cbdcd3d..2dffcb836d 100644
--- c/gpg-interface.h
+++ w/gpg-interface.h
@@ -85,4 +85,6 @@ int check_signature(struct signature_check *sigc,
 void print_signature_buffer(const struct signature_check *sigc,
 			    unsigned flags);
 
+const char *gpg_trust_level_to_string(enum signature_trust_level);
+
 #endif