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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use crate::bson::{DateTime, ObjectId};
use crate::lowlevel::{FFISlice, FromFFI, ToFFI};
use std::fmt::{Debug, Formatter};

#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct ProjectType(u32);

impl ProjectType {
    pub const UNKNOWN: Self = Self(0);
    pub const LEGACY_SDK2: Self = Self(1);
    pub const LEGACY_WORLDS: Self = Self(2);
    pub const LEGACY_AVATARS: Self = Self(3);
    pub const UPM_WORLDS: Self = Self(4);
    pub const UPM_AVATARS: Self = Self(5);
    pub const UPM_STARTER: Self = Self(6);
    pub const WORLDS: Self = Self(7);
    pub const AVATARS: Self = Self(8);
    pub const VPM_STARTER: Self = Self(9);
}

impl Debug for ProjectType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match *self {
            ProjectType::UNKNOWN => f.write_str("Unknown"),
            ProjectType::LEGACY_SDK2 => f.write_str("Legacy SDK2"),
            ProjectType::LEGACY_WORLDS => f.write_str("Legacy Worlds"),
            ProjectType::LEGACY_AVATARS => f.write_str("Legacy Avatars"),
            ProjectType::UPM_WORLDS => f.write_str("UPM Worlds"),
            ProjectType::UPM_AVATARS => f.write_str("UPM Avatars"),
            ProjectType::UPM_STARTER => f.write_str("UPM Starter"),
            ProjectType::WORLDS => f.write_str("Worlds"),
            ProjectType::AVATARS => f.write_str("Avatars"),
            ProjectType::VPM_STARTER => f.write_str("VPM Starter"),
            _ => f.write_fmt(format_args!("Unexpected({})", self.0)),
        }
    }
}

/// Represents a VCC Project
#[derive(Debug)]
pub struct Project {
    path: Box<str>,
    unity_version: Option<Box<str>>,
    created_at: DateTime,
    last_modified: DateTime,
    type_: ProjectType,
    id: ObjectId,
    favorite: bool,
}

impl Project {
    pub fn new(path: Box<str>, unity_version: Option<Box<str>>, project_type: ProjectType) -> Self {
        let created_at: DateTime = DateTime::now();

        Self {
            path,
            unity_version,
            created_at,
            last_modified: created_at,
            type_: project_type,
            id: ObjectId::new(),
            favorite: false,
        }
    }

    pub fn id(&self) -> ObjectId {
        self.id
    }

    pub fn path(&self) -> &str {
        &self.path
    }

    pub fn project_type(&self) -> ProjectType {
        self.type_
    }

    pub fn unity_version(&self) -> Option<&str> {
        self.unity_version.as_deref()
    }

    pub fn favorite(&self) -> bool {
        self.favorite
    }

    pub fn created_at(&self) -> DateTime {
        self.created_at
    }

    pub fn last_modified(&self) -> DateTime {
        self.last_modified
    }

    pub fn set_path(&mut self, path: Box<str>) {
        self.path = path;
    }

    pub fn set_project_type(&mut self, project_type: ProjectType) {
        self.type_ = project_type;
    }

    pub fn set_unity_version(&mut self, unity_version: Option<Box<str>>) {
        self.unity_version = unity_version;
    }

    pub fn set_favorite(&mut self, favorite: bool) {
        self.favorite = favorite;
    }

    pub fn set_last_modified(&mut self, last_modified: DateTime) {
        self.last_modified = last_modified;
    }
}

#[repr(C)]
pub(crate) struct ProjectFFI {
    path: FFISlice,
    unity_version: FFISlice,
    created_at: DateTime,
    last_modified: DateTime,
    type_: ProjectType,
    id: ObjectId,
    favorite: u8,
}

impl FromFFI for Project {
    type FFIType = ProjectFFI;

    unsafe fn from_ffi(ffi: ProjectFFI) -> Self {
        Self {
            path: FromFFI::from_ffi(ffi.path),
            unity_version: FromFFI::from_ffi(ffi.unity_version),
            created_at: ffi.created_at,
            last_modified: ffi.last_modified,
            type_: ffi.type_,
            id: ffi.id,
            favorite: ffi.favorite != 0,
        }
    }
}

impl ToFFI for Project {
    type FFIType = ProjectFFI;

    unsafe fn to_ffi(&self) -> Self::FFIType {
        ProjectFFI {
            path: self.path.as_ref().to_ffi(),
            unity_version: self.unity_version.as_deref().to_ffi(),
            created_at: self.created_at,
            last_modified: self.last_modified,
            type_: self.type_,
            id: self.id,
            favorite: self.favorite as u8,
        }
    }
}