2023-10-25 12:24:36 +11:00
|
|
|
use crate::fleet::FleetEndpoint;
|
|
|
|
use crate::teslatte::TeslatteEndpoint;
|
|
|
|
use crate::timdorr::TimdorrEndpoint;
|
|
|
|
use crate::vehicle_command::VehicleCommandEndpoint;
|
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![];
|
|
|
|
table.push("| API | Teslatte | Timdorr API | Fleet API | Command Mode |".to_string());
|
|
|
|
table.push("| --- | --- | --- | --- | --- |".to_string());
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
|
|
|
if endpoint.teslatte.is_some() {
|
|
|
|
row.push("✅".to_string());
|
|
|
|
} else {
|
|
|
|
row.push("".to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint.timdorr.is_some() {
|
|
|
|
row.push("🟢".to_string());
|
|
|
|
} else {
|
|
|
|
row.push("".to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint.fleet.is_some() {
|
|
|
|
row.push("🟢".to_string());
|
|
|
|
} else {
|
|
|
|
row.push("".to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint.vehicle_command.is_some() {
|
|
|
|
row.push("🟢".to_string());
|
|
|
|
} else {
|
|
|
|
row.push("".to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
table.push(format!("| {} |", row.join(" | ")));
|
|
|
|
}
|
|
|
|
|
|
|
|
let table = table.join("\n");
|
|
|
|
|
|
|
|
// Now join them together and write the whole file back.
|
|
|
|
let contents = format!("{before}\n{START}\n{table}\n{END}\n{after}");
|
|
|
|
let mut file = File::create(&path)?;
|
|
|
|
file.write_all(contents.as_bytes())?;
|
|
|
|
|
|
|
|
Ok(())
|
2023-10-25 12:24:36 +11:00
|
|
|
}
|