pub struct Torrent {
pub announce: Option<String>,
pub announce_list: Option<AnnounceList>,
pub length: Integer,
pub files: Option<Vec<File>>,
pub name: String,
pub piece_length: Integer,
pub pieces: Vec<Piece>,
pub extra_fields: Option<Dictionary>,
pub extra_info_fields: Option<Dictionary>,
}
Expand description
Everything found in a .torrent file.
Modeled after the specifications
in BEP 3 and
BEP 12. Unknown/extension
fields will be placed in extra_fields
(if the unknown
fields are found in the info
dictionary then they are placed in
extra_info_fields
). If you need any of those extra fields you would
have to parse it yourself.
Fields
announce: Option<String>
URL of the torrent’s tracker.
announce_list: Option<AnnounceList>
Announce list as defined in BEP 12.
length: Integer
Total torrent size in bytes (i.e. sum of all files’ sizes).
files: Option<Vec<File>>
If the torrent contains only 1 file then files
is None
.
name: String
If the torrent contains only 1 file then name
is the file name.
Otherwise it’s the suggested root directory’s name.
piece_length: Integer
Block size in bytes.
pieces: Vec<Piece>
SHA1 hashs of each block.
extra_fields: Option<Dictionary>
Top-level fields not defined in BEP 3.
extra_info_fields: Option<Dictionary>
Fields in info
not defined in BEP 3.
Implementations
sourceimpl Torrent
impl Torrent
sourcepub fn read_from_bytes<B>(bytes: B) -> Result<Torrent, LavaTorrentError> where
B: AsRef<[u8]>,
pub fn read_from_bytes<B>(bytes: B) -> Result<Torrent, LavaTorrentError> where
B: AsRef<[u8]>,
Parse bytes
and return the extracted Torrent
.
If bytes
is missing any required field (e.g. info
), or if any other
error is encountered (e.g. IOError
), then Err(error)
will be returned.
sourcepub fn read_from_file<P>(path: P) -> Result<Torrent, LavaTorrentError> where
P: AsRef<Path>,
pub fn read_from_file<P>(path: P) -> Result<Torrent, LavaTorrentError> where
P: AsRef<Path>,
Parse the content of the file at path
and return the extracted Torrent
.
If the file at path
is missing any required field (e.g. info
), or if any other
error is encountered (e.g. IOError
), then Err(error)
will be returned.
sourceimpl Torrent
impl Torrent
sourcepub fn write_into<W>(self, dst: &mut W) -> Result<(), LavaTorrentError> where
W: Write,
pub fn write_into<W>(self, dst: &mut W) -> Result<(), LavaTorrentError> where
W: Write,
Encode self
as bencode and write the result to dst
.
sourcepub fn write_into_file<P>(self, path: P) -> Result<(), LavaTorrentError> where
P: AsRef<Path>,
pub fn write_into_file<P>(self, path: P) -> Result<(), LavaTorrentError> where
P: AsRef<Path>,
Encode self
as bencode and write the result to path
.
path
must be the path to a file.
“This function will create a file if it does not exist, and will truncate it if it does.”
Note: it is the client’s responsibility to ensure
that all directories in path
actually exist (e.g.
by calling create_dir_all
).
sourceimpl Torrent
impl Torrent
sourcepub fn construct_info(&self) -> BencodeElem
pub fn construct_info(&self) -> BencodeElem
Construct the info
dict based on the fields of self
.
Certain operations on torrents, such as calculating info
hashs, require the extracted info
dict. This
convenience method does that.
Note that the info
dict
is constructed each time this method is called (i.e.
the return value is not cached). If caching is needed
then the caller should handle that.
Since self
is taken by reference, and the result is
returned by value, certain values will be cloned. Please
be aware of this overhead.
sourcepub fn info_hash(&self) -> String
pub fn info_hash(&self) -> String
Calculate the Torrent
’s info hash as defined in
BEP 3.
Note that the calculated info hash is not cached. So if this method is called multiple times, multiple calculations will be performed. To avoid that, the caller should cache the return value as needed.
sourcepub fn info_hash_bytes(&self) -> Vec<u8>
pub fn info_hash_bytes(&self) -> Vec<u8>
Calculate the Torrent
’s info hash as defined in
BEP 3.
Note that the calculated info hash is not cached. So if this method is called multiple times, multiple calculations will be performed. To avoid that, the caller should cache the return value as needed.
sourcepub fn magnet_link(&self) -> Result<String, LavaTorrentError>
pub fn magnet_link(&self) -> Result<String, LavaTorrentError>
Calculate the Torrent
’s magnet link as defined in
BEP 9.
The dn
parameter is set to self.name
.
Either self.announce
or all trackers in self.announce_list
will be used,
meaning that there might be multiple tr
entries. We don’t use both because
per BEP 12:
“If the client is compatible with the multitracker specification, and if the
announce-list
key is present, the client will ignore the announce
key
and only use the URLs in announce-list
.”
If neither self.announce
nor self.announce_list
is present, the output
won’t contain any tr
parameter.
The x.pe
parameter (for peer addresses) is currently not supported.
self.extra_fields["url-list"]
will be used to construct ws
parameters.
It must be either a string or a list of strings.
sourcepub fn is_private(&self) -> bool
pub fn is_private(&self) -> bool
Check if this torrent is private as defined in BEP 27.
Returns true
if private
maps to a bencode integer 1
.
Returns false
otherwise.
Trait Implementations
impl Eq for Torrent
impl StructuralEq for Torrent
impl StructuralPartialEq for Torrent
Auto Trait Implementations
impl RefUnwindSafe for Torrent
impl Send for Torrent
impl Sync for Torrent
impl Unpin for Torrent
impl UnwindSafe for Torrent
Blanket Implementations
sourceimpl<Src, Scheme> ApproxFrom<Src, Scheme> for Src where
Scheme: ApproxScheme,
impl<Src, Scheme> ApproxFrom<Src, Scheme> for Src where
Scheme: ApproxScheme,
sourcefn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>
fn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>
Convert the given value into an approximately equivalent representation.
sourceimpl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Src where
Dst: ApproxFrom<Src, Scheme>,
Scheme: ApproxScheme,
impl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Src where
Dst: ApproxFrom<Src, Scheme>,
Scheme: ApproxScheme,
type Err = <Dst as ApproxFrom<Src, Scheme>>::Err
type Err = <Dst as ApproxFrom<Src, Scheme>>::Err
The error type produced by a failed conversion.
sourcefn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>
fn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>
Convert the subject into an approximately equivalent representation.
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T, Dst> ConvAsUtil<Dst> for T
impl<T, Dst> ConvAsUtil<Dst> for T
sourcefn approx(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, DefaultApprox>,
fn approx(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, DefaultApprox>,
Approximate the subject with the default scheme.
sourcefn approx_by<Scheme>(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, Scheme>,
Scheme: ApproxScheme,
fn approx_by<Scheme>(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, Scheme>,
Scheme: ApproxScheme,
Approximate the subject with a specific scheme.
sourceimpl<T> ConvUtil for T
impl<T> ConvUtil for T
sourcefn approx_as<Dst>(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, DefaultApprox>,
fn approx_as<Dst>(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, DefaultApprox>,
Approximate the subject to a given type with the default scheme.
sourcefn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, Scheme>,
Scheme: ApproxScheme,
fn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err> where
Self: ApproxInto<Dst, Scheme>,
Scheme: ApproxScheme,
Approximate the subject to a given type with a specific scheme.