pub mod parser;
pub mod v1;
use std::{fs::File, path::Path, str::FromStr};
use alpm_common::MetadataFile;
use alpm_types::{SchemaVersion, semver_version::Version};
use fluent_i18n::t;
use serde::{Deserialize, Serialize};
use crate::{Error, SourceInfoSchema, SourceInfoV1};
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum SourceInfo {
V1(SourceInfoV1),
}
impl MetadataFile<SourceInfoSchema> for SourceInfo {
type Err = Error;
fn from_file_with_schema(
file: impl AsRef<Path>,
schema: Option<SourceInfoSchema>,
) -> Result<Self, Error> {
let file = file.as_ref();
Self::from_reader_with_schema(
File::open(file).map_err(|source| Error::IoPath {
path: file.to_path_buf(),
context: t!("error-io-path-opening-file"),
source,
})?,
schema,
)
}
fn from_reader_with_schema(
mut reader: impl std::io::Read,
schema: Option<SourceInfoSchema>,
) -> Result<Self, Error> {
let mut buf = String::new();
reader
.read_to_string(&mut buf)
.map_err(|source| Error::Io {
context: t!("error-io-read-srcinfo-data"),
source,
})?;
Self::from_str_with_schema(&buf, schema)
}
fn from_str_with_schema(s: &str, schema: Option<SourceInfoSchema>) -> Result<Self, Error> {
let schema = match schema {
Some(schema) => schema,
None => SourceInfoSchema::V1(SchemaVersion::new(Version::new(1, 0, 0))),
};
match schema {
SourceInfoSchema::V1(_) => Ok(SourceInfo::V1(SourceInfoV1::from_string(s)?)),
}
}
}
impl FromStr for SourceInfo {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_str_with_schema(s, None)
}
}