use crate::{
Docs, Function, InterfaceId, PackageId, Resolve, Stability, TypeDefKind, TypeId, WorldId,
WorldItem, WorldKey,
};
use anyhow::{bail, Result};
use indexmap::IndexMap;
#[cfg(feature = "serde")]
use serde_derive::{Deserialize, Serialize};
type StringMap<V> = IndexMap<String, V>;
#[cfg(feature = "serde")]
const PACKAGE_DOCS_SECTION_VERSION: u8 = 1;
const TRY_TO_EMIT_V0_BY_DEFAULT: bool = true;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct PackageMetadata {
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
docs: Option<String>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "StringMap::is_empty")
)]
worlds: StringMap<WorldMetadata>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "StringMap::is_empty")
)]
interfaces: StringMap<InterfaceMetadata>,
}
impl PackageMetadata {
pub const SECTION_NAME: &'static str = "package-docs";
pub fn extract(resolve: &Resolve, package: PackageId) -> Self {
let package = &resolve.packages[package];
let worlds = package
.worlds
.iter()
.map(|(name, id)| (name.to_string(), WorldMetadata::extract(resolve, *id)))
.filter(|(_, item)| !item.is_empty())
.collect();
let interfaces = package
.interfaces
.iter()
.map(|(name, id)| (name.to_string(), InterfaceMetadata::extract(resolve, *id)))
.filter(|(_, item)| !item.is_empty())
.collect();
Self {
docs: package.docs.contents.as_deref().map(Into::into),
worlds,
interfaces,
}
}
pub fn inject(&self, resolve: &mut Resolve, package: PackageId) -> Result<()> {
for (name, docs) in &self.worlds {
let Some(&id) = resolve.packages[package].worlds.get(name) else {
bail!("missing world {name:?}");
};
docs.inject(resolve, id)?;
}
for (name, docs) in &self.interfaces {
let Some(&id) = resolve.packages[package].interfaces.get(name) else {
bail!("missing interface {name:?}");
};
docs.inject(resolve, id)?;
}
if let Some(docs) = &self.docs {
resolve.packages[package].docs.contents = Some(docs.to_string());
}
Ok(())
}
#[cfg(feature = "serde")]
pub fn encode(&self) -> Result<Vec<u8>> {
let mut data = vec![
if TRY_TO_EMIT_V0_BY_DEFAULT && self.is_compatible_with_v0() {
0
} else {
PACKAGE_DOCS_SECTION_VERSION
},
];
serde_json::to_writer(&mut data, self)?;
Ok(data)
}
#[cfg(feature = "serde")]
pub fn decode(data: &[u8]) -> Result<Self> {
match data.first().copied() {
Some(0) | Some(PACKAGE_DOCS_SECTION_VERSION) => {}
version => {
bail!(
"expected package-docs version {PACKAGE_DOCS_SECTION_VERSION}, got {version:?}"
);
}
}
Ok(serde_json::from_slice(&data[1..])?)
}
#[cfg(feature = "serde")]
fn is_compatible_with_v0(&self) -> bool {
self.worlds.iter().all(|(_, w)| w.is_compatible_with_v0())
&& self
.interfaces
.iter()
.all(|(_, w)| w.is_compatible_with_v0())
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
struct WorldMetadata {
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
docs: Option<String>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Stability::is_unknown")
)]
stability: Stability,
#[cfg_attr(
feature = "serde",
serde(
default,
rename = "interfaces",
skip_serializing_if = "StringMap::is_empty"
)
)]
interface_imports_or_exports: StringMap<InterfaceMetadata>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "StringMap::is_empty")
)]
types: StringMap<TypeMetadata>,
#[cfg_attr(
feature = "serde",
serde(default, rename = "funcs", skip_serializing_if = "StringMap::is_empty")
)]
func_imports_or_exports: StringMap<FunctionMetadata>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "StringMap::is_empty")
)]
interface_exports: StringMap<InterfaceMetadata>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "StringMap::is_empty")
)]
func_exports: StringMap<FunctionMetadata>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "StringMap::is_empty")
)]
interface_import_stability: StringMap<Stability>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "StringMap::is_empty")
)]
interface_export_stability: StringMap<Stability>,
}
impl WorldMetadata {
fn extract(resolve: &Resolve, id: WorldId) -> Self {
let world = &resolve.worlds[id];
let mut interface_imports_or_exports = StringMap::default();
let mut types = StringMap::default();
let mut func_imports_or_exports = StringMap::default();
let mut interface_exports = StringMap::default();
let mut func_exports = StringMap::default();
let mut interface_import_stability = StringMap::default();
let mut interface_export_stability = StringMap::default();
for ((key, item), import) in world
.imports
.iter()
.map(|p| (p, true))
.chain(world.exports.iter().map(|p| (p, false)))
{
match key {
WorldKey::Name(name) => match item {
WorldItem::Interface { id, .. } => {
let data = InterfaceMetadata::extract(resolve, *id);
if data.is_empty() {
continue;
}
let map = if import {
&mut interface_imports_or_exports
} else if !TRY_TO_EMIT_V0_BY_DEFAULT
|| interface_imports_or_exports.contains_key(name)
{
&mut interface_exports
} else {
&mut interface_imports_or_exports
};
let prev = map.insert(name.to_string(), data);
assert!(prev.is_none());
}
WorldItem::Type(id) => {
let data = TypeMetadata::extract(resolve, *id);
if !data.is_empty() {
types.insert(name.to_string(), data);
}
}
WorldItem::Function(f) => {
let data = FunctionMetadata::extract(f);
if data.is_empty() {
continue;
}
let map = if import {
&mut func_imports_or_exports
} else if !TRY_TO_EMIT_V0_BY_DEFAULT
|| func_imports_or_exports.contains_key(name)
{
&mut func_exports
} else {
&mut func_imports_or_exports
};
let prev = map.insert(name.to_string(), data);
assert!(prev.is_none());
}
},
WorldKey::Interface(_) => {
let stability = match item {
WorldItem::Interface { stability, .. } => stability,
_ => continue,
};
if stability.is_unknown() {
continue;
}
let map = if import {
&mut interface_import_stability
} else {
&mut interface_export_stability
};
let name = resolve.name_world_key(key);
map.insert(name, stability.clone());
}
}
}
Self {
docs: world.docs.contents.clone(),
stability: world.stability.clone(),
interface_imports_or_exports,
types,
func_imports_or_exports,
interface_exports,
func_exports,
interface_import_stability,
interface_export_stability,
}
}
fn inject(&self, resolve: &mut Resolve, id: WorldId) -> Result<()> {
for ((name, data), only_export) in self
.interface_imports_or_exports
.iter()
.map(|p| (p, false))
.chain(self.interface_exports.iter().map(|p| (p, true)))
{
let key = WorldKey::Name(name.to_string());
let world = &mut resolve.worlds[id];
let item = if only_export {
world.exports.get_mut(&key)
} else {
match world.imports.get_mut(&key) {
Some(item) => Some(item),
None => world.exports.get_mut(&key),
}
};
let Some(WorldItem::Interface { id, stability }) = item else {
bail!("missing interface {name:?}");
};
*stability = data.stability.clone();
let id = *id;
data.inject(resolve, id)?;
}
for (name, data) in &self.types {
let key = WorldKey::Name(name.to_string());
let Some(WorldItem::Type(id)) = resolve.worlds[id].imports.get(&key) else {
bail!("missing type {name:?}");
};
data.inject(resolve, *id)?;
}
let world = &resolve.worlds[id];
let stabilities = world
.imports
.iter()
.map(|i| (i, true))
.chain(world.exports.iter().map(|i| (i, false)))
.filter_map(|((key, item), import)| match item {
WorldItem::Interface { .. } => {
Some(((resolve.name_world_key(key), import), key.clone()))
}
_ => None,
})
.collect::<IndexMap<_, _>>();
let world = &mut resolve.worlds[id];
for ((name, stability), import) in self
.interface_import_stability
.iter()
.map(|p| (p, true))
.chain(self.interface_export_stability.iter().map(|p| (p, false)))
{
let key = match stabilities.get(&(name.clone(), import)) {
Some(key) => key.clone(),
None => bail!("missing interface `{name}`"),
};
let item = if import {
world.imports.get_mut(&key)
} else {
world.exports.get_mut(&key)
};
match item {
Some(WorldItem::Interface { stability: s, .. }) => *s = stability.clone(),
_ => bail!("item `{name}` wasn't an interface"),
}
}
for ((name, data), only_export) in self
.func_imports_or_exports
.iter()
.map(|p| (p, false))
.chain(self.func_exports.iter().map(|p| (p, true)))
{
let key = WorldKey::Name(name.to_string());
let item = if only_export {
world.exports.get_mut(&key)
} else {
match world.imports.get_mut(&key) {
Some(item) => Some(item),
None => world.exports.get_mut(&key),
}
};
match item {
Some(WorldItem::Function(f)) => data.inject(f)?,
_ => bail!("missing func {name:?}"),
}
}
if let Some(docs) = &self.docs {
world.docs.contents = Some(docs.to_string());
}
world.stability = self.stability.clone();
Ok(())
}
fn is_empty(&self) -> bool {
self.docs.is_none()
&& self.interface_imports_or_exports.is_empty()
&& self.types.is_empty()
&& self.func_imports_or_exports.is_empty()
&& self.stability.is_unknown()
&& self.interface_exports.is_empty()
&& self.func_exports.is_empty()
&& self.interface_import_stability.is_empty()
&& self.interface_export_stability.is_empty()
}
#[cfg(feature = "serde")]
fn is_compatible_with_v0(&self) -> bool {
self.stability.is_unknown()
&& self
.interface_imports_or_exports
.iter()
.all(|(_, w)| w.is_compatible_with_v0())
&& self
.func_imports_or_exports
.iter()
.all(|(_, w)| w.is_compatible_with_v0())
&& self.types.iter().all(|(_, w)| w.is_compatible_with_v0())
&& self.interface_exports.is_empty()
&& self.func_exports.is_empty()
&& self.interface_import_stability.is_empty()
&& self.interface_export_stability.is_empty()
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
struct InterfaceMetadata {
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
docs: Option<String>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Stability::is_unknown")
)]
stability: Stability,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "StringMap::is_empty")
)]
funcs: StringMap<FunctionMetadata>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "StringMap::is_empty")
)]
types: StringMap<TypeMetadata>,
}
impl InterfaceMetadata {
fn extract(resolve: &Resolve, id: InterfaceId) -> Self {
let interface = &resolve.interfaces[id];
let funcs = interface
.functions
.iter()
.map(|(name, func)| (name.to_string(), FunctionMetadata::extract(func)))
.filter(|(_, item)| !item.is_empty())
.collect();
let types = interface
.types
.iter()
.map(|(name, id)| (name.to_string(), TypeMetadata::extract(resolve, *id)))
.filter(|(_, item)| !item.is_empty())
.collect();
Self {
docs: interface.docs.contents.clone(),
stability: interface.stability.clone(),
funcs,
types,
}
}
fn inject(&self, resolve: &mut Resolve, id: InterfaceId) -> Result<()> {
for (name, data) in &self.types {
let Some(&id) = resolve.interfaces[id].types.get(name) else {
bail!("missing type {name:?}");
};
data.inject(resolve, id)?;
}
let interface = &mut resolve.interfaces[id];
for (name, data) in &self.funcs {
let Some(f) = interface.functions.get_mut(name) else {
bail!("missing func {name:?}");
};
data.inject(f)?;
}
if let Some(docs) = &self.docs {
interface.docs.contents = Some(docs.to_string());
}
interface.stability = self.stability.clone();
Ok(())
}
fn is_empty(&self) -> bool {
self.docs.is_none()
&& self.funcs.is_empty()
&& self.types.is_empty()
&& self.stability.is_unknown()
}
#[cfg(feature = "serde")]
fn is_compatible_with_v0(&self) -> bool {
self.stability.is_unknown()
&& self.funcs.iter().all(|(_, w)| w.is_compatible_with_v0())
&& self.types.iter().all(|(_, w)| w.is_compatible_with_v0())
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(untagged, deny_unknown_fields))]
enum FunctionMetadata {
JustDocs(Option<String>),
DocsAndStabilty {
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
docs: Option<String>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Stability::is_unknown")
)]
stability: Stability,
},
}
impl FunctionMetadata {
fn extract(func: &Function) -> Self {
if TRY_TO_EMIT_V0_BY_DEFAULT && func.stability.is_unknown() {
FunctionMetadata::JustDocs(func.docs.contents.clone())
} else {
FunctionMetadata::DocsAndStabilty {
docs: func.docs.contents.clone(),
stability: func.stability.clone(),
}
}
}
fn inject(&self, func: &mut Function) -> Result<()> {
match self {
FunctionMetadata::JustDocs(docs) => {
func.docs.contents = docs.clone();
}
FunctionMetadata::DocsAndStabilty { docs, stability } => {
func.docs.contents = docs.clone();
func.stability = stability.clone();
}
}
Ok(())
}
fn is_empty(&self) -> bool {
match self {
FunctionMetadata::JustDocs(docs) => docs.is_none(),
FunctionMetadata::DocsAndStabilty { docs, stability } => {
docs.is_none() && stability.is_unknown()
}
}
}
#[cfg(feature = "serde")]
fn is_compatible_with_v0(&self) -> bool {
match self {
FunctionMetadata::JustDocs(_) => true,
FunctionMetadata::DocsAndStabilty { .. } => false,
}
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
struct TypeMetadata {
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
docs: Option<String>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Stability::is_unknown")
)]
stability: Stability,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "StringMap::is_empty")
)]
items: StringMap<String>,
}
impl TypeMetadata {
fn extract(resolve: &Resolve, id: TypeId) -> Self {
fn extract_items<T>(items: &[T], f: impl Fn(&T) -> (&String, &Docs)) -> StringMap<String> {
items
.iter()
.flat_map(|item| {
let (name, docs) = f(item);
Some((name.to_string(), docs.contents.clone()?))
})
.collect()
}
let ty = &resolve.types[id];
let items = match &ty.kind {
TypeDefKind::Record(record) => {
extract_items(&record.fields, |item| (&item.name, &item.docs))
}
TypeDefKind::Flags(flags) => {
extract_items(&flags.flags, |item| (&item.name, &item.docs))
}
TypeDefKind::Variant(variant) => {
extract_items(&variant.cases, |item| (&item.name, &item.docs))
}
TypeDefKind::Enum(enum_) => {
extract_items(&enum_.cases, |item| (&item.name, &item.docs))
}
_ => IndexMap::default(),
};
Self {
docs: ty.docs.contents.clone(),
stability: ty.stability.clone(),
items,
}
}
fn inject(&self, resolve: &mut Resolve, id: TypeId) -> Result<()> {
let ty = &mut resolve.types[id];
if !self.items.is_empty() {
match &mut ty.kind {
TypeDefKind::Record(record) => {
self.inject_items(&mut record.fields, |item| (&item.name, &mut item.docs))?
}
TypeDefKind::Flags(flags) => {
self.inject_items(&mut flags.flags, |item| (&item.name, &mut item.docs))?
}
TypeDefKind::Variant(variant) => {
self.inject_items(&mut variant.cases, |item| (&item.name, &mut item.docs))?
}
TypeDefKind::Enum(enum_) => {
self.inject_items(&mut enum_.cases, |item| (&item.name, &mut item.docs))?
}
_ => {
bail!("got 'items' for unexpected type {ty:?}");
}
}
}
if let Some(docs) = &self.docs {
ty.docs.contents = Some(docs.to_string());
}
ty.stability = self.stability.clone();
Ok(())
}
fn inject_items<T: std::fmt::Debug>(
&self,
items: &mut [T],
f: impl Fn(&mut T) -> (&String, &mut Docs),
) -> Result<()> {
let mut unused_docs = self.items.len();
for item in items.iter_mut() {
let (name, item_docs) = f(item);
if let Some(docs) = self.items.get(name.as_str()) {
item_docs.contents = Some(docs.to_string());
unused_docs -= 1;
}
}
if unused_docs > 0 {
bail!(
"not all 'items' match type items; {item_docs:?} vs {items:?}",
item_docs = self.items
);
}
Ok(())
}
fn is_empty(&self) -> bool {
self.docs.is_none() && self.items.is_empty() && self.stability.is_unknown()
}
#[cfg(feature = "serde")]
fn is_compatible_with_v0(&self) -> bool {
self.stability.is_unknown()
}
}