use std::ops::Range;
use harn_vm::value::DictMap;
use tree_sitter::Tree;
use crate::error::HostlibError;
use crate::tools::args::optional_string;
use super::model::{Family, Lang, ModuleRef};
use super::response::{refuse_with_sites, Outcome, Site};
use super::{rust, text};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Level {
Super,
Crate,
Public,
Export,
}
impl Level {
pub(crate) const ACCEPTED: &'static str =
"`pub(super)`, `pub(crate)`, `pub` (Rust) or `export` (TypeScript/JavaScript)";
pub(crate) fn parse(raw: &str) -> Option<Self> {
Some(match raw {
"pub(super)" => Self::Super,
"pub(crate)" => Self::Crate,
"pub" => Self::Public,
"export" => Self::Export,
_ => return None,
})
}
pub(crate) fn as_str(self) -> &'static str {
match self {
Self::Super => "pub(super)",
Self::Crate => "pub(crate)",
Self::Public => "pub",
Self::Export => "export",
}
}
pub(crate) fn from_request(
builtin: &'static str,
dict: &DictMap,
) -> Result<Option<Self>, HostlibError> {
optional_string(builtin, dict, "allow_visibility_widening")?
.map(|raw| {
Self::parse(&raw).ok_or_else(|| HostlibError::InvalidParameter {
builtin,
param: "allow_visibility_widening",
message: format!("expected {}, got `{raw}`", Self::ACCEPTED),
})
})
.transpose()
}
pub(crate) fn require_for(
self,
builtin: &'static str,
family: Family,
language: &str,
) -> Result<(), HostlibError> {
let (fits, has) = match family {
Family::Rust => (self != Self::Export, "`pub(super)`, `pub(crate)` and `pub`"),
Family::Script => (self == Self::Export, "`export`"),
Family::Python => (false, "no visibility levels"),
};
if fits {
return Ok(());
}
Err(HostlibError::InvalidParameter {
builtin,
param: "allow_visibility_widening",
message: format!(
"`{}` does not apply to {language} (it has {has})",
self.as_str()
),
})
}
}
pub(crate) fn sees(restriction: &str, home: &ModuleRef, viewer: &ModuleRef) -> bool {
let (
ModuleRef::Rust {
crate_dir: home_crate,
path: home_path,
},
ModuleRef::Rust {
crate_dir: viewer_crate,
path: viewer_path,
},
) = (home, viewer)
else {
return false;
};
home_crate == viewer_crate
&& rust::visible_from(home_path, restriction)
.is_some_and(|region| viewer_path.starts_with(®ion))
}
#[derive(Clone, Copy)]
pub(crate) struct Origin<'a> {
pub rel: &'a str,
pub source: &'a str,
pub tree: &'a Tree,
}
pub(crate) fn blocker(
lang: &Lang,
origin: &Origin<'_>,
range: &Range<usize>,
reason: String,
) -> Declared {
let Origin { rel, source, tree } = *origin;
let declared = match lang {
Lang::Rust(_) => rust::declared_item(rel, source, tree, range, reason.clone()),
_ => Some(unexported(rel, source, range, reason.clone())),
};
declared
.unwrap_or_else(|| unplaceable(Site::at(rel, source, range.start, "declaration", reason)))
}
pub(crate) fn item_fix(
lang: &mut Lang,
declared: Declared,
outside_users: &[Site],
dest_module: &ModuleRef,
source: &str,
) -> Option<Vec<Fix>> {
let reach = match lang {
Lang::Rust(_) => {
let mut viewers = Vec::new();
for site in outside_users {
viewers.push(lang.module_of(&site.path)?);
}
Reach::Rust {
home: dest_module.clone(),
viewers,
}
}
_ => Reach::Export,
};
declared.fix(source, reach, true, true).map(|fix| vec![fix])
}
pub(crate) fn reader_fixes(
ws: &mut rust::Workspace,
readers: &[(Site, Option<String>)],
hidden_fields: &[rust::HiddenField],
regions: &[(String, Vec<String>, Vec<String>)],
origin: (&str, &str, &str),
dest_module: &ModuleRef,
) -> Option<Vec<Fix>> {
let (crate_dir, path, source) = origin;
let mut names: Vec<&str> = Vec::new();
for (_, field) in readers {
let field = field.as_deref()?;
if !names.contains(&field) {
names.push(field);
}
}
let mut fixes = Vec::new();
for field in names {
let hidden = hidden_fields.iter().find(|h| h.name == field)?;
let (_, _, after) = regions.iter().find(|(f, ..)| f.as_str() == field)?;
let mut viewers = vec![ModuleRef::Rust {
crate_dir: crate_dir.to_string(),
path: after.clone(),
}];
for (site, read) in readers {
if read.as_deref() == Some(field) {
viewers.push(ws.module_of(&site.path)?);
}
}
let from = hidden.modifier.as_ref().map_or("private".to_string(), |m| {
text::slice(source, m.clone()).replace(' ', "")
});
if from.starts_with("pub(in") {
return None;
}
let at = hidden.modifier.clone().unwrap_or(hidden.at..hidden.at);
fixes.push(Fix {
path: path.to_string(),
line: text::line_of(source, at.start),
at,
from,
reach: Reach::Rust {
home: dest_module.clone(),
viewers,
},
in_moved: true,
item: false,
});
}
Some(fixes)
}
#[derive(Clone, Debug)]
pub(crate) enum Reach {
Rust {
home: ModuleRef,
viewers: Vec<ModuleRef>,
},
Export,
}
impl Reach {
pub(crate) fn of_blocked(family: Family, source: &ModuleRef, dest: &ModuleRef) -> Self {
match family {
Family::Rust => Self::Rust {
home: source.clone(),
viewers: vec![dest.clone()],
},
_ => Self::Export,
}
}
}
pub(crate) fn summary(widened: &[Widening]) -> String {
if widened.is_empty() {
String::new()
} else {
format!("; widened {} declaration(s), see `widened`", widened.len())
}
}
#[derive(Clone, Debug)]
pub(crate) struct Fix {
pub path: String,
pub at: Range<usize>,
pub line: u32,
pub from: String,
pub reach: Reach,
pub in_moved: bool,
pub item: bool,
}
impl Fix {
fn needed(&self) -> Option<Level> {
let Reach::Rust { home, viewers } = &self.reach else {
return Some(Level::Export);
};
let ModuleRef::Rust {
crate_dir,
path: home_path,
} = home
else {
return Some(Level::Public);
};
let mut inside_home = true;
let mut inside_parent = !home_path.is_empty();
for viewer in viewers {
let ModuleRef::Rust { crate_dir: c, path } = viewer else {
return Some(Level::Public);
};
if c != crate_dir {
return Some(Level::Public);
}
if !path.starts_with(home_path) {
inside_home = false;
}
if !home_path.is_empty() && !path.starts_with(&home_path[..home_path.len() - 1]) {
inside_parent = false;
}
}
if inside_home {
None
} else if inside_parent {
Some(Level::Super)
} else {
Some(Level::Crate)
}
}
}
pub(crate) struct Declared {
pub site: Site,
pub path: String,
pub modifier: Option<Range<usize>>,
pub at: usize,
pub from: String,
pub placeable: bool,
pub restriction: Option<String>,
}
impl Declared {
pub(crate) fn fix(
&self,
source: &str,
reach: Reach,
in_moved: bool,
item: bool,
) -> Option<Fix> {
if !self.placeable {
return None;
}
let at = self.modifier.clone().unwrap_or(self.at..self.at);
let mut reach = reach;
if let (Reach::Rust { home, viewers }, Some(path), false) =
(&mut reach, &self.restriction, item)
{
let region = match home {
ModuleRef::Rust {
crate_dir,
path: at_home,
} => rust::visible_from(at_home, path).map(|region| (crate_dir.clone(), region)),
_ => None,
};
if let Some((crate_dir, region)) = region {
viewers.push(ModuleRef::Rust {
crate_dir,
path: region,
});
}
}
Some(Fix {
path: self.path.clone(),
line: text::line_of(source, at.start),
at,
from: self.from.clone(),
reach,
in_moved,
item,
})
}
}
pub(crate) fn unexported(
rel: &str,
source: &str,
range: &Range<usize>,
reason: String,
) -> Declared {
Declared {
site: Site::at(rel, source, range.start, "declaration", reason),
path: rel.to_string(),
modifier: None,
at: range.start,
from: "unexported".to_string(),
placeable: !text::slice(source, range.clone()).starts_with('@'),
restriction: None,
}
}
pub(crate) fn unplaceable(site: Site) -> Declared {
Declared {
path: site.path.clone(),
site,
modifier: None,
at: 0,
from: String::new(),
placeable: false,
restriction: None,
}
}
#[derive(Clone, Debug)]
pub(crate) struct Widening {
pub path: String,
pub line: u32,
pub from: String,
pub to: Level,
pub at: Range<usize>,
pub in_moved: bool,
pub item: bool,
}
impl Widening {
pub(crate) fn text(&self) -> String {
if self.at.is_empty() {
format!("{} ", self.to.as_str())
} else {
self.to.as_str().to_string()
}
}
}
pub(crate) struct Widener {
allowance: Option<Level>,
pub applied: Vec<Widening>,
}
impl Widener {
pub(crate) fn new(allowance: Option<Level>) -> Self {
Self {
allowance,
applied: Vec::new(),
}
}
pub(crate) fn allowed(&self) -> bool {
self.allowance.is_some()
}
pub(crate) fn settle(
&mut self,
details: String,
sites: Vec<Site>,
fixes: Option<Vec<Fix>>,
) -> Option<Outcome> {
let Some(fixes) = fixes else {
return Some(refuse_with_sites("visibility_required", details, sites));
};
let needs: Vec<(Fix, Option<Level>)> = fixes
.into_iter()
.map(|fix| {
let needed = fix.needed();
(fix, needed)
})
.collect();
let required = needs.iter().filter_map(|(_, level)| *level).max();
let within = match (required, self.allowance) {
(None, Some(_)) => true,
(Some(required), Some(allowed)) => required <= allowed,
_ => false,
};
if !within {
let details = match required {
Some(required) => format!(
"{details}; the narrowest fix is `{}` (pass `allow_visibility_widening` to allow it)",
required.as_str()
),
None => details,
};
let mut outcome = refuse_with_sites("visibility_required", details, sites);
outcome.response.required_visibility = required.map(|level| level.as_str().to_string());
return Some(outcome);
}
for (fix, needed) in needs {
let Some(to) = needed else { continue };
if fix.from == to.as_str() {
return Some(refuse_with_sites("visibility_required", details, sites));
}
self.applied.push(Widening {
path: fix.path,
line: fix.line,
from: fix.from,
to,
at: fix.at,
in_moved: fix.in_moved,
item: fix.item,
});
}
None
}
pub(crate) fn moved_rewrites(&self) -> Vec<(Range<usize>, String)> {
self.applied
.iter()
.filter(|w| w.in_moved)
.map(|w| (w.at.clone(), w.text()))
.collect()
}
fn item_level(&self) -> Option<Level> {
self.applied.iter().filter(|w| w.item).map(|w| w.to).max()
}
pub(crate) fn module_visibility(&self, exported: bool) -> &'static str {
if exported {
return "pub";
}
match self.item_level() {
Some(Level::Crate) => "pub(crate)",
Some(Level::Public) => "pub",
_ => "",
}
}
pub(crate) fn module_widening(
&self,
exported: bool,
parent: &str,
line: u32,
) -> Option<Widening> {
let to = self.item_level().filter(|_| !exported)?;
(!self.module_visibility(false).is_empty()).then(|| Widening {
path: parent.to_string(),
line,
from: "private".to_string(),
to,
at: 0..0,
in_moved: false,
item: false,
})
}
}
pub(crate) fn users_see(
lang: &mut Lang,
restriction: Option<&str>,
home: &ModuleRef,
users: &[Site],
) -> bool {
restriction.is_some_and(|path| {
users.iter().all(|site| {
lang.module_of(&site.path)
.is_some_and(|m| sees(path, home, &m))
})
})
}