use crate::{Component, Error, query};
use std::{ffi::OsString, path::Path};
pub fn source_changes(
binary: &Path,
root: &Path,
empty: bool,
files: usize,
) -> Result<usize, Error> {
let mut options = vec![OsString::from("--files"), OsString::from(files.to_string())];
if empty {
options.push(OsString::from("--empty-scaffold"));
}
query(Component::Vcs, binary, "source_changes", root, &options)?
.get("local_changes")
.and_then(serde_json::Value::as_u64)
.and_then(|count| usize::try_from(count).ok())
.ok_or(Error::InvalidResponse)
}
pub fn exists(binary: &Path, root: &Path) -> Result<bool, Error> {
query(Component::Vcs, binary, "repository_exists", root, &[])?
.get("exists")
.and_then(serde_json::Value::as_bool)
.ok_or(Error::InvalidResponse)
}
pub fn initialize_new(binary: &Path, root: &Path) -> Result<(), Error> {
initialized(binary, root, "initialize")
}
pub fn initialize_reserved(binary: &Path, git: &Path) -> Result<(), Error> {
initialized(binary, git, "initialize_reserved")
}
fn initialized(binary: &Path, root: &Path, operation: &str) -> Result<(), Error> {
let data = query(Component::Vcs, binary, operation, root, &[])?;
if data.get("initialized").and_then(serde_json::Value::as_bool) != Some(true) {
return Err(Error::InvalidResponse);
}
Ok(())
}