2023-06-07 02:02:58 +00:00
|
|
|
use std::{
|
|
|
|
|
io::{Error, ErrorKind},
|
|
|
|
|
path::PathBuf,
|
|
|
|
|
};
|
2023-06-05 22:18:10 +00:00
|
|
|
|
|
|
|
|
use filenamify::filenamify;
|
2023-09-03 05:04:52 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-06-05 22:18:10 +00:00
|
|
|
|
2023-06-07 02:02:58 +00:00
|
|
|
use crate::path::{config_file, repositories_dir};
|
2023-06-05 03:08:43 +00:00
|
|
|
|
2023-09-03 05:04:52 +00:00
|
|
|
#[derive(Serialize, Deserialize, Default)]
|
2023-06-05 03:08:43 +00:00
|
|
|
pub struct Config {
|
2023-08-07 09:53:00 +00:00
|
|
|
#[serde(default)]
|
2023-10-25 22:05:23 +00:00
|
|
|
pub repositories: Vec<RepositoryConfig>,
|
|
|
|
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
pub server: ServerConfig,
|
2023-07-13 09:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-05 03:08:43 +00:00
|
|
|
impl Config {
|
2023-06-07 02:02:58 +00:00
|
|
|
pub fn load() -> Result<Self, Error> {
|
|
|
|
|
let file = serdeconv::from_toml_file(crate::path::config_file().as_path());
|
2023-08-07 09:53:00 +00:00
|
|
|
file.map_err(|err| {
|
2023-06-07 02:02:58 +00:00
|
|
|
Error::new(
|
|
|
|
|
ErrorKind::InvalidData,
|
2023-08-07 09:53:00 +00:00
|
|
|
format!(
|
|
|
|
|
"Config {:?} doesn't exist or is not valid: `{:?}`",
|
|
|
|
|
config_file(),
|
|
|
|
|
err
|
|
|
|
|
),
|
2023-06-07 02:02:58 +00:00
|
|
|
)
|
|
|
|
|
})
|
2023-06-05 03:08:43 +00:00
|
|
|
}
|
2023-09-03 05:04:52 +00:00
|
|
|
|
|
|
|
|
#[cfg(feature = "testutils")]
|
|
|
|
|
pub fn save(&self) {
|
|
|
|
|
serdeconv::to_toml_file(self, crate::path::config_file().as_path())
|
|
|
|
|
.expect("Failed to write config file");
|
|
|
|
|
}
|
2023-06-05 03:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-03 05:04:52 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2023-10-25 22:05:23 +00:00
|
|
|
pub struct RepositoryConfig {
|
2023-06-05 03:08:43 +00:00
|
|
|
pub git_url: String,
|
|
|
|
|
}
|
2023-06-05 22:18:10 +00:00
|
|
|
|
2023-10-25 22:05:23 +00:00
|
|
|
impl RepositoryConfig {
|
2023-06-05 22:18:10 +00:00
|
|
|
pub fn dir(&self) -> PathBuf {
|
2023-10-24 01:29:38 +00:00
|
|
|
if self.is_local_dir() {
|
2023-10-24 01:41:52 +00:00
|
|
|
let path = self.git_url.strip_prefix("file://").unwrap();
|
2023-10-24 01:29:38 +00:00
|
|
|
path.into()
|
|
|
|
|
} else {
|
2023-11-24 01:58:56 +00:00
|
|
|
repositories_dir().join(self.name())
|
2023-10-24 01:29:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_local_dir(&self) -> bool {
|
|
|
|
|
self.git_url.starts_with("file://")
|
2023-06-05 22:18:10 +00:00
|
|
|
}
|
2023-11-24 01:58:56 +00:00
|
|
|
|
|
|
|
|
pub fn name(&self) -> String {
|
|
|
|
|
filenamify(&self.git_url)
|
|
|
|
|
}
|
2023-06-05 22:18:10 +00:00
|
|
|
}
|
2023-08-07 09:53:00 +00:00
|
|
|
|
2023-10-25 22:05:23 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
|
pub struct ServerConfig {
|
|
|
|
|
/// The timeout in seconds for the /v1/completion api.
|
|
|
|
|
pub completion_timeout: u64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for ServerConfig {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
completion_timeout: 30,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 09:53:00 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2023-10-25 22:05:23 +00:00
|
|
|
use super::{Config, RepositoryConfig};
|
2023-08-07 09:53:00 +00:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn it_parses_empty_config() {
|
|
|
|
|
let config = serdeconv::from_toml_str::<Config>("");
|
|
|
|
|
debug_assert!(config.is_ok(), "{}", config.err().unwrap());
|
|
|
|
|
}
|
2023-10-24 01:29:38 +00:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn it_parses_local_dir() {
|
2023-10-25 22:05:23 +00:00
|
|
|
let repo = RepositoryConfig {
|
2023-10-24 01:41:52 +00:00
|
|
|
git_url: "file:///home/user".to_owned(),
|
2023-10-24 01:29:38 +00:00
|
|
|
};
|
|
|
|
|
assert!(repo.is_local_dir());
|
|
|
|
|
assert_eq!(repo.dir().display().to_string(), "/home/user");
|
|
|
|
|
|
2023-10-25 22:05:23 +00:00
|
|
|
let repo = RepositoryConfig {
|
2023-10-24 01:29:38 +00:00
|
|
|
git_url: "https://github.com/TabbyML/tabby".to_owned(),
|
|
|
|
|
};
|
|
|
|
|
assert!(!repo.is_local_dir());
|
|
|
|
|
}
|
2023-11-24 01:58:56 +00:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_repository_config_name() {
|
|
|
|
|
let repo = RepositoryConfig {
|
|
|
|
|
git_url: "https://github.com/TabbyML/tabby.git".to_owned(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(repo.name(), "https_github.com_TabbyML_tabby.git");
|
|
|
|
|
}
|
2023-08-07 09:53:00 +00:00
|
|
|
}
|