1use crate::error::Error;
2use crate::error::Result;
3use rusqlite::Connection;
4
5use crate::db;
6use crate::db::DB_PATH;
7
8#[derive(Debug)]
9pub struct MDDBProject {
10 root: String,
11 conn: Connection,
12}
13
14impl MDDBProject {
15 pub fn new(root: impl AsRef<std::path::Path>) -> Result<Self> {
16 let root_path = root.as_ref().canonicalize()?.to_string_lossy().into_owned();
17 let conn = db::start(&root_path)?;
18
19 Ok(Self {
20 root: root_path,
21 conn,
22 })
23 }
24
25 pub fn open(root: impl AsRef<std::path::Path>) -> Result<Self> {
27 let root_path = root.as_ref().canonicalize()?.to_string_lossy().into_owned();
28 let db_path = std::path::Path::new(&root_path).join(DB_PATH);
29 if !db_path.exists() {
30 return Err(Error::ProjectNotFound);
31 }
32 let conn = db::start(&root_path)?;
33 Ok(Self {
34 root: root_path,
35 conn,
36 })
37 }
38
39 pub fn get_conn(&self) -> &Connection {
41 &self.conn
42 }
43
44 pub fn get_root(&self) -> &str {
45 &self.root
46 }
47}