use std::fs;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
use super::super::*;
pub(super) fn trash_root() -> PathBuf {
package_root().join(".nichlink").join("trash")
}
pub(super) fn stamp() -> Result<u128, String> {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|elapsed| elapsed.as_nanos())
.map_err(|error| format!("clock error: {error}"))
}
pub(super) fn stash_face_source(
source: &Path,
id: NodeId,
previous: &str,
) -> Result<PathBuf, String> {
let name = source
.file_stem()
.and_then(|stem| stem.to_str())
.unwrap_or("face");
let directory = trash_root().join("faces");
fs::create_dir_all(&directory)
.map_err(|error| format!("cannot create NichLink trash: {error}"))?;
let path = directory.join(format!("{name}-{id}-{}.rs", stamp()?));
fs::write(&path, previous).map_err(|error| {
format!(
"cannot keep the previous text at {} before rewriting: {error}",
path.display()
)
})?;
Ok(path)
}