How to use `Option<MyStruct>` on format? - rust

I'm trying to use an Option of a Method on format!
/// Converts a method into a `&str`.
impl<'a> From<&'a Method> for &'a str {
fn from(v: &'a Method) -> Self {
match v {
Method::Describe => "DESCRIBE",
Method::GetParameter => "GET_PARAMETER",
Method::Options => "OPTIONS",
Method::Pause => "PAUSE",
Method::Play => "PLAY",
Method::PlayNotify => "PLAY_NOTIFY",
Method::Redirect => "REDIRECT",
Method::Setup => "SETUP",
Method::SetParameter => "SET_PARAMETER",
Method::Teardown => "TEARDOWN",
Method::Extension(ref v) => v,
}
}
}
How do I use a Option<Method> in format!? I don't want to use unwrap, I want it to be empty in case there's no Method
I guess it's something like this:
let method = `Some(Method::Describe);
let method = match method {
Some(method) => method.something,
None => ""
};

I believe in this case you can use map_or_else
So what you have would looks something like this?
let method = Some(Method::Describe);
let method = method.map_or_else(|| String::default(), |m| m.something);

Related

Cleaner Match Arms for Deeply Nested Enums

I have the following function with a match expression:
fn handle_event<'e>(&mut self, event: Event<'e>) -> Event<'e> {
match (&event, &self.current_lang) {
(Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(lang))), _) => {
self.start_fenced_code_block(&lang)
}
(Event::End(Tag::CodeBlock(CodeBlockKind::Fenced(_))), _) => {
self.end_fenced_code_block()
}
(Event::Text(text), Some(lang)) => self.code_html(&text, &lang),
_ => event,
}
}
However, the first two arms felt like they were getting out of hand due to deeply nested enums. So I made some macros:
macro_rules! fenced_code_block_start {
($lang:pat_param) => {
Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced($lang)))
};
}
macro_rules! fenced_code_block_end {
() => {
Event::End(Tag::CodeBlock(CodeBlockKind::Fenced(_)))
};
}
and now I have the, IMHO, cleaner:
match (&event, &self.current_lang) {
(fenced_code_block_start!(lang), _) => self.start_fenced_code_block(&lang),
(fenced_code_block_end!(), _) => self.end_fenced_code_block(),
(Event::Text(text), Some(lang)) => self.code_html(&text, &lang),
_ => event,
}
However, I'm wondering if there's a better way to do this. Dropping down into macros always give me pause. Is there some other feature of Rust I can use here that isn't such a heavy hammer?
If clarity isn't lost, you can import and use the enum variants directly:
fn handle_event<'e>(&mut self, event: Event<'e>) -> Event<'e> {
use Event::*;
use Tag::*;
use CodeBlockKind::*;
match (&event, &self.current_lang) {
(Start(CodeBlock(Fenced(lang))), _) => self.start_fenced_code_block(&lang),
(End(CodeBlock(Fenced(_))), _) => self.end_fenced_code_block(),
(Text(text), Some(lang)) => self.code_html(&text, &lang),
_ => event,
}
}

How do I take a selected field of the last element of the slice or return an error? [duplicate]

I have something like this (the real function is Ini::Section::get from rust-ini):
impl Foo {
pub fn get<K>(&'a mut self, key: &K) -> Option<&'a str>
where
K: Hash + Eq,
{
// ...
}
}
I have to call it several times:
fn new() -> Result<Boo, String> {
let item1 = match section.get("item1") {
None => return Result::Err("no item1".to_string()),
Some(v) => v,
};
let item2 = match section.get("item2") {
None => return Result::Err("no item2".to_string()),
Some(v) => v,
};
}
To remove code bloat, I can write a macro like this:
macro_rules! try_ini_get {
($e:expr) => {
match $e {
Some(s) => Ok(s),
None => Err("no ini item".to_string()),
}
}
}
Is there any way to remove the code duplication without this macro implementation?
The ok_or and ok_or_else methods convert Options to Results, and the ? operator automates the boilerplate associated with early Err returns.
You could do something like:
fn new() -> Result<Boo, String> {
let item1 = section.get("item1").ok_or("no item1")?;
let item2 = section.get("item2").ok_or("no item2")?;
// whatever processing...
Ok(final_result)
}
If you're using the crate anyhow you can import the anyhow::Context trait which adds the .context method on Options to turn them into anyhow::Results:
use anyhow::{Result, Context};
fn new() -> Result<Boo> {
let item1 = section.get("item1").context("no item1")?;
let item2 = section.get("item2").context("no item2")?;
// whatever processing...
Ok(final_result)
}

How can I get the T from an Option<T> when using syn?

I'm using syn to parse Rust code. When I read a named field's type using field.ty, I get a syn::Type. When I print it using quote!{#ty}.to_string() I get "Option<String>".
How can I get just "String"? I want to use #ty in quote! to print "String" instead of "Option<String>".
I want to generate code like:
impl Foo {
pub set_bar(&mut self, v: String) {
self.bar = Some(v);
}
}
starting from
struct Foo {
bar: Option<String>
}
My attempt:
let ast: DeriveInput = parse_macro_input!(input as DeriveInput);
let data: Data = ast.data;
match data {
Data::Struct(ref data) => match data.fields {
Fields::Named(ref fields) => {
fields.named.iter().for_each(|field| {
let name = &field.ident.clone().unwrap();
let ty = &field.ty;
quote!{
impl Foo {
pub set_bar(&mut self, v: #ty) {
self.bar = Some(v);
}
}
};
});
}
_ => {}
},
_ => panic!("You can derive it only from struct"),
}
My updated version of the response from #Boiethios, tested and used in a public crate, with support of several syntaxes for Option:
Option
std::option::Option
::std::option::Option
core::option::Option
::core::option::Option
fn extract_type_from_option(ty: &syn::Type) -> Option<&syn::Type> {
use syn::{GenericArgument, Path, PathArguments, PathSegment};
fn extract_type_path(ty: &syn::Type) -> Option<&Path> {
match *ty {
syn::Type::Path(ref typepath) if typepath.qself.is_none() => Some(&typepath.path),
_ => None,
}
}
// TODO store (with lazy static) the vec of string
// TODO maybe optimization, reverse the order of segments
fn extract_option_segment(path: &Path) -> Option<&PathSegment> {
let idents_of_path = path
.segments
.iter()
.into_iter()
.fold(String::new(), |mut acc, v| {
acc.push_str(&v.ident.to_string());
acc.push('|');
acc
});
vec!["Option|", "std|option|Option|", "core|option|Option|"]
.into_iter()
.find(|s| &idents_of_path == *s)
.and_then(|_| path.segments.last())
}
extract_type_path(ty)
.and_then(|path| extract_option_segment(path))
.and_then(|path_seg| {
let type_params = &path_seg.arguments;
// It should have only on angle-bracketed param ("<String>"):
match *type_params {
PathArguments::AngleBracketed(ref params) => params.args.first(),
_ => None,
}
})
.and_then(|generic_arg| match *generic_arg {
GenericArgument::Type(ref ty) => Some(ty),
_ => None,
})
}
You should do something like this untested example:
use syn::{GenericArgument, PathArguments, Type};
fn extract_type_from_option(ty: &Type) -> Type {
fn path_is_option(path: &Path) -> bool {
leading_colon.is_none()
&& path.segments.len() == 1
&& path.segments.iter().next().unwrap().ident == "Option"
}
match ty {
Type::Path(typepath) if typepath.qself.is_none() && path_is_option(typepath.path) => {
// Get the first segment of the path (there is only one, in fact: "Option"):
let type_params = typepath.path.segments.iter().first().unwrap().arguments;
// It should have only on angle-bracketed param ("<String>"):
let generic_arg = match type_params {
PathArguments::AngleBracketed(params) => params.args.iter().first().unwrap(),
_ => panic!("TODO: error handling"),
};
// This argument must be a type:
match generic_arg {
GenericArgument::Type(ty) => ty,
_ => panic!("TODO: error handling"),
}
}
_ => panic!("TODO: error handling"),
}
}
There's not many things to explain, it just "unrolls" the diverse components of a type:
Type -> TypePath -> Path -> PathSegment -> PathArguments -> AngleBracketedGenericArguments -> GenericArgument -> Type.
If there is an easier way to do that, I would be happy to know it.
Note that since syn is a parser, it works with tokens. You cannot know for sure that this is an Option. The user could, for example, type std::option::Option, or write type MaybeString = std::option::Option<String>;. You cannot handle those arbitrary names.

Is there a way to simplify converting an Option into a Result without a macro?

I have something like this (the real function is Ini::Section::get from rust-ini):
impl Foo {
pub fn get<K>(&'a mut self, key: &K) -> Option<&'a str>
where
K: Hash + Eq,
{
// ...
}
}
I have to call it several times:
fn new() -> Result<Boo, String> {
let item1 = match section.get("item1") {
None => return Result::Err("no item1".to_string()),
Some(v) => v,
};
let item2 = match section.get("item2") {
None => return Result::Err("no item2".to_string()),
Some(v) => v,
};
}
To remove code bloat, I can write a macro like this:
macro_rules! try_ini_get {
($e:expr) => {
match $e {
Some(s) => Ok(s),
None => Err("no ini item".to_string()),
}
}
}
Is there any way to remove the code duplication without this macro implementation?
The ok_or and ok_or_else methods convert Options to Results, and the ? operator automates the boilerplate associated with early Err returns.
You could do something like:
fn new() -> Result<Boo, String> {
let item1 = section.get("item1").ok_or("no item1")?;
let item2 = section.get("item2").ok_or("no item2")?;
// whatever processing...
Ok(final_result)
}
If you're using the crate anyhow you can import the anyhow::Context trait which adds the .context method on Options to turn them into anyhow::Results:
use anyhow::{Result, Context};
fn new() -> Result<Boo> {
let item1 = section.get("item1").context("no item1")?;
let item2 = section.get("item2").context("no item2")?;
// whatever processing...
Ok(final_result)
}

How to implement iter method using static/dynamic dispatch?

I need to implement the method iter, which returns something which implements the trait Iterator<Item = char>. But the return value will be different implementations, depending on the enum variant.
Something like this:
pub enum Class {
SingleChar(char),
Range(Range),
And(Vec<Class>),
Or(Vec<Class>),
}
impl Class {
pub fn iter(&self) -> Iterator<Item = char> {
match *self {
Class::SingleChar(c) => vec![c],
Class::Range(ref range) => range.iter(),
Class::And(ref classes) => {
let iter: Option<_> = classes.iter().fold(None, |iter, &class| {
match iter {
None => Some(class.iter()),
Some(iter) => Some(iter.merge(class.iter())),
}
});
Box::new(iter.unwrap())
},
Class::Or(ref classes) => {
let iter: Option<_> = classes.iter().fold(None, |iter, &class| {
match iter {
None => Some(class.iter()),
Some(iter) => Some(iter.interleave(class.iter())),
}
});
Box::new(iter.unwrap())
},
}
}
}
range.iter() returns a struct that implements Iterator<Item=char>.
merge and interleave are itertools methods, which return MergeAscend and Interleave respectively (both of them implement Iterator<Item=char>)
How to implement such a scheme using static dispatch?
If static dispatch is not possible, how to implement such a scheme using dynamic dispatch?
It is not possible to do it using static dispatch. There is a tracking RFC issue on unboxed abstract return types, but Rust is not there yet (and I'm not sure if it could cover the use case of returning different types). Therefore, dynamic dispatch is the way to go.
You're pretty close, actually. Just make the return type Box<Iterator<Item=char>> and add more boxing:
pub fn iter(&self) -> Box<Iterator<Item=char>> {
match *self {
Class::SingleChar(c) => Box::new(Some(c).into_iter()),
Class::Range(ref range) => Box::new(range.iter()),
Class::And(ref classes) => {
let iter: Option<_> = classes.iter().fold(None, |iter, &class| {
match iter {
None => Some(Box::new(class.iter())),
Some(iter) => Some(Box::new(iter.merge(class.iter()))),
}
});
iter.unwrap()
},
Class::Or(ref classes) => {
let iter: Option<_> = classes.iter().fold(None, |iter, &class| {
match iter {
None => Some(Box::new(class.iter())),
Some(iter) => Some(Box::new(iter.interleave(class.iter()))),
}
});
iter.unwrap()
},
}
}
This should work.

Resources