Skip to main content

cgx_core/messages/
source.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5use super::Message;
6use crate::crate_resolver::{ResolvedCrate, ResolvedSource};
7
8/// Messages related to source code downloading and source cache operations.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(tag = "event", rename_all = "snake_case")]
11pub enum SourceMessage {
12    CacheLookup {
13        name: String,
14        version: String,
15        source: ResolvedSource,
16    },
17    CacheHit {
18        path: PathBuf,
19    },
20    CacheMiss {
21        name: String,
22        version: String,
23        source: ResolvedSource,
24    },
25    Downloading {
26        name: String,
27        version: String,
28        source: ResolvedSource,
29    },
30    Downloaded {
31        path: PathBuf,
32    },
33    CacheStored {
34        path: PathBuf,
35    },
36}
37
38impl SourceMessage {
39    pub fn cache_lookup(resolved: &ResolvedCrate) -> Self {
40        Self::CacheLookup {
41            name: resolved.name.clone(),
42            version: resolved.version.to_string(),
43            source: resolved.source.clone(),
44        }
45    }
46
47    pub fn cache_hit(path: &std::path::Path) -> Self {
48        Self::CacheHit {
49            path: path.to_path_buf(),
50        }
51    }
52
53    pub fn cache_miss(resolved: &ResolvedCrate) -> Self {
54        Self::CacheMiss {
55            name: resolved.name.clone(),
56            version: resolved.version.to_string(),
57            source: resolved.source.clone(),
58        }
59    }
60
61    pub fn downloading(resolved: &ResolvedCrate) -> Self {
62        Self::Downloading {
63            name: resolved.name.clone(),
64            version: resolved.version.to_string(),
65            source: resolved.source.clone(),
66        }
67    }
68
69    pub fn downloaded(path: &std::path::Path) -> Self {
70        Self::Downloaded {
71            path: path.to_path_buf(),
72        }
73    }
74
75    pub fn cache_stored(path: &std::path::Path) -> Self {
76        Self::CacheStored {
77            path: path.to_path_buf(),
78        }
79    }
80}
81
82impl From<SourceMessage> for Message {
83    fn from(msg: SourceMessage) -> Self {
84        Message::Source(msg)
85    }
86}