Skip to main content

Decoder

Struct Decoder 

Source
pub struct Decoder<R> {
    pub max_project_json: u64,
    pub max_asset: u64,
    pub max_depth: usize,
    pub allow_unknown_blocks: bool,
    /* private fields */
}
Expand description

Decodes a project from a ZIP archive containing a project.json entry.

The decoder supports reading from files or in-memory byte slices and limits the size of the project JSON to protect against oversized entries. Use Decoder::max_project_json to configure the limit.

§Examples

let bytes = std::fs::read("project.zip")?;
let mut decoder = Decoder::from_bytes(&bytes)?;
let project = decoder.decode()?;

Fields§

§max_project_json: u64

Maximum number of bytes allowed when reading project.json.

Defaults to 5 MiB. If the entry exceeds this limit, decoding returns an error.

§max_asset: u64

Maximum number of bytes allowed when reading an asset.

Defaults to 10 MiB. If the entry exceeds this limit, decoding returns an error.

§max_depth: usize

Maximum nesting of C-blocks and reporter inputs.

Defaults to 256. If the block nesting exceeds this limit, decoding returns an error.

§allow_unknown_blocks: bool

Decides if the decoder returns an error or outputs an crate::model::code::Action::Unknown or an crate::model::code::Reporter::Unknown when met with an unknown block opcode.

Defaults to false.

Implementations§

Source§

impl<R: Read + Seek> Decoder<R>

Source

pub fn new(reader: R) -> Result<Self, Error>

Creates a decoder from a readable and seekable ZIP archive.

The JSON size limit defaults to 5 MiB. Returns an error if the archive cannot be opened.

§Examples
let decoder = Decoder::new(Cursor::new(zip_bytes));
Source

pub fn max_project_json(self, bytes: u64) -> Self

Sets the maximum size of the project.json entry in bytes. Defaults to 5 MiB.

Source

pub fn max_asset(self, bytes: u64) -> Self

Sets the maximum size of the asset entries in bytes. Defaults to 10 MiB.

Source

pub fn max_depth(self, depth: usize) -> Self

Sets the maximum nesting of C-blocks and reporter inputs. Defaults to 256.

Source

pub fn allow_unknown_blocks(self, allow: bool) -> Self

Sets whether to allow unknown blocks. Defaults to false.

Source

pub fn decode(self) -> Result<Project, Error>

Decodes the Scratch project.

Source§

impl Decoder<BufReader<File>>

Source

pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Error>

Source§

impl<'a> Decoder<Cursor<&'a [u8]>>

Source

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

Auto Trait Implementations§

§

impl<R> Freeze for Decoder<R>
where ZipArchive<R>: Freeze,

§

impl<R> RefUnwindSafe for Decoder<R>

§

impl<R> Send for Decoder<R>
where ZipArchive<R>: Send,

§

impl<R> Sync for Decoder<R>
where ZipArchive<R>: Sync,

§

impl<R> Unpin for Decoder<R>
where ZipArchive<R>: Unpin,

§

impl<R> UnsafeUnpin for Decoder<R>

§

impl<R> UnwindSafe for Decoder<R>

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<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> 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> 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<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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.