1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//! Utilities for use while developing dynamic libraries.

use std::{
    io,
    path::{Path, PathBuf},
};

use crate::library::RootModule;

/// Returns the path in the target directory
/// to the last version of an implementation crate's dynamic library.
///
/// The path can be in either the "debug" or "release" subdirectories.
pub fn compute_library_path<M: RootModule>(target_path: &Path) -> io::Result<PathBuf> {
    let debug_dir = target_path.join("debug/");
    let release_dir = target_path.join("release/");

    let debug_path = M::get_library_path(&debug_dir);
    let release_path = M::get_library_path(&release_dir);

    Ok(match (debug_path.exists(), release_path.exists()) {
        (false, false) => debug_dir,
        (true, false) => debug_dir,
        (false, true) => release_dir,
        (true, true) => {
            if debug_path.metadata()?.modified()? < release_path.metadata()?.modified()? {
                release_dir
            } else {
                debug_dir
            }
        }
    })
}