liblingo/
lib.rs

1use crate::package::tree::GitLock;
2use std::io;
3
4pub mod args;
5pub mod backends;
6pub mod package;
7pub mod util;
8
9#[derive(Debug)]
10pub enum WhichError {
11    /// An executable binary with that name was not found
12    CannotFindBinaryPath,
13    /// There was nowhere to search and the provided name wasn't an absolute path
14    CannotGetCurrentDirAndPathListEmpty,
15    /// Failed to canonicalize the path found
16    CannotCanonicalize,
17}
18#[derive(Debug)]
19pub struct GitCloneError(pub String); // TODO: create a more domain-specific error time like the actual git2::Error
20
21impl std::fmt::Display for WhichError {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        match self {
24            WhichError::CannotFindBinaryPath => write!(f, "cannot find binary"),
25            WhichError::CannotGetCurrentDirAndPathListEmpty => {
26                write!(f, "cannot get current dir and path list empty")
27            }
28            WhichError::CannotCanonicalize => write!(f, "cannot canonicalize"),
29        }
30    }
31}
32
33impl std::fmt::Display for GitCloneError {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        write!(f, "{}", self.0)
36    }
37}
38
39impl std::error::Error for WhichError {}
40
41impl std::error::Error for GitCloneError {}
42
43pub struct GitUrl<'a>(&'a str);
44
45impl<'a> From<&'a str> for GitUrl<'a> {
46    fn from(value: &'a str) -> Self {
47        GitUrl(value)
48    }
49}
50
51impl<'a> From<GitUrl<'a>> for &'a str {
52    fn from(value: GitUrl<'a>) -> Self {
53        value.0
54    }
55}
56
57pub type WhichCapability<'a> = Box<dyn Fn(&str) -> Result<std::path::PathBuf, WhichError> + 'a>;
58pub type GitCloneCapability<'a> =
59    Box<dyn Fn(GitUrl, &std::path::Path) -> Result<(), GitCloneError> + 'a>;
60pub type FsReadCapability<'a> = Box<dyn Fn(&std::path::Path) -> io::Result<String> + 'a>;
61pub type GitCloneAndCheckoutCap<'a> = Box<
62    dyn Fn(GitUrl, &std::path::Path, Option<GitLock>) -> Result<Option<String>, GitCloneError> + 'a,
63>;