use std::sync::Arc;
use ruff_db::Db;
use ruff_db::diagnostic::Span;
use ruff_db::files::system_path_to_file;
use ruff_db::system::SystemPathBuf;
use ruff_python_ast::PythonVersion;
use ruff_text_size::TextRange;
#[derive(Clone, Debug, Eq, PartialEq, Default, get_size2::GetSize)]
pub enum PythonVersionSource {
ConfigFile(PythonVersionFileSource),
PyvenvCfgFile(PythonVersionFileSource),
InstallationDirectoryLayout {
site_packages_parent_dir: Box<str>,
source: Option<PythonVersionFileSource>,
},
Cli,
Editor,
#[default]
Default,
}
#[derive(Debug, PartialEq, Eq, Clone, get_size2::GetSize)]
pub struct PythonVersionFileSource {
path: Arc<SystemPathBuf>,
range: Option<TextRange>,
}
impl PythonVersionFileSource {
pub fn new(path: Arc<SystemPathBuf>, range: Option<TextRange>) -> Self {
Self { path, range }
}
pub fn path(&self) -> &SystemPathBuf {
&self.path
}
pub fn range(&self) -> Option<TextRange> {
self.range
}
pub fn span(&self, db: &dyn Db) -> Option<Span> {
let file = system_path_to_file(db, &*self.path).ok()?;
Some(Span::from(file).with_optional_range(self.range))
}
}
#[derive(Eq, PartialEq, Debug, Clone, get_size2::GetSize)]
pub struct PythonVersionWithSource {
pub version: PythonVersion,
pub source: PythonVersionSource,
}
impl Default for PythonVersionWithSource {
fn default() -> Self {
Self {
version: PythonVersion::latest_ty(),
source: PythonVersionSource::Default,
}
}
}