use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceLocation {
pub repository: PathBuf,
pub file_path: PathBuf,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub git_ref: Option<String>,
}
impl SourceLocation {
pub fn new(repository: impl Into<PathBuf>, file_path: impl Into<PathBuf>) -> Self {
Self {
repository: repository.into(),
file_path: file_path.into(),
git_ref: None,
}
}
pub fn with_git_ref(
repository: impl Into<PathBuf>,
file_path: impl Into<PathBuf>,
git_ref: impl Into<String>,
) -> Self {
Self {
repository: repository.into(),
file_path: file_path.into(),
git_ref: Some(git_ref.into()),
}
}
pub fn full_path(&self) -> PathBuf {
self.repository.join(&self.file_path)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_source_location_full_path() {
let loc = SourceLocation::new("/repo", "docs/SOL-001.md");
assert_eq!(loc.full_path(), PathBuf::from("/repo/docs/SOL-001.md"));
}
#[test]
fn test_source_location_with_git_ref() {
let loc = SourceLocation::with_git_ref("/repo", "docs/SOL-001.md", "main");
assert_eq!(loc.git_ref, Some("main".to_string()));
}
}