Cotton code
81 lines · 3.0 KB · RustRaw
//! One resolved package for sharing and for shipping refinements. No record values are read.use crate::{definitions, mcp::protocol::ToolFailure};use cotton_core::definitions::package::ToolPackage;use cotton_store::driver::Driver;use std::collections::BTreeMap;pub fn package(db: &Driver, workspace: &str, id: &str) -> Result<ToolPackage, ToolFailure> {    let catalogue = definitions::export_table_definitions_from_driver(db, workspace)?;    if !catalogue.refused.is_empty() {        return Err(ToolFailure::new("cannot export an unreadable definition"));    }    let tools = definitions::tool_definitions_from_driver(db, workspace)?;    // Every tool is a package record (72nd amendment): a tool this database    // holds no row of has nothing to export.    let tool = tools        .definitions        .into_iter()        .find(|tool| tool.id == id)        .ok_or_else(|| ToolFailure::new(format!("there is no tool {id:?}")))?;    let tables = tool        .tables        .iter()        .map(|id| {            catalogue                .definitions                .iter()                .find(|table| &table.id == id)                .cloned()                .ok_or_else(|| ToolFailure::new(format!("missing table {id:?}")))        })        .collect::<Result<Vec<_>, _>>()?;    let pictures =        crate::tool_pictures::package_pictures(db, workspace, id).map_err(ToolFailure::new)?;    use base64::Engine;    let mut pictures: BTreeMap<String, Vec<u8>> = pictures        .into_iter()        .map(|(name, bytes)| {            base64::engine::general_purpose::STANDARD                .decode(bytes)                .map(|bytes| (name, bytes))                .map_err(|error| ToolFailure::new(error.to_string()))        })        .collect::<Result<BTreeMap<_, _>, _>>()?;    if let Some(photo) = tool        .picture        .as_ref()        .and_then(|picture| match picture {            cotton_core::definitions::ToolPicture::Photo(photo) => Some(photo.photo.as_str()),            _ => None,        })        .and_then(|photo| photo.strip_prefix("photo:"))    {        if let Some(bytes) = cotton_core::definitions::pictures::shipped_bytes(photo) {            pictures                .entry(format!("{photo}.png"))                .or_insert_with(|| bytes.to_vec());        }    }    let board = crate::presentation::board(db, workspace, id)        .map_err(ToolFailure::new)?        .map(|resolved| {            cotton_core::definitions::board::BoardDefinition::strict(&resolved.definition)                .map_err(|error| ToolFailure::new(error.to_string()))        })        .transpose()?;    Ok(ToolPackage {        board,        tool,        tables,        pictures,        samples: Default::default(),    })}/// Package JSON contains no local overlay bookkeeping. Repository shipping/// uses the core definition-file writer to retain the authored file format.pub fn product_table(    table: &cotton_core::definitions::TableDefinition,) -> Result<Vec<u8>, serde_json::Error> {    cotton_core::definitions::package::product_table(table)}