use std::fmt;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::model::{Profile, VarId};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ProjectId(Uuid);
impl ProjectId {
#[must_use]
pub fn new_v4() -> Self {
Self(Uuid::new_v4())
}
#[must_use]
pub const fn from_uuid(id: Uuid) -> Self {
Self(id)
}
#[must_use]
pub const fn as_uuid(&self) -> &Uuid {
&self.0
}
}
impl fmt::Display for ProjectId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Project {
id: ProjectId,
name: String,
path: PathBuf,
}
impl Project {
pub fn new(name: impl Into<String>, path: PathBuf) -> Self {
Self {
id: ProjectId::new_v4(),
name: name.into(),
path,
}
}
#[must_use]
pub const fn from_parts(id: ProjectId, name: String, path: PathBuf) -> Self {
Self { id, name, path }
}
#[must_use]
pub const fn id(&self) -> ProjectId {
self.id
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub fn path(&self) -> &std::path::Path {
self.path.as_path()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProjectVar {
pub project_id: ProjectId,
pub var_id: VarId,
pub alias: Option<String>,
pub profile: Profile,
}
impl ProjectVar {
#[must_use]
pub fn new(project_id: ProjectId, var_id: VarId) -> Self {
Self {
project_id,
var_id,
alias: None,
profile: Profile::default_profile(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_project_has_fresh_id() {
let a = Project::new("a", PathBuf::from("./a"));
let b = Project::new("b", PathBuf::from("./b"));
assert_ne!(a.id(), b.id());
}
#[test]
fn project_var_default_profile_no_alias() {
let pid = ProjectId::new_v4();
let vid = VarId::new_v4();
let pv = ProjectVar::new(pid, vid);
assert_eq!(pv.project_id, pid);
assert_eq!(pv.var_id, vid);
assert!(pv.alias.is_none());
assert!(pv.profile.is_default());
}
#[test]
fn project_id_display_matches_uuid() {
let id = ProjectId::new_v4();
assert_eq!(id.to_string(), id.as_uuid().to_string());
}
}