Struct MiniON

Source
pub struct MiniON {
    pub name: String,
    pub length: usize,
    pub content: Option<String>,
}

Fields§

§name: String§length: usize§content: Option<String>

Implementations§

Source§

impl MiniON

Source

pub fn new(name: String) -> MiniON

Construct a new MiniON.

Source

pub fn set_content(&mut self, content: String)

Set the content of a MiniON.

Source

pub fn to_string(&self) -> String

Return the MiniON as a String.

§Example
    use minimal_object_notation::*;
 
    let mut minion = MiniON::new("greeting".to_string());
 
    minion.set_content("Hello, world!".to_string());
 
    let minion = minion.to_string();

Will give you a String containing "greeting|13~Hello, world!".

Source

pub fn parse_one(bytes: &[u8], incr: &mut usize) -> Result<MiniON, Error>

Parse data into a MiniON object.

§Example
    use minimal_object_notation::*;
 
    let data = b"greeting|13~Hello, world!";
 
    let mut incr: usize = 0;
 
    match MiniON::parse_one(data, &mut incr) {
        Ok(minion) => {
            assert_eq!("greeting",minion.name);
             
            match minion.content {
                Some(content) => {
                    assert_eq!("Hello, world!",content);
                },
                None => {
                    panic!("Expected content!");
                }
            }
        },
        Err(e) => {
            panic!("{}",e.to_string());
        }
    }
 
Source

pub fn parse_all(bytes: &[u8]) -> Result<Vec<MiniON>, Error>

Parse data that contains multiple miniON objects ONE AFTER THE OTHER. Will not parse nested miniON objects.

§Example
    use minimal_object_notation::*;
 
    let data = b"first|4~ONE,second|4~TWO,third|6~THREE,container|29~name|5~NAME,content|7~CONTENT";
 
    match MiniON::parse_all(data) {
        Ok(minions) => {
            assert_eq!(4,minions.len());
 
            assert_eq!("first",minions[0].name);
 
            assert_eq!("second",minions[1].name);
 
            assert_eq!("third",minions[2].name);
 
            assert_eq!("container",minions[3].name);
 
            match &minions[0].content {
                Some(content) => {
                    assert_eq!("ONE,",content);
                },
                None => {
                    panic!("Expected content!");
                }
            }
 
            match &minions[3].content {
                Some(content) => {
                    assert_eq!("name|5~NAME,content|7~CONTENT",content);
                },
                None => {
                    panic!("Expected content!");
                }
            }
        },
        Err(e) => {
            panic!("{}",e.to_string());
        }
    }
Source

pub fn find(bytes: &[u8], name: &str) -> Result<MiniON, Error>

Find a specific miniON object by its name.

§Example
    use minimal_object_notation::*;
 
    let data = b"one|3~ONEtwo|3~TWOthree|5~THREE";
 
    match MiniON::find(data,"two") {
        Ok(minion) => {
            assert_eq!("two",minion.name);
            assert_eq!(Some("TWO".to_string()),minion.content);
        },
        Err(e) => {
            panic!("{}",e.to_string());
        }
    }
Source

pub fn parse_name(bytes: &[u8], incr: &mut usize) -> Result<String, Error>

Parse the name of a miniON object. (Start at the correct position.)

§Example
    use minimal_object_notation::*;
 
    let data = b"greeting|13~Hello, world!";
 
    let mut incr: usize = 0;
 
    match MiniON::parse_name(data,&mut incr) {
        Ok(name) => {
            assert_eq!("greeting",name);
            assert_eq!(9,incr);
        },
        Err(e) => {
            panic!("{}",e.to_string());
        }
    }
Source

pub fn parse_length( bytes: &[u8], incr: &mut usize, name: &str, ) -> Result<usize, Error>

Parse the length of a miniON object (after having parsed the name tag).

§Example
    use minimal_object_notation::*;
 
    let data = b"greeting|13~Hello, world!";
 
    let mut incr: usize = 9;
 
    match MiniON::parse_length(data,&mut incr,"greeting") {
        Ok(length) => {
            assert_eq!(13,length);
            assert_eq!(12,incr);
        },
        Err(e) => {
            panic!("{}",e.to_string());
        }
    }
Source

pub fn parse_content( bytes: &[u8], incr: &mut usize, name: &str, length: usize, ) -> Result<String, Error>

Parse the contents of a miniON object (after having parsed the name and length tags).

§Example
    use minimal_object_notation::*;
 
    let data = b"greeting|13~Hello, world!";
 
    let mut incr: usize = 12;
 
    match MiniON::parse_content(data, &mut incr, "greeting", 13) {
        Ok(content) => {
            assert_eq!("Hello, world!",content);
            assert_eq!(incr,data.len());
        },
        Err(e) => {
            panic!("{}",e.to_string());
        }
    }
§Warning!

Should not be called when the object has a length of 0! This will result in errors!

Auto Trait Implementations§

§

impl Freeze for MiniON

§

impl RefUnwindSafe for MiniON

§

impl Send for MiniON

§

impl Sync for MiniON

§

impl Unpin for MiniON

§

impl UnwindSafe for MiniON

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> 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, 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.