use std::{fs::File, io::Write};
use anyhow::Result;
use schemars::{schema::RootSchema, schema_for, JsonSchema};
macro_rules! write_schema_for {
($type:ty) => {
use itertools::Itertools;
let schema = schema_for!($type);
let type_name = stringify!($type);
let path: Vec<&str> = type_name.split("::").collect();
let dir_path =
String::from("support/schemas/") + &path.iter().take(path.len() - 2).join("/");
write_schema(schema, dir_path, type_name.to_string())?;
};
($path:literal, $type:ty) => {
let schema = schema_for!($type);
write_schema(
schema,
String::from("support/schemas/") + $path,
stringify!($type).to_string(),
)?;
};
}
fn write_schema(schema: RootSchema, dir_path: String, type_name: String) -> Result<()> {
let file_name = type_name
.split("::")
.last()
.unwrap()
.to_string()
.trim_end_matches('>')
.to_string()
+ ".json";
let content = serde_json::to_string_pretty(&schema)?;
let _ = std::fs::create_dir_all(&dir_path);
let mut file = File::create(format!("{}/{}", dir_path, file_name))?;
writeln!(&mut file, "{}", &content)?;
Ok(())
}
use bitwarden_json::response::Response;
#[allow(dead_code)]
#[derive(JsonSchema)]
struct SchemaTypes {
client_settings: bitwarden::ClientSettings,
input_command: bitwarden_json::command::Command,
api_key_login: Response<bitwarden::auth::login::ApiKeyLoginResponse>,
password_login: Response<bitwarden::auth::login::PasswordLoginResponse>,
login_access_token: Response<bitwarden::auth::login::AccessTokenLoginResponse>,
secret_identifiers: Response<bitwarden::secrets_manager::secrets::SecretIdentifiersResponse>,
secret: Response<bitwarden::secrets_manager::secrets::SecretResponse>,
secrets: Response<bitwarden::secrets_manager::secrets::SecretsResponse>,
secrets_delete: Response<bitwarden::secrets_manager::secrets::SecretsDeleteResponse>,
secrets_sync: Response<bitwarden::secrets_manager::secrets::SecretsSyncResponse>,
project: Response<bitwarden::secrets_manager::projects::ProjectResponse>,
projects: Response<bitwarden::secrets_manager::projects::ProjectsResponse>,
projects_delete: Response<bitwarden::secrets_manager::projects::ProjectsDeleteResponse>,
password: Response<String>,
#[cfg(feature = "internal")]
fingerprint: Response<bitwarden::platform::FingerprintResponse>,
#[cfg(feature = "internal")]
sync: Response<bitwarden::vault::SyncResponse>,
#[cfg(feature = "internal")]
user_api_key: Response<bitwarden::platform::UserApiKeyResponse>,
}
fn main() -> Result<()> {
write_schema_for!("schema_types", SchemaTypes);
#[cfg(feature = "internal")]
write_schema_for!(bitwarden_uniffi::docs::DocRef);
Ok(())
}