RIIR: factor out Repository struct

andybalholm
Mar 17, 2023, 10:11 PM
ATRA7XTTN62JZQOI7A4JI3Z7L3XVTAEEW2U4AX6UQKEQ6USOXQDQC

Dependencies

Change contents

  • file addition: repo.rs (----------)
    [3.33]
    use std::error::Error;
    use std::fmt;
    use std::path::Path;
    use libpijul::pristine::sanakirja::Pristine;
    use libpijul::TxnT;
    use libpijul::TxnTExt;
    pub struct Repository {
    pristine: Pristine,
    }
    pub struct Change {
    pub hash: String,
    }
    #[derive(Debug, Clone)]
    pub struct NoSuchChannelError {
    channel_name: String,
    }
    impl fmt::Display for NoSuchChannelError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "no such channel: {}", self.channel_name)
    }
    }
    impl Error for NoSuchChannelError {}
    impl Repository {
    pub fn load(path: &str) -> Result<Repository, Box<dyn Error>> {
    let repo_path = Path::new(path);
    let pristine_path = repo_path.join(".pijul/pristine/db");
    let pristine = Pristine::new(pristine_path)?;
    return Ok(Repository {
    pristine,
    });
    }
    pub fn log(&mut self, channel_name: &str) -> Result<Vec<Change>, Box<dyn Error>> {
    let txn = self.pristine.txn_begin()?;
    let channel = txn.load_channel(&channel_name)?.ok_or(NoSuchChannelError {
    channel_name: channel_name.to_string(),
    })?;
    let log = txn.log(&*channel.read(), 0)?;
    let mut changes: Vec<Change> = Vec::new();
    for pr in log {
    let (_, (h, _)) = pr?;
    changes.push(Change {
    hash: format!("{:?}", h),
    });
    }
    return Ok(changes);
    }
    }
  • edit in rust/src/main.rs at line 2
    [2.14][2.14:35]()
    use std::path::Path;
  • replacement in rust/src/main.rs at line 4
    [2.58][2.58:146]()
    use libpijul::pristine::sanakirja::Pristine;
    use libpijul::TxnT;
    use libpijul::TxnTExt;
    [2.58]
    [2.146]
    mod repo;
    use repo::Repository;
  • replacement in rust/src/main.rs at line 13
    [2.342][2.342:506]()
    let repo = matches.opt_str("r").unwrap_or(".".to_string());
    let repo_path = Path::new(&repo);
    let pristine_path = repo_path.join(".pijul/pristine/db");
    [2.342]
    [2.506]
    let repo_path = matches.opt_str("r").unwrap_or(".".to_string());
  • replacement in rust/src/main.rs at line 15
    [2.507][2.507:939]()
    let pristine = Pristine::new(pristine_path).unwrap();
    let txn = pristine.txn_begin().unwrap();
    let channel_name = txn.current_channel().unwrap();
    println!("On channel {}", channel_name);
    let channel = txn.load_channel(channel_name).unwrap().unwrap();
    let rev_log = txn.reverse_log(&*channel.read(), None).unwrap();
    for pr in rev_log {
    let (_, (h, _)) = pr.unwrap();
    println!("{:?}", h);
    [2.507]
    [2.939]
    let mut repo = Repository::load(&repo_path).unwrap();
    let changes = repo.log("main").unwrap();
    for c in changes {
    println!("{}", c.hash);