mod api;
mod docs;
mod entries;
mod packages;
mod ranking;
mod render;
mod workspace;
#[cfg(test)]
mod tests;
pub use ranking::redirects;
pub use render::render_entry;
use crate::index::PackageIndex;
use crate::project::Project;
use serde_json::Value;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
pub const DEFAULT_TOKENS: usize = 1200;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Options {
pub fetch: bool,
pub upstream: bool,
pub private: bool,
}
impl Default for Options {
fn default() -> Self {
let mut fetch = std::env::var("LOCKDOCS_FETCH").is_ok_and(|v| matches!(v.as_str(), "1" | "true" | "yes"));
let mut upstream = automatic_upstream(std::env::var("LOCKDOCS_FETCH").ok().as_deref(), std::env::var("LOCKDOCS_NO_UPSTREAM").is_ok());
if std::env::var("LOCKDOCS_OFFLINE").is_ok_and(|v| v != "0") {
fetch = false;
upstream = false;
}
Options {
fetch,
upstream,
private: false,
}
}
}
fn automatic_upstream(fetch: Option<&str>, disabled: bool) -> bool {
!disabled && !fetch.is_some_and(|v| matches!(v, "0" | "false" | "no"))
}
pub struct Answer {
pub text: String,
pub json: Value,
}
pub struct Engine {
pub project: Project,
opts: Options,
indexes: Mutex<HashMap<String, Arc<PackageIndex>>>,
upstream_notes: Mutex<HashMap<String, String>>,
private_blocked: Mutex<Vec<crate::private::PrivateRequired>>,
}
impl Engine {
pub fn new(root: &Path, opts: Options) -> Engine {
Engine {
project: Project::load(root),
opts,
indexes: Mutex::new(HashMap::new()),
upstream_notes: Mutex::new(HashMap::new()),
private_blocked: Mutex::new(Vec::new()),
}
}
pub fn take_private_blocked(&self) -> Vec<crate::private::PrivateRequired> {
std::mem::take(&mut *self.private_blocked.lock().unwrap())
}
pub fn root(&self) -> &Path {
&self.project.root
}
}
#[derive(Default)]
pub struct Workspace {
engines: Mutex<HashMap<PathBuf, (u64, Arc<Engine>)>>,
}