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
impl Resource
Sourcepub fn new(name: impl Into<String>, unit: Unit) -> Self
👎Deprecated: Use new_with_namespace instead
pub fn new(name: impl Into<String>, unit: Unit) -> Self
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");Sourcepub fn new_with_namespace(
name: impl Into<String>,
unit: Unit,
namespace: impl Into<String>,
) -> Self
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");Sourcepub fn from_legacy_uuid(
uuid: Uuid,
name: impl Into<String>,
unit: Unit,
namespace: impl Into<String>,
) -> Self
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.
Sourcepub fn unit_symbol(&self) -> &str
pub fn unit_symbol(&self) -> &str
Returns the resource’s unit symbol (for backward compatibility).
Sourcepub fn set_attribute(&mut self, key: impl Into<String>, value: Value)
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)));Sourcepub fn get_attribute(&self, key: &str) -> Option<&Value>
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);Sourcepub fn attributes(&self) -> &HashMap<String, Value>
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<'de> Deserialize<'de> for Resource
impl<'de> Deserialize<'de> for Resource
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl StructuralPartialEq for Resource
Auto Trait Implementations§
impl Freeze for Resource
impl RefUnwindSafe for Resource
impl Send for Resource
impl Sync for Resource
impl Unpin for Resource
impl UnwindSafe for Resource
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more