evault_core/model/
project.rs1use std::fmt;
4use std::path::PathBuf;
5
6use serde::{Deserialize, Serialize};
7use uuid::Uuid;
8
9use crate::model::{Profile, VarId};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
13pub struct ProjectId(Uuid);
14
15impl ProjectId {
16 #[must_use]
18 pub fn new_v4() -> Self {
19 Self(Uuid::new_v4())
20 }
21
22 #[must_use]
24 pub const fn from_uuid(id: Uuid) -> Self {
25 Self(id)
26 }
27
28 #[must_use]
30 pub const fn as_uuid(&self) -> &Uuid {
31 &self.0
32 }
33}
34
35impl fmt::Display for ProjectId {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 fmt::Display::fmt(&self.0, f)
38 }
39}
40
41#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
55pub struct Project {
56 id: ProjectId,
57 name: String,
58 path: PathBuf,
59}
60
61impl Project {
62 pub fn new(name: impl Into<String>, path: PathBuf) -> Self {
67 Self {
68 id: ProjectId::new_v4(),
69 name: name.into(),
70 path,
71 }
72 }
73
74 #[must_use]
76 pub const fn from_parts(id: ProjectId, name: String, path: PathBuf) -> Self {
77 Self { id, name, path }
78 }
79
80 #[must_use]
82 pub const fn id(&self) -> ProjectId {
83 self.id
84 }
85
86 #[must_use]
88 pub fn name(&self) -> &str {
89 &self.name
90 }
91
92 #[must_use]
94 pub fn path(&self) -> &std::path::Path {
95 self.path.as_path()
96 }
97}
98
99#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
104pub struct ProjectVar {
105 pub project_id: ProjectId,
107 pub var_id: VarId,
109 pub alias: Option<String>,
111 pub profile: Profile,
113}
114
115impl ProjectVar {
116 #[must_use]
118 pub fn new(project_id: ProjectId, var_id: VarId) -> Self {
119 Self {
120 project_id,
121 var_id,
122 alias: None,
123 profile: Profile::default_profile(),
124 }
125 }
126}
127
128#[cfg(test)]
129mod tests {
130 use super::*;
131
132 #[test]
133 fn new_project_has_fresh_id() {
134 let a = Project::new("a", PathBuf::from("./a"));
135 let b = Project::new("b", PathBuf::from("./b"));
136 assert_ne!(a.id(), b.id());
137 }
138
139 #[test]
140 fn project_var_default_profile_no_alias() {
141 let pid = ProjectId::new_v4();
142 let vid = VarId::new_v4();
143 let pv = ProjectVar::new(pid, vid);
144 assert_eq!(pv.project_id, pid);
145 assert_eq!(pv.var_id, vid);
146 assert!(pv.alias.is_none());
147 assert!(pv.profile.is_default());
148 }
149
150 #[test]
151 fn project_id_display_matches_uuid() {
152 let id = ProjectId::new_v4();
153 assert_eq!(id.to_string(), id.as_uuid().to_string());
154 }
155}