KnownValue

Struct KnownValue 

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

A value in a namespace of unsigned integers that represents a stand-alone ontological concept.

Known Values provide a compact, deterministic way to represent commonly used ontological concepts such as relationships between entities, classes of entities, properties, or enumerated values. They are particularly useful as predicates in Gordian Envelope assertions, offering a more compact and deterministic alternative to URIs. However, known values are not exclusive to Gordian Envelopes and can be used in any context where a compact, unique identifier for a concept is needed.

A Known Value is represented as a 64-bit unsigned integer with an optional human-readable name. This approach ensures:

  • Compact binary representation - Each Known Value requires only 1-9 bytes depending on value range
  • Deterministic encoding - Every concept has exactly one valid binary representation
  • Enhanced security - Eliminates URI manipulation vulnerabilities
  • Standardized semantics - Values are registered in a central registry

While Known Values are most commonly used as predicates in assertions, they can appear in any position in an Envelope (subject, predicate, or object).

§Examples

use known_values::KnownValue;

// Create a Known Value with a numeric value
let known_value = KnownValue::new(42);
assert_eq!(known_value.value(), 42);

// Create a Known Value with a name
let named_value = KnownValue::new_with_name(1u64, "isA".to_string());
assert_eq!(named_value.value(), 1);
assert_eq!(named_value.name(), "isA");

// Use a pre-defined Known Value from the registry
let is_a_value = known_values::IS_A;
assert_eq!(is_a_value.value(), 1);
assert_eq!(is_a_value.name(), "isA");

§Specification

Known Values are defined in BCR-2023-002 and implemented as an Envelope extension in BCR-2023-003.

Implementations§

Source§

impl KnownValue

Source

pub fn new(value: u64) -> Self

Creates a new KnownValue with the given numeric value and no name.

§Examples
use known_values::KnownValue;

let known_value = KnownValue::new(42);
assert_eq!(known_value.value(), 42);
Source

pub fn new_with_name<T: Into<u64>>(value: T, assigned_name: String) -> Self

Creates a KnownValue with the given value and associated name.

This function accepts any type that can be converted into a u64 and a String for the name. The name is stored as a dynamic value.

§Examples
use known_values::KnownValue;

let known_value = KnownValue::new_with_name(1u64, "isA".to_string());
assert_eq!(known_value.value(), 1);
assert_eq!(known_value.name(), "isA");
Source

pub const fn new_with_static_name(value: u64, name: &'static str) -> Self

Creates a KnownValue at compile time with the given value and static name.

This function is used primarily with the const_known_value! macro to define known values as constants in the registry.

§Examples
use known_values::KnownValue;

// This is similar to how registry constants are defined
const IS_A: KnownValue = KnownValue::new_with_static_name(1, "isA");

assert_eq!(IS_A.value(), 1);
assert_eq!(IS_A.name(), "isA");
Source

pub fn value(&self) -> u64

Returns the numeric value of the KnownValue.

This is the raw 64-bit unsigned integer that identifies the concept.

§Examples
assert_eq!(known_values::IS_A.value(), 1);
assert_eq!(known_values::NOTE.value(), 4);
Source

pub fn assigned_name(&self) -> Option<&str>

Returns the assigned name of the KnownValue, if one exists.

§Examples
use known_values::KnownValue;

let named_value = KnownValue::new_with_name(1u64, "isA".to_string());
assert_eq!(named_value.assigned_name(), Some("isA"));

let unnamed_value = KnownValue::new(42);
assert_eq!(unnamed_value.assigned_name(), None);
Source

pub fn name(&self) -> String

Returns a human-readable name for the KnownValue.

If the KnownValue has an assigned name, that name is returned. Otherwise, the string representation of the numeric value is returned.

§Examples
use known_values::KnownValue;

let named_value = KnownValue::new_with_name(1u64, "isA".to_string());
assert_eq!(named_value.name(), "isA");

let unnamed_value = KnownValue::new(42);
assert_eq!(unnamed_value.name(), "42");

Trait Implementations§

Source§

impl CBORTagged for KnownValue

Specifies the CBOR tag used for KnownValue.

Source§

fn cbor_tags() -> Vec<Tag>

Returns the CBOR tags associated with this type. Read more
Source§

impl CBORTaggedDecodable for KnownValue

Creates a KnownValue from untagged CBOR.

Source§

fn from_untagged_cbor(cbor: CBOR) -> Result<Self>

Creates an instance of this type by decoding it from untagged CBOR. Read more
Source§

fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>
where Self: Sized,

Creates an instance of this type by decoding it from tagged CBOR. Read more
Source§

fn from_tagged_cbor_data(data: impl AsRef<[u8]>) -> Result<Self, Error>
where Self: Sized,

Creates an instance of this type by decoding it from binary encoded tagged CBOR. Read more
Source§

fn from_untagged_cbor_data(data: impl AsRef<[u8]>) -> Result<Self, Error>
where Self: Sized,

Creates an instance of this type by decoding it from binary encoded untagged CBOR. Read more
Source§

impl CBORTaggedEncodable for KnownValue

Provides the untagged CBOR representation of a KnownValue.

Source§

fn untagged_cbor(&self) -> CBOR

Returns the untagged CBOR encoding of this instance. Read more
Source§

fn tagged_cbor(&self) -> CBOR

Returns the tagged CBOR encoding of this instance. Read more
Source§

fn tagged_cbor_data(&self) -> Vec<u8>

Returns the tagged value in CBOR binary representation. Read more
Source§

impl Clone for KnownValue

Source§

fn clone(&self) -> KnownValue

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 KnownValue

Source§

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

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

impl DigestProvider for KnownValue

Provides a cryptographic digest for a KnownValue.

Source§

fn digest(&self) -> Digest

Returns a digest that uniquely characterizes the content of the implementing type.
Source§

impl Display for KnownValue

Formats the KnownValue for display.

If a name is assigned, the name is displayed. Otherwise, the numeric value is displayed.

Source§

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

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

impl From<KnownValue> for CBOR

Converts a KnownValue to CBOR.

Source§

fn from(value: KnownValue) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for KnownValue

Creates a KnownValue from an i32.

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for KnownValue

Creates a KnownValue from a u64.

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for KnownValue

Creates a KnownValue from a usize.

Source§

fn from(value: usize) -> Self

Converts to this type from the input type.
Source§

impl Hash for KnownValue

Hash implementation for KnownValue that considers only the numeric value.

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for KnownValue

Equality for KnownValue is based solely on the numeric value, ignoring the name.

Source§

fn eq(&self, other: &Self) -> 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 TryFrom<CBOR> for KnownValue

Attempts to convert CBOR to a KnownValue.

Source§

type Error = Error

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

fn try_from(cbor: CBOR) -> Result<Self>

Performs the conversion.
Source§

impl Eq for KnownValue

KnownValue implements Eq since equality is based on the numeric value, which can be compared for equality.

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> CBORDecodable for T
where T: TryFrom<CBOR, Error = Error>,

Source§

fn try_from_cbor(cbor: &CBOR) -> Result<Self, Error>

Source§

impl<T> CBOREncodable for T
where T: Into<CBOR> + Clone,

Source§

fn to_cbor(&self) -> CBOR

Converts this value to a CBOR object. Read more
Source§

fn to_cbor_data(&self) -> Vec<u8>

Converts this value directly to binary CBOR data. 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> Same for T

Source§

type Output = T

Should always be Self
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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> URDecodable for T

Source§

fn from_ur(ur: impl AsRef<UR>) -> Result<Self, Error>
where Self: Sized,

Source§

fn from_ur_string(ur_string: impl Into<String>) -> Result<Self, Error>
where Self: Sized,

Source§

impl<T> UREncodable for T

Source§

fn ur(&self) -> UR

Returns the UR representation of the object.
Source§

fn ur_string(&self) -> String

Returns the UR string representation of the object.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> CBORCodable for T

Source§

impl<T> CBORTaggedCodable for T

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> URCodable for T