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
impl MiniON
Sourcepub fn set_content(&mut self, content: String)
pub fn set_content(&mut self, content: String)
Set the content of a MiniON
.
Sourcepub fn to_string(&self) -> String
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!"
.
Sourcepub fn parse_one(bytes: &[u8], incr: &mut usize) -> Result<MiniON, Error>
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());
}
}
Sourcepub fn parse_all(bytes: &[u8]) -> Result<Vec<MiniON>, Error>
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());
}
}
Sourcepub fn find(bytes: &[u8], name: &str) -> Result<MiniON, Error>
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());
}
}
Sourcepub fn parse_name(bytes: &[u8], incr: &mut usize) -> Result<String, Error>
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());
}
}
Sourcepub fn parse_length(
bytes: &[u8],
incr: &mut usize,
name: &str,
) -> Result<usize, Error>
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());
}
}
Sourcepub fn parse_content(
bytes: &[u8],
incr: &mut usize,
name: &str,
length: usize,
) -> Result<String, Error>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more