Metainfo

Struct Metainfo 

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

Owned, validated metainfo inspection object.

Input bytes are copied so this value does not borrow its source. The exact original bytes remain available, while Metainfo::to_bytes and Metainfo::write_canonical emit a canonical re-encoding.

§Examples

use btpc_core::{Metainfo, TorrentMode};

let bytes = b"d4:infod6:lengthi0e4:name5:empty12:piece lengthi16e6:pieces0:ee";
let torrent = Metainfo::from_bytes(bytes)?;
assert_eq!(torrent.mode(), TorrentMode::V1);
assert_eq!(torrent.name_utf8(), Some("empty"));
assert_eq!(torrent.original_bytes(), bytes);

Implementations§

Source§

impl Metainfo

Source

pub fn magnet(&self, options: &MagnetOptions) -> String

Generates a deterministic magnet URI.

Source

pub fn from_bytes(bytes: &[u8]) -> Result<Self>

Parses and validates metainfo from bytes, copying the input.

§Errors

Returns a bencode, resource-limit, or protocol validation error.

Source

pub fn from_bytes_with_options( bytes: &[u8], options: ParseOptions, ) -> Result<Self>

Parses and validates metainfo using caller-provided limits.

§Errors

Returns a bencode, resource-limit, or protocol validation error.

Source

pub fn from_vec(bytes: Vec<u8>) -> Result<Self>

Parses and validates owned metainfo bytes without copying the input buffer.

§Errors

Returns a bencode, resource-limit, or protocol validation error.

Source

pub fn from_vec_with_options( bytes: Vec<u8>, options: ParseOptions, ) -> Result<Self>

Parses and validates owned metainfo bytes using caller-provided limits.

The returned object retains the supplied buffer as its exact original representation, including the raw bencoded info slice used for hashes.

§Errors

Returns a bencode, resource-limit, or protocol validation error.

Source

pub fn from_path(path: impl AsRef<Path>) -> Result<Self>

Reads, parses, and validates metainfo from a filesystem path.

§Errors

Returns a contextual I/O error or any error from Metainfo::from_bytes.

Source

pub fn from_path_with_options( path: impl AsRef<Path>, options: ParseOptions, ) -> Result<Self>

Reads, parses, and validates metainfo using caller-provided limits.

Regular files are checked before allocation. The actual read is capped at one byte beyond the configured maximum so growth races and streams are still rejected without unbounded allocation.

§Errors

Returns a contextual I/O, resource-limit, bencode, or validation error.

Source

pub const fn parse_options(&self) -> ParseOptions

Returns the resource limits used to load this object.

Source

pub const fn mode(&self) -> TorrentMode

Returns the validated protocol mode.

Source

pub fn name(&self) -> &[u8] ⓘ

Returns the raw torrent name bytes.

Source

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

Returns the torrent name as UTF-8 when valid.

Source

pub fn files(&self) -> &[TorrentFile]

Returns payload files.

Source

pub const fn piece_length(&self) -> u64

Returns the piece length in bytes.

Source

pub const fn total_length(&self) -> u64

Returns total payload bytes.

Source

pub const fn piece_count(&self) -> u64

Returns the logical piece count for the active representation.

Source

pub fn trackers(&self) -> &[Vec<Vec<u8>>]

Returns tracker tiers as raw byte strings.

Source

pub fn web_seeds(&self) -> &[Vec<u8>]

Returns web seed URLs as raw byte strings.

Source

pub const fn optional_metadata(&self) -> &OptionalMetadata

Returns all validated optional metadata in one lossless owned model.

Source

pub fn nodes(&self) -> &[DhtNode]

Returns validated DHT bootstrap nodes.

Source

pub const fn private(&self) -> Option<bool>

Returns the private flag when explicitly present.

Source

pub fn source(&self) -> Option<&[u8]>

Returns raw source bytes when explicitly present in the info dictionary.

Source

pub fn comment(&self) -> Option<&[u8]>

Returns raw top-level comment bytes when present.

Source

pub fn created_by(&self) -> Option<&[u8]>

Returns raw top-level creator bytes when present.

Source

pub const fn creation_date(&self) -> Option<i64>

Returns the top-level creation timestamp when representable as i64.

Source

pub const fn info_hash_v1(&self) -> Option<InfoHashV1>

Returns the v1 info hash when applicable.

Source

pub const fn info_hash_v2(&self) -> Option<InfoHashV2>

Returns the v2 info hash when applicable.

Source

pub fn original_bytes(&self) -> &[u8] ⓘ

Returns exact input bytes.

Source

pub fn unknown_fields(&self) -> &[UnknownField]

Returns unknown top-level extension fields.

Source

pub fn unknown_field_bytes(&self, field: &UnknownField) -> &[u8] ⓘ

Returns the exact source bytes for an unknown top-level field.

Source

pub const fn validate(&self) -> &ValidationReport

Returns the successful validation report retained at construction.

Source

pub fn write_original(&self, writer: &mut impl Write) -> Result<()>

Writes exact original input bytes.

§Errors

Returns an I/O error preserving the writer’s error as its source.

Source

pub fn write_canonical(&self, writer: &mut impl Write) -> Result<()>

Writes canonical bencode for this unchanged parsed object.

§Errors

Returns an I/O error preserving the writer’s error as its source.

Source

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

Returns canonical bencode bytes.

§Errors

This unchanged owned snapshot is already validated, so this method does not currently fail; the result type preserves serialization API stability.

Trait Implementations§

Source§

impl Clone for Metainfo

Source§

fn clone(&self) -> Metainfo

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 Metainfo

Source§

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

Formats the value using the given formatter. 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> 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> 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, 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.