Trait surf_header::utils::Str
source · pub trait Str {
// Required methods
fn cut(&self, start: i32, end: i32, step: usize) -> String;
fn format(
&self,
targets: Vec<(Box<dyn ToString>, Box<dyn ToString>)>
) -> String;
fn to_path(&self) -> Box<Path>;
fn to_pathbuf(&self) -> PathBuf;
fn parse_to_surf_header(&self) -> Result<HeaderInfo, Box<dyn Error>>;
fn split_to_vec(&self, delimiter: &str) -> Vec<String>;
}Required Methods§
sourcefn cut(&self, start: i32, end: i32, step: usize) -> String
fn cut(&self, start: i32, end: i32, step: usize) -> String
implment cut format for str and String
use surf_header::Str;
let a = "this is a demo {}";
let c = a.cut(-1,-2,1);
let f = a.format("so {}").format(vec![("{}","demo")]);
println!("{:?}",f);
println!("{:?}",c);sourcefn format(&self, targets: Vec<(Box<dyn ToString>, Box<dyn ToString>)>) -> String
fn format(&self, targets: Vec<(Box<dyn ToString>, Box<dyn ToString>)>) -> String
implment format for impl ToString
fn main() {
use surf_header::Print;
use surf_header::Str;
"this is a {s},I like Trait Object {p}%"
.format(vec![(Box::new("{s}"),Box::new("demo")),(Box::new("{p}"),Box::new(100))]).println();//this is a demo,I like Trait Object 100%
use surf_header::targets;
"this is a {d},I like Trait Object {p}}%"
.format(targets!{"{d}"=>"demo","{p}"=>100})
.println(); //this is a demo,I like Trait Object 100%
}sourcefn to_pathbuf(&self) -> PathBuf
fn to_pathbuf(&self) -> PathBuf
str String to PathBuf
sourcefn parse_to_surf_header(&self) -> Result<HeaderInfo, Box<dyn Error>>
fn parse_to_surf_header(&self) -> Result<HeaderInfo, Box<dyn Error>>
parse_to_surf_header
#[tokio::main]
async fn main() {
use surf_header::Header;
use surf_header::parse_to_surf_header;
let h = r##"
GET / HTTP/1.1
Host: crates.io
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.5249.119 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Referer: https://crates.io/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Connection: close
"##;
let info = h.parse_to_surf_header().unwrap();
let mut req = surf::get(info.url.unwrap()).build();
req.headers(info.headers);
let client = surf::Client::new();
let mut res = client.send(req).await.unwrap();
println!("{:?}",res.body_string().await.unwrap());
}sourcefn split_to_vec(&self, delimiter: &str) -> Vec<String>
fn split_to_vec(&self, delimiter: &str) -> Vec<String>
split string by delimiter into Vec