use serde::Deserialize;
mod deps;
mod meta;
#[cfg(test)]
mod tests;
#[doc(inline)]
pub use self::{
deps::{Deps, DepInfo},
meta::Meta,
};
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Manifest {
pub meta: Meta,
#[serde(rename = "dependencies")]
#[serde(skip_serializing_if = "Option::is_none")]
pub deps: Option<Deps>,
}
impl Manifest {
pub const FILE_NAME: &'static str = "Ocean.toml";
#[cfg(feature = "toml")]
pub fn parse_toml(toml: &str) -> Result<Self, toml::de::Error> {
toml::de::from_str(toml)
}
#[cfg(feature = "toml")]
pub fn read_toml_file<T>(toml: T) -> Result<Self, std::io::Error>
where T: AsRef<std::path::Path>
{
use std::io::{Error, ErrorKind, Read};
let mut buf = String::with_capacity(128);
std::fs::File::open(toml)?.read_to_string(&mut buf)?;
Self::parse_toml(&buf).map_err(|error| {
Error::new(ErrorKind::InvalidData, error)
})
}
pub fn parse_json(json: &str) -> Result<Self, json::Error> {
json::from_str(json)
}
pub fn read_json<J>(json: J) -> Result<Self, json::Error>
where J: std::io::Read
{
json::from_reader(json)
}
pub fn read_json_file<J>(json: J) -> Result<Self, std::io::Error>
where J: AsRef<std::path::Path>
{
let reader = std::io::BufReader::new(std::fs::File::open(json)?);
Self::read_json(reader).map_err(Into::into)
}
#[cfg(feature = "toml")]
pub fn to_toml(&self, pretty: bool) -> Result<String, toml::ser::Error> {
if pretty {
toml::to_string_pretty(self)
} else {
toml::to_string(self)
}
}
pub fn to_json(&self, pretty: bool) -> Result<String, json::Error> {
if pretty {
json::to_string_pretty(self)
} else {
json::to_string(self)
}
}
pub fn files(&self) -> Vec<&str> {
let mut files = Vec::new();
let exe_path = self.meta.exe_path();
files.push(exe_path);
files
}
}