I have this function:
fn init_clients() -> (upload_executor::Executor, download_executor::Executor) {
let client_fs = if USE_CLOUD {
None
} else {
Some(Arc::new(adapter_fs::Client::new(".")))
};
let client_cloud = if USE_CLOUD {
Some(Arc::new(
adapter_s3::Client::new(
"https://s3.region.aws.com",
"region",
"bucket_name",
"KEY_ID",
"KEY_SECRET",
)
.unwrap(),
))
} else {
None
};
let client = match USE_CLOUD {
true => client_cloud
.as_ref()
.map(|o| o.clone() as Arc<dyn AdapterTrait>)
.unwrap(),
false => client_fs
.as_ref()
.map(|o| o.clone() as Arc<dyn AdapterTrait>)
.unwrap(),
};
let upload_executor = upload_executor::Executor::new(client.clone());
let download_executor = download_executor::Executor::new(client);
(upload_executor, download_executor)
}
I would like to know how to make it more readable, like:
let upload_client: TraitA + TraitB = if USE_CLOUD {
upload_client = fs::new()
} else {
upload_client = s3::new()
}
Is it possible?
If you merged the sections together you could create something like this. Also keep in mind that I haven't checked this in an IDE so I am not completely sure if I correctly converted to an anonymous trait.
fn init_s3_client() -> Arc<adapter_s3::Client> {
let client = adapter_s3::Client::new(
"https://s3.region.aws.com",
"region",
"bucket_name",
"KEY_ID",
"KEY_SECRET");
match client {
Ok(x) => Arc::new(x),
Err(err) => panic!("Failed to create s3 client: {:?}", err),
}
}
fn init_clients() -> (upload_executor::Executor, download_executor::Executor) {
let client = match USE_CLOUD {
true => init_s3_client() as Arc<dyn AdapterTrait>,
false => Arc::new(adapter_fs::Client::new(".")) as Arc<dyn AdapterTrait>,
};
let upload_executor = upload_executor::Executor::new(client.clone());
let download_executor = download_executor::Executor::new(client);
(upload_executor, download_executor)
}
Related
I send an http get request to a server and receive a response:
let resp = reqwest::blocking::get(req)?.text()?;
resp holds a String like this:
<?xml version=\"1.0\" encoding=\"UTF-8\">\n<Document xmlns=...
<datetime>202207102300</datetime>\n\t\t\t\t\t\t<value>320.08</value>\n\t\t\t\t\t<datetime>202207110000</datetime>\n\t\t\t\t\t\t<value>278.00</value>
...</Document>
What is the best way to get this text parsed into a vector containing tuple elements, as follows:
let mut tuple_items: (chrono::DateTime, f32)
This is my code that I created with the quickxml crate:
use chrono::NaiveDateTime;
use quick_xml::events::Event;
use quick_xml::Reader;
pub struct DatetimeValue {
pub dt: NaiveDateTime,
pub val: f32,
}
pub fn parse_xml_string(&self, xml_string: String) -> Vec<DatetimeValue> {
let mut response_vector: Vec<DatetimeValue> = vec![];
let mut reader = Reader::from_str(&xml_string[..]);
reader.trim_text(true);
let mut val_flag = false;
let mut dt_flag = false;
let mut buf = Vec::new();
let mut count = 0;
let mut actual_dt: NaiveDateTime;
loop {
match reader.read_event(&mut buf) {
Ok(Event::Start(ref e)) => {
if let b"value" = e.name() { val_flag = true }
else if let b"datetime" = e.name() { dt_flag = true }
}
Ok(Event::Text(e)) => {
if dt_flag {
actual_dt = NaiveDateTime::parse_from_str(e
.unescape_and_decode(&reader)
.unwrap(), "%Y%m%d%H%M").unwrap();
dt_flag = false;
}
else if val_flag {
response_vector.push(DatetimeValue {
dt: actual_dt,
val: e
.unescape_and_decode(&reader)
.unwrap()
.parse::<f32>()
.unwrap(),
});
val_flag = false;
}
}
Ok(Event::Eof) => break,
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
_ => (),
}
buf.clear();
}
response_vector
}
I'm new to Rust and am trying to wrap my head around error handling.
I'm trying to return error if parsing the date goes wrong, here is the function:
pub fn create_posts(contents: &Vec<String>) -> Result<Vec<Post>, CreatePostError> {
const TITLE_SEP: &str = "Title: ";
const DESC_SEP: &str = "Description: ";
const DATE_SEP: &str = "Date: ";
const TAGS_SEP: &str = "Tags: ";
let mut posts: Vec<Post> = Vec::new();
for entry in contents {
let lines = entry.lines().collect::<Vec<_>>();
let metadata = lines[0].contains(&TITLE_SEP)
&& lines[1].contains(&DESC_SEP)
&& lines[2].contains(&DATE_SEP)
&& lines[3].contains(&TAGS_SEP);
if metadata {
let date = &lines[2][DATE_SEP.len()..];
let parsed_date = match NaiveDate::parse_from_str(date, "%Y-%m-%d") {
Ok(parsed_date) => parsed_date,
Err(e) => eprintln!("Error: {:?}", CreatePostError::ParseError { inner_err: e }),
};
let tags: Vec<String> = lines[3][TAGS_SEP.len()..]
.split(", ")
.map(|s| s.to_string())
.collect();
let mut article_content = String::new();
for line in &lines[4..] {
article_content.push_str(line);
article_content.push_str("\n")
}
let post = Post {
title: lines[0][TITLE_SEP.len()..].to_string(),
description: lines[1][DESC_SEP.len()..].to_string(),
date: parsed_date,
tags,
content: article_content,
};
posts.push(post);
} else {
return Err(CreatePostError::MetadataError);
}
}
return Ok(posts);
}
You can see the full code here since i wrote custom errors: link
The problem I'm having is with this part:
let date = &lines[2][DATE_SEP.len()..];
let parsed_date = match NaiveDate::parse_from_str(date, "%Y-%m-%d") {
Ok(parsed_date) => parsed_date,
Err(e) => eprintln!("Error: {:?}", CreatePostError::ParseError { inner_err: e }),
};
I'm getting match arms have incompatible types. Expected struct NaiveDate, found ()
Here is my enum and impl for the error:
#[derive(Debug)]
pub enum CreatePostError {
ReadFileError { path: PathBuf },
MetadataError,
ParseError { inner_err: ParseError },
}
impl fmt::Display for CreatePostError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::ReadFileError { path } => write!(f, "Error reading file {path:?}"),
Self::MetadataError => write!(f, "Some metadata is missing"),
Self::ParseError { inner_err } => {
write!(f, "Error parsing date: {inner_err}")
}
}
}
}
impl From<chrono::format::ParseError> for CreatePostError {
fn from(e: chrono::format::ParseError) -> Self {
CreatePostError::ParseError { inner_err: e }
}
}
You probably want to have a result here, here is a way to do it:
let parsed_date = match NaiveDate::parse_from_str(date, "%Y-%m-%d") {
Ok(parsed_date) => Ok(parsed_date),
Err(e) => {eprintln!("Error: {:?}", &e);
Err(CreatePostError::ParseError { inner_err: e }}),
}?;
The ? is saying: if the thing before is an error, return it, and if it is Ok, unwrap it.
This pattern is so common that rust's result gives a lot of utilities to make this kind of things easier. Here, the map_err function would make it more straightforward: map_err
see:
let parsed_date = NaiveDate::parse_from_str(date, "%Y-%m-%d")
.map_err(|e| {
eprintln!("Error: {:?}", &e);
CreatePostError::ParseError { inner_err: e }})?;
But it is only a matter of preference and it might be a lot to digest if you are just beginning, so you can choose the way that you like the most.
I am trying to write some code that could be able to write a method that returns me a Vec with the names of the fields of a struct.
Code snippet below:
# Don't forget about dependencies if you try to reproduce this on local
use proc_macro2::{Span, Ident};
use quote::quote;
use syn::{
punctuated::Punctuated, token::Comma, Attribute, DeriveInput, Fields, Meta, NestedMeta,
Variant, Visibility,
};
#[proc_macro_derive(StructFieldNames, attributes(struct_field_names))]
pub fn derive_field_names(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ast: DeriveInput = syn::parse(input).unwrap();
let (vis, ty, generics) = (&ast.vis, &ast.ident, &ast.generics);
let names_struct_ident = Ident::new(&(ty.to_string() + "FieldStaticStr"), Span::call_site());
let fields = filter_fields(match ast.data {
syn::Data::Struct(ref s) => &s.fields,
_ => panic!("FieldNames can only be derived for structs"),
});
let names_struct_fields = fields.iter().map(|(vis, ident)| {
quote! {
#vis #ident: &'static str
}
});
let mut vec_fields: Vec<String> = Vec::new();
let names_const_fields = fields.iter().map(|(_vis, ident)| {
let ident_name = ident.to_string();
vec_fields.push(ident_name);
quote! {
#vis #ident: -
}
});
let names_const_fields_as_vec = fields.iter().map(|(_vis, ident)| {
let ident_name = ident.to_string();
// vec_fields.push(ident_name)
});
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let tokens = quote! {
#[derive(Debug)]
#vis struct #names_struct_ident {
#(#names_struct_fields),*
}
impl #impl_generics #ty #ty_generics
#where_clause
{
#vis fn get_field_names() -> &'static str {
// stringify!(
[ #(#vec_fields),* ]
.map( |s| s.to_string())
.collect()
// )
}
}
};
tokens.into()
}
fn filter_fields(fields: &Fields) -> Vec<(Visibility, Ident)> {
fields
.iter()
.filter_map(|field| {
if field
.attrs
.iter()
.find(|attr| has_skip_attr(attr, "struct_field_names"))
.is_none()
&& field.ident.is_some()
{
let field_vis = field.vis.clone();
let field_ident = field.ident.as_ref().unwrap().clone();
Some((field_vis, field_ident))
} else {
None
}
})
.collect::<Vec<_>>()
}
const ATTR_META_SKIP: &'static str = "skip";
fn has_skip_attr(attr: &Attribute, path: &'static str) -> bool {
if let Ok(Meta::List(meta_list)) = attr.parse_meta() {
if meta_list.path.is_ident(path) {
for nested_item in meta_list.nested.iter() {
if let NestedMeta::Meta(Meta::Path(path)) = nested_item {
if path.is_ident(ATTR_META_SKIP) {
return true;
}
}
}
}
}
false
}
The code it's taken from here. Basically I just want to get those values as a String, and not to access them via Foo::FIELD_NAMES.some_random_field, because I need them for another process.
How can I achieve that?
Thanks
Here is my simplified code. I need to change the pdp_state in a function. but the state remain 'a'. I don't figure out why cloning Rc does not work. I also checked this cloning out side a structure an it worked.
#[derive(Clone,Copy)]
enum PDPStatus{
a,
b
}
struct network{
pdp_state:Rc<RefCell<PDPStatus>>,
}
impl network{
fn set(&mut self){
let mut t = *self.pdp_state.clone().borrow_mut();
match t {
a => {let m1 = self.pdp_state.clone();
let mut a = (*m1).borrow_mut() ;
*a = PDPStatus::b;
println!("a");},
b=> {let m1 = self.pdp_state.clone();m1.replace( PDPStatus::a);
println!("b");},
};
}
}
fn main() {
let mut network1 = network::new();
network1.set();
network1.set();
network1.set();
network1.set();
}
Update:
My set function would look like this. I need two closure that have access to pdp_state. I pass these closures as callbacks. I am sure the these closure wouldn't call together.
fn set(&mut self){
let borrowed_pdp_status = self.pdp_state.borrow().clone();
match borrowed_pdp_status {
PDPStatus::a => {
let mut state = self.pdp_state.clone();
let mut closuree = || state = Rc::new(RefCell::new(PDPStatus::b));
let mut state1 = self.pdp_state.clone();
let mut closuree1 = || state1 = Rc::new(RefCell::new(PDPStatus::b));
closuree();
closuree1();
println!("a");
},
PDPStatus::b => {
let mut closuree = || self.pdp_state = Rc::new(RefCell::new(PDPStatus::a));
closuree();
println!("b");
},
};
}
In the set method, you need to borrow self.pdp_state and then clone() it in a variable, and then match the variable where you cloned it.
Replace the set method with this:
fn set(&mut self) {
let borrowed_pdp_status = self.pdp_state.borrow().clone();
match borrowed_pdp_status {
PDPStatus::a => {
self.pdp_state = Rc::new(RefCell::new(PDPStatus::b));
println!("a");
},
PDPStatus::b => {
self.pdp_state = Rc::new(RefCell::new(PDPStatus::a));
println!("b");
},
};
}
Playground link - https://play.rust-lang.org/?version=stable&mode=release&edition=2018&gist=426d4cb7db9a92ee4ddcd4f36dbc12f7
This answer was posted after the question was updated:
EDIT
You can use the replace() method from RefCell
fn set(&mut self) {
let borrowed_pdp_status = self.pdp_state.borrow().clone();
match borrowed_pdp_status {
PDPStatus::a => {
let mut closuree = || {
self.pdp_state.replace(PDPStatus::b);
};
let mut closuree1 = || {
self.pdp_state.replace(PDPStatus::b);
};
closuree();
closuree1();
println!("a");
}
PDPStatus::b => {
let mut closuree = || {
self.pdp_state.replace(PDPStatus::a);
};
closuree();
println!("b");
}
};
}
Playground link - https://play.rust-lang.org/?version=stable&mode=release&edition=2018&gist=4af02228d58b2f2c865a525e3f70d6a0
OLD
You could just make the closures take &mut self.pdp_state as an argument, and then update it.
fn set(&mut self) {
let borrowed_pdp_status = self.pdp_state.borrow().clone();
match borrowed_pdp_status {
PDPStatus::a => {
let mut closuree = |local_pdp_state: &mut Rc<RefCell<PDPStatus>>| {
*local_pdp_state = Rc::new(RefCell::new(PDPStatus::b))
};
let mut closuree1 = |local_pdp_state: &mut Rc<RefCell<PDPStatus>>| {
*local_pdp_state = Rc::new(RefCell::new(PDPStatus::b))
};
closuree(&mut self.pdp_state);
closuree1(&mut self.pdp_state);
println!("a");
}
PDPStatus::b => {
let mut closuree = |local_pdp_state: &mut Rc<RefCell<PDPStatus>>| {
*local_pdp_state = Rc::new(RefCell::new(PDPStatus::a))
};
closuree(&mut self.pdp_state);
println!("b");
}
};
}
Playground link - https://play.rust-lang.org/?version=stable&mode=release&edition=2018&gist=4af96385b0446082afdb7d615bb8eecb
I have this code below that is repeated in many places in my application.
The only thing that differs on the per-command basis is the if let Role { whatever } = role line
#[command]
pub async fn protect(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let target_tag: String = args.single()?;
let (user_id, guild_id) = msg.get_ids();
let (target_id, _) = LupusCtxHelper::parse_tag_to_target_id(ctx, Tag(target_tag))
.await
.ok_or(MyError)?;
let player = {
let dt = ctx.data.read().await;
dt.get_player(&guild_id, &user_id).await
};
if let Some(p) = player {
// this line is the problem
if let LupusRole::BODYGUARD { .. } = *p.current_role() {
LupusCtxHelper::send_lupus_command(ctx, msg, LupusAction::Protect(target_id)).await?
} else {
msg.channel_id
.say(&ctx.http, "check your role dude")
.await?;
}
}
Ok(())
}
How would you guys suggest I go about cleaning up this code/refactoring in an external function? It seems like I can't pattern match dynamically on the left.
ps: beware that the enum LupusRole has struct values
Example of another file:
#[command]
pub async fn frame(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let target_tag: String = args.single()?;
let (user_id, guild_id) = msg.get_ids();
let (target_id, _) = LupusCtxHelper::parse_tag_to_target_id(ctx, Tag(target_tag))
.await
.ok_or(MyError)?;
let player = {
let dt = ctx.data.read().await;
dt.get_player(&guild_id, &user_id).await
};
if let Some(p) = player {
if let LupusRole::GUFO = *p.current_role() {
LupusCtxHelper::send_lupus_command(ctx, msg, LupusAction::Frame(target_id)).await?
} else {
msg.channel_id
.say(&ctx.http, "fra... ruolo sbagliato")
.await?;
}
}
Ok(())
}