use crate::{
traits::convert_value_container::ConvertValueContainer, utils::sheep::Sheep,
};
mod equality;
mod identity;
mod serde_dif;
mod try_clone;
use super::value::Value;
use crate::{
prelude::*,
shared_values::SharedContainer,
types::{
r#type::Type,
type_definition::TypeDefinition,
type_definition_with_metadata::{
TypeDefinitionWithMetadata, TypeMetadata,
},
},
};
pub mod borrowed_value_container;
mod apply;
mod update_handler;
use crate::{
shared_values::{
OwnedSharedContainer, ReferencedSharedContainer,
collapsed_container_value::{
CollapsedContainerValue, CollapsedContainerValueMut,
},
traits::SharedContainerCommon,
},
traits::convert_parts::FromParts,
utils::{
impl_display_for_datex_value::impl_display_for_datex_value,
sheep_mut::SheepMut,
},
values::core_values::endpoint::Endpoint,
};
use core::{
hash::{Hash, Hasher},
ops::FnOnce,
};
pub mod classification;
pub mod convert_parts;
mod convert_value_container;
mod datex_hash;
pub mod error;
pub mod get_core_lib_type_id;
pub mod get_datex_type;
pub mod method_calls;
#[cfg(feature = "ast")]
mod to_datex_expression_data;
mod to_instructions;
#[derive(Debug, Eq, Clone)]
pub enum ValueContainer {
Local(Value),
Shared(SharedContainer),
}
impl ValueContainer {
pub fn new_from_option(value: Option<ValueContainer>) -> ValueContainer {
match value {
Some(value) => value,
None => ValueContainer::Local(Value::null()),
}
}
pub fn local(value: impl Into<Value>) -> Self {
ValueContainer::Local(value.into())
}
pub fn owner(&self) -> Endpoint {
match self {
ValueContainer::Local(_value) => Endpoint::LOCAL,
ValueContainer::Shared(shared) => {
shared.pointer_address().endpoint()
}
}
}
pub fn value_container(&self) -> Sheep<'_, ValueContainer> {
match self {
ValueContainer::Local(_) => Sheep::Borrowed(self),
ValueContainer::Shared(shared) => {
Sheep::Ref(shared.value_container())
}
}
}
pub fn value_container_mut(&mut self) -> SheepMut<'_, ValueContainer> {
match self {
ValueContainer::Local(_) => SheepMut::Borrowed(self),
ValueContainer::Shared(shared) => {
SheepMut::Ref(shared.value_container_mut())
}
}
}
pub fn collapsed_value(&self) -> CollapsedContainerValue<'_> {
match self {
ValueContainer::Local(value) => {
CollapsedContainerValue::new_local(value)
}
ValueContainer::Shared(shared) => shared.collapsed_value(),
}
}
pub fn collapsed_value_mut(&mut self) -> CollapsedContainerValueMut<'_> {
match self {
ValueContainer::Local(value) => {
CollapsedContainerValueMut::new_local(value)
}
ValueContainer::Shared(shared) => shared.collapsed_value_mut(),
}
}
pub fn get_cloned_value(&self) -> Value {
let val = self.collapsed_value();
val.borrow().clone()
}
pub fn try_as<T>(&self) -> Option<&T>
where
T: ConvertValueContainer,
{
T::try_borrow_from_value_container(self).ok()
}
pub fn try_as_mut<T>(&mut self) -> Option<&mut T>
where
T: ConvertValueContainer,
{
T::try_borrow_mut_from_value_container(self).ok()
}
pub fn try_as_str(&self) -> Option<&str> {
match self {
ValueContainer::Local(value) => value.try_as_str(),
ValueContainer::Shared(_shared) => None,
}
}
pub fn try_into_value<T>(self) -> Result<T, ValueContainer>
where
T: ConvertValueContainer,
{
T::try_from_value_container(self)
}
pub fn try_convert_into_value<T>(self) -> Result<T, ()>
where
T: ConvertValueContainer + FromParts,
{
T::try_convert_from_value_container(self)
}
pub fn without_local_observers(self) -> ValueContainer {
match self {
ValueContainer::Local(value) => {
ValueContainer::Local(value.without_local_observers())
}
val => val,
}
}
pub fn get_cloned(&self) -> ValueContainer {
match self {
ValueContainer::Local(value) => {
ValueContainer::Local(value.clone())
}
ValueContainer::Shared(shared) => shared.value_container().clone(),
}
}
pub fn actual_type(&self) -> TypeDefinition {
match self {
ValueContainer::Local(local) => local.actual_type(),
ValueContainer::Shared(shared) => shared.actual_type(),
}
}
pub fn actual_container_type(&self) -> TypeDefinitionWithMetadata {
match self {
ValueContainer::Local(value) => TypeDefinitionWithMetadata::new(
value.actual_type(),
TypeMetadata::default(),
),
ValueContainer::Shared(shared) => {
let inner_type =
shared.value_container().actual_container_type();
TypeDefinitionWithMetadata::new(
TypeDefinition::Box(Box::new(Type::from(inner_type))),
TypeMetadata::Shared {
mutability: shared.container_mutability(),
ownership: shared.ownership(),
},
)
}
}
}
pub fn allowed_or_actual_type(&self) -> Sheep<'_, TypeDefinition> {
match self {
ValueContainer::Local(value) => Sheep::Owned(value.actual_type()),
ValueContainer::Shared(shared) => Sheep::Ref(shared.allowed_type()),
}
}
pub fn maybe_shared(&self) -> Option<&SharedContainer> {
if let ValueContainer::Shared(shared) = self {
Some(shared)
} else {
None
}
}
pub fn maybe_shared_mut(&mut self) -> Option<&mut SharedContainer> {
if let ValueContainer::Shared(shared) = self {
Some(shared)
} else {
None
}
}
pub fn with_maybe_shared<F, R>(&self, f: F) -> Option<R>
where
F: FnOnce(&SharedContainer) -> R,
{
if let ValueContainer::Shared(shared) = self {
Some(f(shared))
} else {
None
}
}
pub fn shared_unchecked(&self) -> &SharedContainer {
match self {
ValueContainer::Shared(shared) => shared,
_ => {
panic!("Cannot convert ValueContainer to SharedContainer")
}
}
}
pub fn shared_unchecked_mut(&mut self) -> &mut SharedContainer {
match self {
ValueContainer::Shared(shared) => shared,
_ => {
panic!("Cannot convert ValueContainer to SharedContainer")
}
}
}
pub fn is_uninitialized(&self) -> bool {
match self {
ValueContainer::Local(value) => value.is_uninitialized(),
ValueContainer::Shared(shared) => shared.is_uninitialized(),
}
}
pub fn is_null(&self) -> bool {
match self {
ValueContainer::Local(value) => value.is_null(),
ValueContainer::Shared(shared) => {
shared.value_container().is_null()
}
}
}
}
impl<T: Into<Value>> From<T> for ValueContainer {
fn from(value: T) -> Self {
ValueContainer::Local(value.into())
}
}
impl From<SharedContainer> for ValueContainer {
fn from(shared: SharedContainer) -> Self {
ValueContainer::Shared(shared)
}
}
impl From<OwnedSharedContainer> for ValueContainer {
fn from(shared: OwnedSharedContainer) -> Self {
ValueContainer::Shared(shared.into())
}
}
impl From<ReferencedSharedContainer> for ValueContainer {
fn from(shared: ReferencedSharedContainer) -> Self {
ValueContainer::Shared(shared.into())
}
}
impl Hash for ValueContainer {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
ValueContainer::Local(value) => value.hash(state),
ValueContainer::Shared(pointer) => pointer.hash(state),
}
}
}
impl_display_for_datex_value!(
ValueContainer,
impl core::fmt::Display for ValueContainer {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
ValueContainer::Local(value) => core::write!(f, "{value}"),
ValueContainer::Shared(shared) => {
if shared.is_borrowed() {
write!(f, "{}", shared.to_string_omit_content())
} else {
let value = shared.collapsed_value();
write!(f, "shared ({})", value.borrow().as_ref())
}
}
}
}
}
);