use super::*;
impl CodeGraph {
pub fn init(
project_root: impl AsRef<Path>,
options: InitOptions,
) -> Result<Self, CodeGraphError> {
let resolved_root = resolve_root(project_root);
if is_initialized(&resolved_root) {
return Err(CodeGraphError::new(
format!(
"RustCodeGraph already initialized in {}",
resolved_root.display()
),
"ALREADY_INITIALIZED",
None,
));
}
create_directory(&resolved_root)?;
let db = DatabaseConnection::initialize(facade_database_path(&resolved_root))
.map_err(|err| database_error("initialize", err))?;
let mut instance = Self::new(resolved_root, db);
if options.index {
let _ = instance.index_all(IndexOptions::default());
}
Ok(instance)
}
pub fn init_sync(project_root: impl AsRef<Path>) -> Result<Self, CodeGraphError> {
Self::init(project_root, InitOptions { index: false })
}
pub fn open(
project_root: impl AsRef<Path>,
options: OpenOptions,
) -> Result<Self, CodeGraphError> {
let resolved_root = resolve_root(project_root);
if !is_initialized(&resolved_root) {
return Err(CodeGraphError::new(
format!(
"RustCodeGraph not initialized in {}. Run init() first.",
resolved_root.display()
),
"NOT_INITIALIZED",
None,
));
}
let validation = validate_directory(&resolved_root);
if !validation.valid {
return Err(CodeGraphError::new(
format!(
"Invalid RustCodeGraph directory: {}",
validation.errors.join(", ")
),
"INVALID_DIRECTORY",
None,
));
}
let db = DatabaseConnection::open(facade_database_path(&resolved_root))
.map_err(|err| database_error("open", err))?;
let mut instance = Self::new(resolved_root, db);
if options.sync && !options.read_only {
let _ = instance.sync(IndexOptions::default());
}
Ok(instance)
}
pub fn open_sync(project_root: impl AsRef<Path>) -> Result<Self, CodeGraphError> {
Self::open(project_root, OpenOptions::default())
}
pub fn is_initialized(project_root: impl AsRef<Path>) -> bool {
is_initialized(resolve_root(project_root))
}
fn new(project_root: PathBuf, db: DatabaseConnection) -> Self {
Self {
project_root,
db,
indexing: false,
watching: false,
watch_registered: false,
watch_stop: None,
watch_thread: None,
watcher: None,
watcher_degraded_reason: None,
pending_files: Vec::new(),
index_build_info: IndexBuildInfo::default(),
}
}
pub fn close(&mut self) {
self.unwatch();
let _ = self.db.close();
}
pub fn get_project_root(&self) -> String {
self.project_root.to_string_lossy().into_owned()
}
pub fn destroy(&mut self) {
self.close();
}
pub fn uninitialize(&mut self) -> Result<(), CodeGraphError> {
self.close();
remove_directory(&self.project_root)
}
}