Skip to main content

DotNetResourceEncoder

Struct DotNetResourceEncoder 

pub struct DotNetResourceEncoder { /* private fields */ }
Expand description

Specialized encoder for .NET resource file format.

The crate::metadata::resources::encoder::DotNetResourceEncoder creates resource files compatible with the .NET resource system, including proper magic numbers, type headers, and data serialization according to the .NET binary format specification.

§.NET Resource Format

The .NET resource format includes:

  1. Magic Number: 0xBEEFCACE to identify the format
  2. Version Information: Resource format version numbers
  3. Type Table: Names and indices of resource types used
  4. Resource Table: Names and data offsets for each resource
  5. Data Section: Actual resource data with type information

§Usage Examples

use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();

// Add various .NET resource types
encoder.add_string("WelcomeMessage", "Welcome to the application!")?;
encoder.add_int32("MaxRetries", 3)?;
encoder.add_boolean("DebugMode", true)?;
encoder.add_byte_array("ConfigData", &[1, 2, 3, 4])?;

// Generate .NET resource file
let resource_file = encoder.encode_dotnet_format()?;

§Thread Safety

This type is not Send or Sync because it maintains mutable state during resource building. Create separate instances for concurrent encoding.

Implementations§

§

impl DotNetResourceEncoder

pub fn new() -> Self

Creates a new .NET resource encoder.

Initializes an empty encoder configured for .NET resource file format generation with the current format version.

§Returns

Returns a new crate::metadata::resources::encoder::DotNetResourceEncoder instance ready for resource addition.

§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
assert_eq!(encoder.resource_count(), 0);

pub fn add_string(&mut self, name: &str, value: &str) -> Result<()>

Adds a string resource.

Registers a string value with the specified name. String resources are encoded using the .NET string serialization format.

§Arguments
  • name - Unique name for the resource
  • value - String value to store
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
encoder.add_string("ApplicationName", "My Application")?;
encoder.add_string("Version", "1.0.0")?;

pub fn add_int32(&mut self, name: &str, value: i32) -> Result<()>

Adds a 32-bit integer resource.

Registers an integer value with the specified name. Integer resources use the .NET Int32 serialization format.

§Arguments
  • name - Unique name for the resource
  • value - Integer value to store
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
encoder.add_int32("MaxConnections", 100)?;
encoder.add_int32("TimeoutSeconds", 30)?;

pub fn add_boolean(&mut self, name: &str, value: bool) -> Result<()>

Adds a boolean resource.

Registers a boolean value with the specified name. Boolean resources use the .NET Boolean serialization format.

§Arguments
  • name - Unique name for the resource
  • value - Boolean value to store
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
encoder.add_boolean("DebugMode", true)?;
encoder.add_boolean("EnableLogging", false)?;

pub fn add_byte_array(&mut self, name: &str, data: &[u8]) -> Result<()>

Adds a byte array resource.

Registers binary data as a byte array resource. Byte array resources use the .NET byte array serialization format with length prefix.

§Arguments
  • name - Unique name for the resource
  • data - Binary data to store
§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();

let config_data = vec![0x01, 0x02, 0x03, 0x04];
encoder.add_byte_array("ConfigurationData", &config_data)?;

// For file data, read the file first
// let icon_data = std::fs::read("icon.png")?;
// encoder.add_byte_array("ApplicationIcon", &icon_data)?;
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

pub fn add_stream(&mut self, name: &str, data: &[u8]) -> Result<()>

Adds a stream resource.

Registers binary data as a stream resource. Stream resources use the same storage format as byte arrays (4-byte LE length prefix + data), but are returned as a Stream by .NET’s ResourceReader instead of a byte[]. This allows for memory-mapped access and streaming reads for large resources.

§Arguments
  • name - Unique name for the resource
  • data - Binary data to store
§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();

let image_data = vec![0x89, 0x50, 0x4E, 0x47]; // PNG header
encoder.add_stream("BackgroundImage", &image_data)?;
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

pub fn add_byte(&mut self, name: &str, value: u8) -> Result<()>

Adds an unsigned 8-bit integer resource.

Registers a byte value with the specified name.

§Arguments
  • name - Unique name for the resource
  • value - Byte value to store (0-255)
§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
encoder.add_byte("MaxRetries", 5)?;
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

pub fn add_sbyte(&mut self, name: &str, value: i8) -> Result<()>

Adds a signed 8-bit integer resource.

Registers a signed byte value with the specified name.

§Arguments
  • name - Unique name for the resource
  • value - Signed byte value to store (-128 to 127)
§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
encoder.add_sbyte("TemperatureOffset", -10)?;
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

pub fn add_char(&mut self, name: &str, value: char) -> Result<()>

Adds a character resource.

Registers a Unicode character with the specified name.

§Arguments
  • name - Unique name for the resource
  • value - Character value to store
§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
encoder.add_char("Separator", ',')?;
encoder.add_char("Delimiter", '|')?;
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

pub fn add_int16(&mut self, name: &str, value: i16) -> Result<()>

Adds a signed 16-bit integer resource.

Registers a 16-bit signed integer value with the specified name.

§Arguments
  • name - Unique name for the resource
  • value - 16-bit signed integer value to store
§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
encoder.add_int16("PortNumber", 8080)?;
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

pub fn add_uint16(&mut self, name: &str, value: u16) -> Result<()>

Adds an unsigned 16-bit integer resource.

Registers a 16-bit unsigned integer value with the specified name.

§Arguments
  • name - Unique name for the resource
  • value - 16-bit unsigned integer value to store
§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
encoder.add_uint16("MaxConnections", 65535)?;
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

pub fn add_uint32(&mut self, name: &str, value: u32) -> Result<()>

Adds an unsigned 32-bit integer resource.

Registers a 32-bit unsigned integer value with the specified name.

§Arguments
  • name - Unique name for the resource
  • value - 32-bit unsigned integer value to store
§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
encoder.add_uint32("FileSize", 1024000)?;
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

pub fn add_int64(&mut self, name: &str, value: i64) -> Result<()>

Adds a signed 64-bit integer resource.

Registers a 64-bit signed integer value with the specified name.

§Arguments
  • name - Unique name for the resource
  • value - 64-bit signed integer value to store
§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
encoder.add_int64("TimestampTicks", 637500000000000000)?;
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

pub fn add_uint64(&mut self, name: &str, value: u64) -> Result<()>

Adds an unsigned 64-bit integer resource.

Registers a 64-bit unsigned integer value with the specified name.

§Arguments
  • name - Unique name for the resource
  • value - 64-bit unsigned integer value to store
§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
encoder.add_uint64("MaxFileSize", 18446744073709551615)?;
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

pub fn add_single(&mut self, name: &str, value: f32) -> Result<()>

Adds a 32-bit floating point resource.

Registers a single-precision floating point value with the specified name.

§Arguments
  • name - Unique name for the resource
  • value - 32-bit floating point value to store
§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
encoder.add_single("ScaleFactor", 1.5)?;
encoder.add_single("Pi", 3.14159)?;
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

pub fn add_double(&mut self, name: &str, value: f64) -> Result<()>

Adds a 64-bit floating point resource.

Registers a double-precision floating point value with the specified name.

§Arguments
  • name - Unique name for the resource
  • value - 64-bit floating point value to store
§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
encoder.add_double("PreciseValue", 3.14159265358979323846)?;
encoder.add_double("EulerNumber", 2.71828182845904523536)?;
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

pub fn add_decimal( &mut self, name: &str, lo: i32, mid: i32, hi: i32, flags: i32, ) -> Result<()>

Adds a .NET Decimal resource using raw bit representation.

Registers a 128-bit decimal value with the specified name using the same binary format as .NET’s System.Decimal. The four 32-bit integers represent the internal structure of a .NET decimal number.

§Arguments
  • name - Unique name for the resource
  • lo - Low 32 bits of the 96-bit mantissa
  • mid - Middle 32 bits of the 96-bit mantissa
  • hi - High 32 bits of the 96-bit mantissa
  • flags - Sign (bit 31) and scale (bits 16-23, valid range 0-28)
§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();

// Represents 3.261 (mantissa=3261, scale=3, positive)
// flags = 0x00030000 (scale 3 in bits 16-23)
encoder.add_decimal("Price", 3261, 0, 0, 0x00030000)?;

// Represents -123.45 (mantissa=12345, scale=2, negative)
// flags = 0x80020000 (sign bit + scale 2)
encoder.add_decimal("Discount", 12345, 0, 0, 0x80020000_u32 as i32)?;
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

pub fn add_datetime(&mut self, name: &str, binary_value: i64) -> Result<()>

Adds a .NET DateTime resource using binary representation.

Registers a date/time value with the specified name using the same binary format as .NET’s DateTime.ToBinary(). The 64-bit value encodes both the ticks and the DateTimeKind.

§Arguments
  • name - Unique name for the resource
  • binary_value - The binary representation from DateTime.ToBinary()
    • Bits 0-61: Ticks (100-nanosecond intervals since 01/01/0001 00:00:00)
    • Bits 62-63: DateTimeKind (0=Unspecified, 1=UTC, 2=Local)
§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();

// Ticks for 2024-01-01 00:00:00 UTC (kind=1, so bits 62-63 = 0b01)
let ticks: i64 = 638_396_736_000_000_000; // Ticks component
let kind: i64 = 1 << 62; // UTC kind
encoder.add_datetime("ReleaseDate", ticks | kind)?;
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

pub fn add_timespan(&mut self, name: &str, ticks: i64) -> Result<()>

Adds a .NET TimeSpan resource using ticks.

Registers a time interval with the specified name. The 64-bit signed value represents the number of 100-nanosecond intervals in the time span.

§Arguments
  • name - Unique name for the resource
  • ticks - Number of 100-nanosecond intervals (negative for negative spans)
§Conversion Reference

Common tick conversions:

  • 1 millisecond = 10,000 ticks
  • 1 second = 10,000,000 ticks
  • 1 minute = 600,000,000 ticks
  • 1 hour = 36,000,000,000 ticks
  • 1 day = 864,000,000,000 ticks
§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();

// 1 hour timeout
encoder.add_timespan("Timeout", 36_000_000_000)?;

// 30 seconds
encoder.add_timespan("RetryDelay", 300_000_000)?;

// Negative 5 minutes
encoder.add_timespan("TimeOffset", -3_000_000_000)?;
§Errors

Currently always returns Ok(()). Future versions may return errors for invalid resource names or encoding issues.

pub fn resource_count(&self) -> usize

Returns the number of resources in the encoder.

§Returns

The total number of resources that have been added.

§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
assert_eq!(encoder.resource_count(), 0);

encoder.add_string("test", "value")?;
assert_eq!(encoder.resource_count(), 1);

pub fn encode_dotnet_format(&self) -> Result<Vec<u8>>

Encodes all resources into .NET resource file format.

Generates a complete .NET resource file including magic number, headers, type information, and resource data according to the .NET specification.

§Returns

Returns the encoded .NET resource file as a byte vector.

§Errors

Returns crate::Error if encoding fails due to invalid resource data or serialization errors.

§Examples
use dotscope::metadata::resources::DotNetResourceEncoder;

let mut encoder = DotNetResourceEncoder::new();
encoder.add_string("AppName", "My Application")?;
encoder.add_int32("Version", 1)?;

let resource_file = encoder.encode_dotnet_format()?;

// The encoded data can be saved to a file or embedded in an assembly
assert!(!resource_file.is_empty());

Trait Implementations§

§

impl Clone for DotNetResourceEncoder

§

fn clone(&self) -> DotNetResourceEncoder

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
§

impl Debug for DotNetResourceEncoder

§

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

Formats the value using the given formatter. Read more
§

impl Default for DotNetResourceEncoder

§

fn default() -> Self

Returns the “default value” for a type. Read more

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> AsAny for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Source§

fn type_name(&self) -> &'static str

Gets the type name of self
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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Downcast for T
where T: AsAny + ?Sized,

Source§

fn is<T>(&self) -> bool
where T: AsAny,

Returns true if the boxed type is the same as T. Read more
Source§

fn downcast_ref<T>(&self) -> Option<&T>
where T: AsAny,

Forward to the method defined on the type Any.
Source§

fn downcast_mut<T>(&mut self) -> Option<&mut T>
where T: AsAny,

Forward to the method defined on the type Any.
Source§

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

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

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, A> IntoAst<A> for T
where T: Into<A>, A: Ast,

Source§

fn into_ast(self, _a: &A) -> A

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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> TryClone for T
where T: Clone,

Source§

fn try_clone(&self) -> Result<T, Error>

Clones self, possibly returning an error.
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more