Resource

Struct Resource 

Source
pub struct Resource { /* private fields */ }
Expand description

Represents a quantifiable subject of value in enterprise models.

Resources are the “WHAT” - things that flow between entities, measured in specific units (units, kg, USD, etc.)

§Examples

Basic usage:

use sea_core::primitives::Resource;
use sea_core::units::{Unit, Dimension};
use rust_decimal::Decimal;

let units = Unit::new("units", "units", Dimension::Count, Decimal::from(1), "units");
let product = Resource::new_with_namespace("Camera", units, "default".to_string());
assert_eq!(product.name(), "Camera");
assert_eq!(product.unit().symbol(), "units");
assert_eq!(product.namespace(), "default");

With namespace:

use sea_core::primitives::Resource;
use sea_core::units::{Unit, Dimension};
use rust_decimal::Decimal;

let currency = Unit::new("currency", "currency", Dimension::Currency, Decimal::from(1), "currency");
let usd = Resource::new_with_namespace("USD", currency, "finance");
assert_eq!(usd.namespace(), "finance");

With custom attributes:

use sea_core::primitives::Resource;
use sea_core::units::{Unit, Dimension};
use rust_decimal::Decimal;
use serde_json::json;

let kg = Unit::new("kg", "kilogram", Dimension::Mass, Decimal::from(1), "kg");
let mut gold = Resource::new_with_namespace("Gold", kg, "default".to_string());
gold.set_attribute("purity", json!(0.999));
gold.set_attribute("origin", json!("South Africa"));

assert_eq!(gold.get_attribute("purity"), Some(&json!(0.999)));

Serialization:

use sea_core::primitives::Resource;
use sea_core::units::{Unit, Dimension};
use rust_decimal::Decimal;

let oz = Unit::new("oz", "ounce", Dimension::Mass, Decimal::new(28349523, 9), "oz");
let resource = Resource::new_with_namespace("Silver", oz, "default".to_string());
let json = serde_json::to_string(&resource).unwrap();
let deserialized: Resource = serde_json::from_str(&json).unwrap();
assert_eq!(resource.name(), deserialized.name());
assert_eq!(resource.unit().symbol(), deserialized.unit().symbol());

Implementations§

Source§

impl Resource

Source

pub fn new(name: impl Into<String>, unit: Unit) -> Self

👎Deprecated: Use new_with_namespace instead

Creates a new Resource (deprecated - use new_with_namespace).

§Examples
use sea_core::primitives::Resource;
use sea_core::units::{Unit, Dimension};
use rust_decimal::Decimal;

let kg = Unit::new("kg", "kilogram", Dimension::Mass, Decimal::from(1), "kg");
let resource = Resource::new_with_namespace("Camera", kg, "default".to_string());
assert_eq!(resource.name(), "Camera");
assert_eq!(resource.unit().symbol(), "kg");
Source

pub fn new_with_namespace( name: impl Into<String>, unit: Unit, namespace: impl Into<String>, ) -> Self

Creates a new Resource with a specific namespace.

§Examples
use sea_core::primitives::Resource;
use sea_core::units::{Unit, Dimension};
use rust_decimal::Decimal;

let usd = Unit::new("USD", "US Dollar", Dimension::Currency, Decimal::from(1), "USD");
let resource = Resource::new_with_namespace("USD", usd, "finance");
assert_eq!(resource.namespace(), "finance");
Source

pub fn from_legacy_uuid( uuid: Uuid, name: impl Into<String>, unit: Unit, namespace: impl Into<String>, ) -> Self

Creates a Resource from a legacy UUID for backward compatibility.

Source

pub fn id(&self) -> &ConceptId

Returns the resource’s unique identifier.

Source

pub fn name(&self) -> &str

Returns the resource’s name.

Source

pub fn unit(&self) -> &Unit

Returns the resource’s unit of measurement.

Source

pub fn unit_symbol(&self) -> &str

Returns the resource’s unit symbol (for backward compatibility).

Source

pub fn namespace(&self) -> &str

Returns the resource’s namespace.

Source

pub fn set_attribute(&mut self, key: impl Into<String>, value: Value)

Sets a custom attribute.

§Examples
use sea_core::primitives::Resource;
use sea_core::units::unit_from_string;
use serde_json::json;

let mut resource = Resource::new_with_namespace("Gold", unit_from_string("kg"), "default".to_string());
resource.set_attribute("purity", json!(0.999));
assert_eq!(resource.get_attribute("purity"), Some(&json!(0.999)));
Source

pub fn get_attribute(&self, key: &str) -> Option<&Value>

Gets a custom attribute.

Returns None if the attribute doesn’t exist.

§Examples
use sea_core::primitives::Resource;
use sea_core::units::unit_from_string;
use serde_json::json;

let mut resource = Resource::new_with_namespace("Gold", unit_from_string("kg"), "default".to_string());
resource.set_attribute("purity", json!(0.999));
assert_eq!(resource.get_attribute("purity"), Some(&json!(0.999)));
assert_eq!(resource.get_attribute("missing"), None);
Source

pub fn attributes(&self) -> &HashMap<String, Value>

Returns all attributes as a reference.

§Examples
use sea_core::primitives::Resource;
use sea_core::units::unit_from_string;
use serde_json::json;

let mut resource = Resource::new_with_namespace("Gold", unit_from_string("kg"), "default".to_string());
resource.set_attribute("purity", json!(0.999));
resource.set_attribute("origin", json!("South Africa"));

let attrs = resource.attributes();
assert_eq!(attrs.len(), 2);
assert_eq!(attrs.get("purity"), Some(&json!(0.999)));

Trait Implementations§

Source§

impl Clone for Resource

Source§

fn clone(&self) -> Resource

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Resource

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Resource

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Resource

Source§

fn eq(&self, other: &Resource) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Resource

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Resource

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,