use std::{borrow::Cow, collections::HashMap, path::Path};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Resource<'a, X: 'a>
where
[X]: ToOwned<Owned = Vec<X>>,
{
pub name: Cow<'a, str>,
pub is_python_module: bool,
pub is_python_builtin_extension_module: bool,
pub is_python_frozen_module: bool,
pub is_python_extension_module: bool,
pub is_shared_library: bool,
pub is_utf8_filename_data: bool,
pub is_python_package: bool,
pub is_python_namespace_package: bool,
pub in_memory_source: Option<Cow<'a, [X]>>,
pub in_memory_bytecode: Option<Cow<'a, [X]>>,
pub in_memory_bytecode_opt1: Option<Cow<'a, [X]>>,
pub in_memory_bytecode_opt2: Option<Cow<'a, [X]>>,
pub in_memory_extension_module_shared_library: Option<Cow<'a, [X]>>,
pub in_memory_package_resources: Option<HashMap<Cow<'a, str>, Cow<'a, [X]>>>,
pub in_memory_distribution_resources: Option<HashMap<Cow<'a, str>, Cow<'a, [X]>>>,
pub in_memory_shared_library: Option<Cow<'a, [X]>>,
pub shared_library_dependency_names: Option<Vec<Cow<'a, str>>>,
pub relative_path_module_source: Option<Cow<'a, Path>>,
pub relative_path_module_bytecode: Option<Cow<'a, Path>>,
pub relative_path_module_bytecode_opt1: Option<Cow<'a, Path>>,
pub relative_path_module_bytecode_opt2: Option<Cow<'a, Path>>,
pub relative_path_extension_module_shared_library: Option<Cow<'a, Path>>,
pub relative_path_package_resources: Option<HashMap<Cow<'a, str>, Cow<'a, Path>>>,
pub relative_path_distribution_resources: Option<HashMap<Cow<'a, str>, Cow<'a, Path>>>,
pub file_executable: bool,
pub file_data_embedded: Option<Cow<'a, [X]>>,
pub file_data_utf8_relative_path: Option<Cow<'a, str>>,
}
impl<'a, X> Default for Resource<'a, X>
where
[X]: ToOwned<Owned = Vec<X>>,
{
fn default() -> Self {
Resource {
name: Cow::Borrowed(""),
is_python_module: false,
is_python_builtin_extension_module: false,
is_python_frozen_module: false,
is_python_extension_module: false,
is_shared_library: false,
is_utf8_filename_data: false,
is_python_package: false,
is_python_namespace_package: false,
in_memory_source: None,
in_memory_bytecode: None,
in_memory_bytecode_opt1: None,
in_memory_bytecode_opt2: None,
in_memory_extension_module_shared_library: None,
in_memory_package_resources: None,
in_memory_distribution_resources: None,
in_memory_shared_library: None,
shared_library_dependency_names: None,
relative_path_module_source: None,
relative_path_module_bytecode: None,
relative_path_module_bytecode_opt1: None,
relative_path_module_bytecode_opt2: None,
relative_path_extension_module_shared_library: None,
relative_path_package_resources: None,
relative_path_distribution_resources: None,
file_executable: false,
file_data_embedded: None,
file_data_utf8_relative_path: None,
}
}
}
impl<'a, X> AsRef<Resource<'a, X>> for Resource<'a, X>
where
[X]: ToOwned<Owned = Vec<X>>,
{
fn as_ref(&self) -> &Resource<'a, X> {
self
}
}
impl<'a, X> Resource<'a, X>
where
[X]: ToOwned<Owned = Vec<X>>,
{
pub fn merge_from(&mut self, other: Resource<'a, X>) -> Result<(), &'static str> {
if self.name != other.name {
return Err("resource names must be identical to perform a merge");
}
self.is_python_module |= other.is_python_module;
self.is_python_builtin_extension_module |= other.is_python_builtin_extension_module;
self.is_python_frozen_module |= other.is_python_frozen_module;
self.is_python_extension_module |= other.is_python_extension_module;
self.is_shared_library |= other.is_shared_library;
self.is_utf8_filename_data |= other.is_utf8_filename_data;
self.is_python_package |= other.is_python_package;
self.is_python_namespace_package |= other.is_python_namespace_package;
if let Some(value) = other.in_memory_source {
self.in_memory_source.replace(value);
}
if let Some(value) = other.in_memory_bytecode {
self.in_memory_bytecode.replace(value);
}
if let Some(value) = other.in_memory_bytecode_opt1 {
self.in_memory_bytecode_opt1.replace(value);
}
if let Some(value) = other.in_memory_bytecode_opt2 {
self.in_memory_bytecode_opt2.replace(value);
}
if let Some(value) = other.in_memory_extension_module_shared_library {
self.in_memory_extension_module_shared_library
.replace(value);
}
if let Some(value) = other.in_memory_package_resources {
self.in_memory_package_resources.replace(value);
}
if let Some(value) = other.in_memory_distribution_resources {
self.in_memory_distribution_resources.replace(value);
}
if let Some(value) = other.in_memory_shared_library {
self.in_memory_shared_library.replace(value);
}
if let Some(value) = other.shared_library_dependency_names {
self.shared_library_dependency_names.replace(value);
}
if let Some(value) = other.relative_path_module_source {
self.relative_path_module_source.replace(value);
}
if let Some(value) = other.relative_path_module_bytecode {
self.relative_path_module_bytecode.replace(value);
}
if let Some(value) = other.relative_path_module_bytecode_opt1 {
self.relative_path_module_bytecode_opt1.replace(value);
}
if let Some(value) = other.relative_path_module_bytecode_opt2 {
self.relative_path_module_bytecode_opt2.replace(value);
}
if let Some(value) = other.relative_path_extension_module_shared_library {
self.relative_path_extension_module_shared_library
.replace(value);
}
if let Some(value) = other.relative_path_package_resources {
self.relative_path_package_resources.replace(value);
}
if let Some(value) = other.relative_path_distribution_resources {
self.relative_path_distribution_resources.replace(value);
}
self.file_executable |= other.file_executable;
if let Some(value) = other.file_data_embedded {
self.file_data_embedded.replace(value);
}
if let Some(value) = other.file_data_utf8_relative_path {
self.file_data_utf8_relative_path.replace(value);
}
Ok(())
}
pub fn to_owned(&self) -> Resource<'static, X> {
Resource {
name: Cow::Owned(self.name.clone().into_owned()),
is_python_module: self.is_python_module,
is_python_builtin_extension_module: self.is_python_builtin_extension_module,
is_python_frozen_module: self.is_python_frozen_module,
is_python_extension_module: self.is_python_extension_module,
is_shared_library: self.is_shared_library,
is_utf8_filename_data: self.is_utf8_filename_data,
is_python_package: self.is_python_package,
is_python_namespace_package: self.is_python_namespace_package,
in_memory_source: self
.in_memory_source
.as_ref()
.map(|value| Cow::Owned(value.clone().into_owned())),
in_memory_bytecode: self
.in_memory_bytecode
.as_ref()
.map(|value| Cow::Owned(value.clone().into_owned())),
in_memory_bytecode_opt1: self
.in_memory_bytecode_opt1
.as_ref()
.map(|value| Cow::Owned(value.clone().into_owned())),
in_memory_bytecode_opt2: self
.in_memory_bytecode_opt2
.as_ref()
.map(|value| Cow::Owned(value.clone().into_owned())),
in_memory_extension_module_shared_library: self
.in_memory_extension_module_shared_library
.as_ref()
.map(|value| Cow::Owned(value.clone().into_owned())),
in_memory_package_resources: self.in_memory_package_resources.as_ref().map(|value| {
value
.iter()
.map(|(k, v)| {
(
Cow::Owned(k.clone().into_owned()),
Cow::Owned(v.clone().into_owned()),
)
})
.collect()
}),
in_memory_distribution_resources: self.in_memory_distribution_resources.as_ref().map(
|value| {
value
.iter()
.map(|(k, v)| {
(
Cow::Owned(k.clone().into_owned()),
Cow::Owned(v.clone().into_owned()),
)
})
.collect()
},
),
in_memory_shared_library: self
.in_memory_source
.as_ref()
.map(|value| Cow::Owned(value.clone().into_owned())),
shared_library_dependency_names: self.shared_library_dependency_names.as_ref().map(
|value| {
value
.iter()
.map(|x| Cow::Owned(x.clone().into_owned()))
.collect()
},
),
relative_path_module_source: self
.relative_path_module_source
.as_ref()
.map(|value| Cow::Owned(value.clone().into_owned())),
relative_path_module_bytecode: self
.relative_path_module_bytecode
.as_ref()
.map(|value| Cow::Owned(value.clone().into_owned())),
relative_path_module_bytecode_opt1: self
.relative_path_module_bytecode_opt1
.as_ref()
.map(|value| Cow::Owned(value.clone().into_owned())),
relative_path_module_bytecode_opt2: self
.relative_path_module_bytecode_opt2
.as_ref()
.map(|value| Cow::Owned(value.clone().into_owned())),
relative_path_extension_module_shared_library: self
.relative_path_extension_module_shared_library
.as_ref()
.map(|value| Cow::Owned(value.clone().into_owned())),
relative_path_package_resources: self.relative_path_package_resources.as_ref().map(
|value| {
value
.iter()
.map(|(k, v)| {
(
Cow::Owned(k.clone().into_owned()),
Cow::Owned(v.clone().into_owned()),
)
})
.collect()
},
),
relative_path_distribution_resources: self
.relative_path_distribution_resources
.as_ref()
.map(|value| {
value
.iter()
.map(|(k, v)| {
(
Cow::Owned(k.clone().into_owned()),
Cow::Owned(v.clone().into_owned()),
)
})
.collect()
}),
file_executable: self.file_executable,
file_data_embedded: self
.file_data_embedded
.as_ref()
.map(|value| Cow::Owned(value.clone().into_owned())),
file_data_utf8_relative_path: self
.file_data_utf8_relative_path
.as_ref()
.map(|value| Cow::Owned(value.clone().into_owned())),
}
}
}