fusabi-pm 0.1.0

Fusabi Package Manager
Documentation
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Registry handling for the fusabi-community package registry.
//!
//! Provides functionality to fetch and parse the registry index, and resolve
//! package names to git URLs with version constraints.

use git2::Repository;
use serde::Deserialize;
use std::cmp::Ordering;
use std::path::{Path, PathBuf};
use thiserror::Error;

const REGISTRY_URL: &str = "https://github.com/fusabi-lang/fusabi-community";
const REGISTRY_INDEX_PATH: &str = "registry/index.toml";

#[derive(Debug, Error)]
pub enum RegistryError {
    #[error("Git error: {0}")]
    Git(#[from] git2::Error),

    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Failed to parse registry index: {0}")]
    Parse(#[from] toml::de::Error),

    #[error("Package '{0}' not found in registry")]
    PackageNotFound(String),

    #[error("No version of '{0}' satisfies constraint '{1}'")]
    NoMatchingVersion(String, String),

    #[error("Invalid version format: {0}")]
    InvalidVersion(String),
}

#[derive(Debug, Clone, Deserialize)]
pub struct RegistryIndex {
    #[serde(default)]
    pub packages: Vec<RegistryPackage>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct RegistryPackage {
    pub name: String,
    pub version: String,
    pub repository: String,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub license: Option<String>,
    #[serde(default)]
    pub authors: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct ResolvedPackage {
    pub name: String,
    pub version: String,
    pub git_url: String,
    pub rev: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SemVer {
    pub major: u64,
    pub minor: u64,
    pub patch: u64,
}

impl SemVer {
    pub fn parse(version: &str) -> Result<Self, RegistryError> {
        let version = version.trim_start_matches('v');
        let parts: Vec<&str> = version.split('.').collect();

        let major = parts
            .first()
            .and_then(|s| s.parse().ok())
            .ok_or_else(|| RegistryError::InvalidVersion(version.to_string()))?;

        let minor = parts.get(1).and_then(|s| s.parse().ok()).unwrap_or(0);

        let patch = parts.get(2).and_then(|s| s.parse().ok()).unwrap_or(0);

        Ok(SemVer {
            major,
            minor,
            patch,
        })
    }

    pub fn satisfies(&self, constraint: &str) -> bool {
        let constraint = constraint.trim();

        if constraint.is_empty() || constraint == "*" {
            return true;
        }

        if constraint.starts_with('^') {
            if let Ok(base) = SemVer::parse(&constraint[1..]) {
                return self.major == base.major
                    && (self.minor > base.minor
                        || (self.minor == base.minor && self.patch >= base.patch));
            }
        }

        if constraint.starts_with('~') {
            if let Ok(base) = SemVer::parse(&constraint[1..]) {
                return self.major == base.major
                    && self.minor == base.minor
                    && self.patch >= base.patch;
            }
        }

        if constraint.starts_with(">=") {
            if let Ok(base) = SemVer::parse(&constraint[2..]) {
                return *self >= base;
            }
        }

        if constraint.starts_with('>') {
            if let Ok(base) = SemVer::parse(&constraint[1..]) {
                return *self > base;
            }
        }

        if constraint.starts_with("<=") {
            if let Ok(base) = SemVer::parse(&constraint[2..]) {
                return *self <= base;
            }
        }

        if constraint.starts_with('<') {
            if let Ok(base) = SemVer::parse(&constraint[1..]) {
                return *self < base;
            }
        }

        if constraint.starts_with('=') {
            if let Ok(base) = SemVer::parse(&constraint[1..]) {
                return *self == base;
            }
        }

        if let Ok(base) = SemVer::parse(constraint) {
            return *self == base;
        }

        false
    }
}

impl PartialOrd for SemVer {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for SemVer {
    fn cmp(&self, other: &Self) -> Ordering {
        match self.major.cmp(&other.major) {
            Ordering::Equal => match self.minor.cmp(&other.minor) {
                Ordering::Equal => self.patch.cmp(&other.patch),
                ord => ord,
            },
            ord => ord,
        }
    }
}

pub struct Registry {
    cache_dir: PathBuf,
}

impl Registry {
    pub fn new() -> Self {
        let cache_dir = dirs::cache_dir()
            .unwrap_or_else(|| PathBuf::from("."))
            .join("fusabi")
            .join("registry");

        Self { cache_dir }
    }

    pub fn with_cache_dir<P: AsRef<Path>>(cache_dir: P) -> Self {
        Self {
            cache_dir: cache_dir.as_ref().to_path_buf(),
        }
    }

    pub fn update(&self) -> Result<(), RegistryError> {
        std::fs::create_dir_all(&self.cache_dir)?;

        let repo_path = self.cache_dir.join("fusabi-community");

        if repo_path.exists() {
            println!("Updating registry index...");
            let repo = Repository::open(&repo_path)?;
            let mut remote = repo.find_remote("origin")?;
            remote.fetch(&["main"], None, None)?;

            let fetch_head = repo.find_reference("FETCH_HEAD")?;
            let commit = repo.reference_to_annotated_commit(&fetch_head)?;
            let (analysis, _) = repo.merge_analysis(&[&commit])?;

            if analysis.is_fast_forward() || analysis.is_normal() {
                let refname = "refs/heads/main";
                let mut reference = repo.find_reference(refname)?;
                reference.set_target(commit.id(), "Fast-forward")?;
                repo.set_head(refname)?;
                repo.checkout_head(Some(git2::build::CheckoutBuilder::default().force()))?;
            }
        } else {
            println!("Cloning registry index...");
            Repository::clone(REGISTRY_URL, &repo_path)?;
        }

        println!("Registry updated.");
        Ok(())
    }

    pub fn load_index(&self) -> Result<RegistryIndex, RegistryError> {
        let index_path = self
            .cache_dir
            .join("fusabi-community")
            .join(REGISTRY_INDEX_PATH);

        if !index_path.exists() {
            self.update()?;
        }

        let contents = std::fs::read_to_string(&index_path)?;
        let index: RegistryIndex = toml::from_str(&contents)?;
        Ok(index)
    }

    pub fn resolve(
        &self,
        name: &str,
        version_constraint: &str,
    ) -> Result<ResolvedPackage, RegistryError> {
        let index = self.load_index()?;

        let matching_packages: Vec<&RegistryPackage> =
            index.packages.iter().filter(|p| p.name == name).collect();

        if matching_packages.is_empty() {
            return Err(RegistryError::PackageNotFound(name.to_string()));
        }

        let mut candidates: Vec<(&RegistryPackage, SemVer)> = matching_packages
            .into_iter()
            .filter_map(|p| {
                let semver = SemVer::parse(&p.version).ok()?;
                if semver.satisfies(version_constraint) {
                    Some((p, semver))
                } else {
                    None
                }
            })
            .collect();

        candidates.sort_by(|a, b| b.1.cmp(&a.1));

        let (package, _) = candidates.into_iter().next().ok_or_else(|| {
            RegistryError::NoMatchingVersion(name.to_string(), version_constraint.to_string())
        })?;

        let rev = Some(format!("v{}", package.version));

        Ok(ResolvedPackage {
            name: package.name.clone(),
            version: package.version.clone(),
            git_url: package.repository.clone(),
            rev,
        })
    }

    pub fn search(&self, query: &str) -> Result<Vec<RegistryPackage>, RegistryError> {
        let index = self.load_index()?;
        let query_lower = query.to_lowercase();

        Ok(index
            .packages
            .into_iter()
            .filter(|p| {
                p.name.to_lowercase().contains(&query_lower)
                    || p.description
                        .as_ref()
                        .is_some_and(|d| d.to_lowercase().contains(&query_lower))
            })
            .collect())
    }
}

impl Default for Registry {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_semver_parse() {
        let v = SemVer::parse("1.2.3").unwrap();
        assert_eq!(v.major, 1);
        assert_eq!(v.minor, 2);
        assert_eq!(v.patch, 3);

        let v = SemVer::parse("v2.0").unwrap();
        assert_eq!(v.major, 2);
        assert_eq!(v.minor, 0);
        assert_eq!(v.patch, 0);

        let v = SemVer::parse("1").unwrap();
        assert_eq!(v.major, 1);
        assert_eq!(v.minor, 0);
        assert_eq!(v.patch, 0);
    }

    #[test]
    fn test_semver_satisfies_star() {
        let v = SemVer::parse("1.2.3").unwrap();
        assert!(v.satisfies("*"));
        assert!(v.satisfies(""));
    }

    #[test]
    fn test_semver_satisfies_caret() {
        let v100 = SemVer::parse("1.0.0").unwrap();
        let v110 = SemVer::parse("1.1.0").unwrap();
        let v200 = SemVer::parse("2.0.0").unwrap();

        assert!(v100.satisfies("^1.0.0"));
        assert!(v110.satisfies("^1.0.0"));
        assert!(!v200.satisfies("^1.0.0"));
        assert!(!v100.satisfies("^1.1.0"));
    }

    #[test]
    fn test_semver_satisfies_tilde() {
        let v100 = SemVer::parse("1.0.0").unwrap();
        let v101 = SemVer::parse("1.0.1").unwrap();
        let v110 = SemVer::parse("1.1.0").unwrap();

        assert!(v100.satisfies("~1.0.0"));
        assert!(v101.satisfies("~1.0.0"));
        assert!(!v110.satisfies("~1.0.0"));
    }

    #[test]
    fn test_semver_satisfies_exact() {
        let v = SemVer::parse("1.2.3").unwrap();
        assert!(v.satisfies("1.2.3"));
        assert!(v.satisfies("=1.2.3"));
        assert!(!v.satisfies("1.2.4"));
    }

    #[test]
    fn test_semver_satisfies_comparison() {
        let v = SemVer::parse("1.5.0").unwrap();

        assert!(v.satisfies(">=1.0.0"));
        assert!(v.satisfies(">1.0.0"));
        assert!(v.satisfies("<=2.0.0"));
        assert!(v.satisfies("<2.0.0"));
        assert!(!v.satisfies(">2.0.0"));
        assert!(!v.satisfies("<1.0.0"));
    }

    #[test]
    fn test_semver_ordering() {
        let v100 = SemVer::parse("1.0.0").unwrap();
        let v101 = SemVer::parse("1.0.1").unwrap();
        let v110 = SemVer::parse("1.1.0").unwrap();
        let v200 = SemVer::parse("2.0.0").unwrap();

        assert!(v100 < v101);
        assert!(v101 < v110);
        assert!(v110 < v200);
    }

    #[test]
    fn test_registry_index_parse() {
        let toml = r#"
[[packages]]
name = "test-pkg"
version = "1.0.0"
repository = "https://github.com/test/test-pkg"
description = "A test package"

[[packages]]
name = "another-pkg"
version = "0.1.0"
repository = "https://github.com/test/another"
"#;

        let index: RegistryIndex = toml::from_str(toml).unwrap();
        assert_eq!(index.packages.len(), 2);
        assert_eq!(index.packages[0].name, "test-pkg");
        assert_eq!(index.packages[1].name, "another-pkg");
    }
}