2023-10-25 14:44:34 +11:00
|
|
|
|
use crate::Endpoint;
|
2023-10-25 12:24:36 +11:00
|
|
|
|
use std::collections::HashMap;
|
2023-10-25 14:44:34 +11:00
|
|
|
|
use std::fs::File;
|
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
|
use std::path::PathBuf;
|
2023-10-25 12:24:36 +11:00
|
|
|
|
|
2023-10-25 14:44:34 +11:00
|
|
|
|
const START: &str = "<!-- tesla_api_coverage start table -->";
|
|
|
|
|
const END: &str = "<!-- tesla_api_coverage end table -->";
|
|
|
|
|
|
|
|
|
|
pub fn generate(merged: &HashMap<String, Endpoint>) -> anyhow::Result<()> {
|
|
|
|
|
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
|
path.push("..");
|
|
|
|
|
path.push("API.md");
|
|
|
|
|
|
|
|
|
|
let mut file = File::open(&path)?;
|
|
|
|
|
let mut contents = String::new();
|
|
|
|
|
file.read_to_string(&mut contents)?;
|
|
|
|
|
drop(file);
|
|
|
|
|
|
|
|
|
|
// Split the file into before and after the table.
|
|
|
|
|
let mut parts = contents.split(START);
|
|
|
|
|
let before = parts.next().unwrap();
|
|
|
|
|
let mut parts = parts.next().unwrap().split(END);
|
|
|
|
|
let table = parts.next().unwrap();
|
|
|
|
|
let after = parts.next().unwrap();
|
|
|
|
|
|
|
|
|
|
let mut table = vec![];
|
2023-10-25 15:49:57 +11:00
|
|
|
|
table.push("| API | Owners API | Fleet API | Command Mode |".to_string());
|
2023-10-25 15:16:44 +11:00
|
|
|
|
table.push("| --- | --- | --- | --- |".to_string());
|
2023-10-25 14:44:34 +11:00
|
|
|
|
|
|
|
|
|
// Sort by URL for into a Vec for now. Keep the key.
|
|
|
|
|
let mut endpoints = merged.iter().collect::<Vec<(&String, &Endpoint)>>();
|
|
|
|
|
endpoints.sort_by(|a, b| a.1.name.cmp(&b.1.name));
|
|
|
|
|
|
|
|
|
|
for (name, endpoint) in endpoints {
|
|
|
|
|
let mut row = vec![];
|
|
|
|
|
row.push(format!("{}", name));
|
|
|
|
|
|
2023-10-25 15:16:44 +11:00
|
|
|
|
if endpoint.teslatte_owners_api.is_some() {
|
2023-10-25 14:44:34 +11:00
|
|
|
|
row.push("✅".to_string());
|
2023-10-25 15:16:44 +11:00
|
|
|
|
} else if endpoint.timdorr.is_some() {
|
|
|
|
|
row.push("🔴".to_string());
|
2023-10-25 14:44:34 +11:00
|
|
|
|
} else {
|
2023-10-25 15:49:57 +11:00
|
|
|
|
row.push("➖".to_string());
|
2023-10-25 14:44:34 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if endpoint.fleet.is_some() {
|
2023-10-25 15:16:44 +11:00
|
|
|
|
row.push("🔴".to_string());
|
2023-10-25 14:44:34 +11:00
|
|
|
|
} else {
|
2023-10-25 15:49:57 +11:00
|
|
|
|
row.push("➖".to_string());
|
2023-10-25 14:44:34 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if endpoint.vehicle_command.is_some() {
|
2023-10-25 15:16:44 +11:00
|
|
|
|
row.push("🔴".to_string());
|
2023-10-25 14:44:34 +11:00
|
|
|
|
} else {
|
2023-10-25 15:49:57 +11:00
|
|
|
|
row.push("➖".to_string());
|
2023-10-25 14:44:34 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
table.push(format!("| {} |", row.join(" | ")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let table = table.join("\n");
|
|
|
|
|
|
|
|
|
|
// Now join them together and write the whole file back.
|
2023-10-25 15:49:57 +11:00
|
|
|
|
let contents = format!("{before}{START}\n{table}\n{END}{after}");
|
2023-10-25 14:44:34 +11:00
|
|
|
|
let mut file = File::create(&path)?;
|
|
|
|
|
file.write_all(contents.as_bytes())?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2023-10-25 12:24:36 +11:00
|
|
|
|
}
|