llmclient 0.3.2

Rust LLM client - Gemini, OpenAI, Claude, Mistral, DeepSeek, Groq
Documentation
use serde_json::{Value, Map, json};
use serde_json::Value::*;
use std::string::String;

fn main() {
    // The type of `john` is `serde_json::Value`
    /*
    let john = json!({
        "name": "John Doe",
	"not": "fred",
        "age": 43,
        "phones": [
            "+44 1234567",
            "+44 2345678"
        ]
    });
    */
    let data = r#"{
  "id": "chatcmpl-9KmDSgvDNpdWzZMjNikOFzWyZRi8D",
  "object": "chat.completion",
  "created": 1714738930,
  "model": "gpt-4-turbo-2024-04-09",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "tool_calls": [
          {
            "id": "call_lPmkbGFiJkvuXvzs09uUYdzD",
            "type": "function",
            "function": {
              "name": "calc",
              "arguments": "{\"expr\":\"(60 * 24) * 265.25\"}"
            }
          }
        ]
      },
      "logprobs": null,
      "finish_reason": "tool_calls"
    }
  ],
  "usage": {
    "prompt_tokens": 80,
    "completion_tokens": 23,
    "total_tokens": 103
  },
  "system_fingerprint": "fp_3450ce39d5"
}"#;

    // Convert to a string of JSON and print it out
    //println!("{}", john.to_string());
    let v: serde_json::Value = serde_json::from_str(data).unwrap();
    //println!("{}", v.to_string());
    //println!("{:?}", v["choices"]["finish_reason"].to_string());
    /*
    let val = match v {
        Null => "".to_string(),
        Bool(ref b) => b.to_string(),
        Number(ref n) => n.to_string(),
        String(ref s) => s,
        Array(ref a) => format!("{a:?}"),
        Object(ref o) => format!("{o:?}"),
    };
    */
    //println!("{:?}", v.choices.to_string());
    //println!("{:?}", val);
    //v.for_each(|f| println!("{f}"));
    println!("{:?}", finder(&v, "id"));
}

//fn finder(v: serde_json::Value, tag: &str) -> String {
fn finder(v: &Value, tag: &str) -> String {
    /*
    fn inner_finder(v: Map<String, Value>, tag: &str) -> String {
        let val = match v {
            Null => "".to_string(),
            Bool(b) => b.to_string(),
            Number(n) => n.to_string(),
            String(s) => s,
            Array(a) => {
                match a.iter().position(|&r| r == tag) {
                    None => "".to_string(),
                    Some(v) => v.to_string(),
                }
            },
            Object(o) => {
                inner_finder(o, tag)
            },
        };

        val
    }
    */

    let val = match v {
        Null => {
            "".to_string()
        },
        Bool(b) => {
            b.to_string() 
        },
        Number(n) => {
            println!("{}", n);
            n.to_string() 
        },
        String(s) => {
            println!("{}", s);
            s.to_string() 
        },
        Array(a) => {
            /*
            match a.iter().position(|r| r == tag) {
                None => "".to_string(),
                Some(v) => {
                    //v.to_string()
                    println!("--- {v}");
                    finder(&v.into(), tag)
                },
            }
            */
            for v in a {
                //println!("-- {v:?}");
                finder(v, tag);
            }
            "".to_string()
        },
        Object(o) => {
            //inner_finder(o, tag)
            for (k, v) in o.iter() {
                //println!("{k}");
                finder(v, tag);
            }
            //println!("{o:?} {}", o.len());
            "".to_string()
        },
    };

    val
}