Skip to content

Commit

Permalink
Merge pull request #14 from dvdsk/dev
Browse files Browse the repository at this point in the history
adresses #13 and #12, joining spans no longer chrashes
  • Loading branch information
dvdsk authored Sep 1, 2022
2 parents 87a597f + 5bdfef7 commit 3ae2651
Show file tree
Hide file tree
Showing 21 changed files with 139 additions and 71 deletions.
17 changes: 12 additions & 5 deletions dbstruct-derive/src/ir/accessor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use proc_macro2::Span;
use quote::format_ident;
use syn::spanned::Spanned;
use syn::{parse_quote, parse_quote_spanned};
Expand Down Expand Up @@ -29,16 +30,20 @@ impl Accessor {
let body = parse_quote!({
dbstruct::wrappers::Map::new(self.ds.clone(), #key)
});
let span = key_ty.span().join(val_ty.span()).unwrap();
let returns = parse_quote_spanned!(span=> dbstruct::wrappers::Map<#key_ty, #val_ty, #ds>);
// Using proc_macro2 version until
// https://github.com/rust-lang/rust/issues/54725 stabalizes
let span = key_ty.span().join(val_ty.span()).unwrap_or(Span::call_site());
let returns =
parse_quote_spanned!(span=> dbstruct::wrappers::Map<#key_ty, #val_ty, #ds>);
(body, returns)
}
#[allow(unused_variables)]
Wrapper::DefaultTrait { ty } => {
let body = parse_quote!({
dbstruct::wrappers::DefaultTrait::new(self.ds.clone(), #key)
});
let returns = parse_quote_spanned!(ty.span()=> dbstruct::wrappers::DefaultTrait<#ty, #ds>);
let returns =
parse_quote_spanned!(ty.span()=> dbstruct::wrappers::DefaultTrait<#ty, #ds>);
(body, returns)
}
#[allow(unused_variables)]
Expand All @@ -47,15 +52,17 @@ impl Accessor {
let default_value = #value;
dbstruct::wrappers::DefaultValue::new(self.ds.clone(), #key, default_value)
});
let returns = parse_quote_spanned!(ty.span()=> dbstruct::wrappers::DefaultValue<#ty, #ds>);
let returns =
parse_quote_spanned!(ty.span()=> dbstruct::wrappers::DefaultValue<#ty, #ds>);
(body, returns)
}
#[allow(unused_variables)]
Wrapper::Option { ty } => {
let body = parse_quote!({
dbstruct::wrappers::OptionValue::new(self.ds.clone(), #key)
});
let returns = parse_quote_spanned!(ty.span()=> dbstruct::wrappers::OptionValue<#ty, #ds>);
let returns =
parse_quote_spanned!(ty.span()=> dbstruct::wrappers::OptionValue<#ty, #ds>);
(body, returns)
}
};
Expand Down
2 changes: 1 addition & 1 deletion dbstruct-derive/src/ir/new_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl NewMethod {
.len_vars
.iter()
.map(|def| def.ident.clone())
.map(Option::unwrap)
.map(|ident| ident.expect("ident is None"))
.map(as_len_value)
.collect();

Expand Down
15 changes: 0 additions & 15 deletions dbstruct-derive/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,6 @@ mod tests {
use super::*;
use syn::parse_str;

#[test]
fn failt2() {
// todo figure out why not fail? (unit test time)
let input_attr = proc_macro2::TokenStream::from_str("db=sled").unwrap();
let input_struct: syn::ItemStruct = parse_str(
"struct PersistentData {
    mode_cur_playlist: HashMap<AudioMode, String>,
    playlist_positions: HashMap<String, Position>,
    playlist_last_played: HashMap<String, u64>,
    current_mode: Option<AudioMode>,
}",
).unwrap();
let _model = Model::try_from(input_struct, input_attr).unwrap();
}

#[test]
fn analyze_model_does_not_crash() {
let input_attr = proc_macro2::TokenStream::from_str("db=sled").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion dbstruct-derive/src/model/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Backend {
(None, _) => return Err(NoBackendSpecified.with_span(Span::call_site())),
(Some(b), None) => *b,
(Some(b0), Some(b1)) => {
let span = b0.span.join(b1.span).unwrap();
let span = b0.span.join(b1.span).unwrap_or(b1.span);
return Err(MultipleBackends.with_span(span));
}
};
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub enum Error<DbError: fmt::Debug> {
Database(#[from] DbError),
}

// test the readme example as part of the doctests
#[doc = include_str!("../Readme.md")]
#[cfg(doctest)]
pub struct ReadmeDoctests;
10 changes: 8 additions & 2 deletions src/stores/hashmap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections;
use std::sync::{RwLock, Arc};
use std::sync::{Arc, RwLock};

use crate::traits::ByteStore;

Expand All @@ -9,8 +9,14 @@ pub enum Error {
Poisoned,
}

/// This is a very simple backend that offers no persistance but also needs no path argument. It only
/// supports some wrappers. Use it for testing.
///
/// ### ALL CHANGES ARE LOST WHEN THE OBJECT IS DROPPED
/// again: use for testing the api only
///
#[derive(Default, Clone)]
pub struct HashMap(Arc<RwLock<collections::HashMap<Vec<u8>, Vec<u8>>>>);
pub struct HashMap(Arc<RwLock<collections::HashMap<Vec<u8>, Vec<u8>>>>);

impl HashMap {
pub fn new() -> Self {
Expand Down
2 changes: 0 additions & 2 deletions tests/persistance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::path::Path;

#[dbstruct::dbstruct(db=sled)]
pub struct Test {
#[dbstruct(Default)]
Expand Down
20 changes: 0 additions & 20 deletions tests/simple_impl.rs
Original file line number Diff line number Diff line change
@@ -1,20 +0,0 @@
// pub struct Test<DS: dbstruct::DataStore + std::clone::Clone> {
// ds: DS,
// }
//
// impl<DS> Test<DS>
// where
// DS: dbstruct::DataStore + std::clone::Clone,
// {
// pub fn new(ds: DS) -> Result<Self, dbstruct::Error<DS::Error>> {
// Ok(Self {
// ds,
// ds: std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0)),
// })
// }
// fn the_field(&self) -> dbstruct::wrappers::DefaultTrait<u8, DS> {
// dbstruct::wrappers::DefaultTrait::new(self.ds.clone(), 0u8)
// }
// }
//
// fn main() {}
5 changes: 3 additions & 2 deletions tests/ui/attribute_does_not_exist.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
error: incorrect syntax for backend

= help: a backend should be a single world not enclosed in "

--> tests/ui/attribute_does_not_exist.rs:3:14
|
3 | #[dbstruct(db+)]
| ^
|
= help: a backend should be a single world not enclosed in "
7 changes: 4 additions & 3 deletions tests/ui/db_misses_traits.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
error: The database backend (hashmap) you specified can not support all the structs fields

= help: You need a backend that implements all of these traits: {Ordered}.
Database backends that implement those traits: sled

--> tests/ui/db_misses_traits.rs:3:15
|
3 | #[dbstruct(db=hashmap)]
| ^^^^^^^
|
= help: You need a backend that implements all of these traits: {Ordered}.
Database backends that implement those traits: sled
5 changes: 3 additions & 2 deletions tests/ui/db_option_misses_value.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
error: backend option has no value set

= help: try setting a supported backend, for example `db=sled`

--> tests/ui/db_option_misses_value.rs:3:14
|
3 | #[dbstruct(db=)]
| ^
|
= help: try setting a supported backend, for example `db=sled`
4 changes: 3 additions & 1 deletion tests/ui/db_option_missing.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error: No database backend specified to use as backend

= help: specify a backend using #[dbstruct(db=sled)]

--> tests/ui/db_option_missing.rs:3:1
|
3 | #[dbstruct]
| ^^^^^^^^^^^
|
= help: specify a backend using #[dbstruct(db=sled)]
= note: this error originates in the attribute macro `dbstruct` (in Nightly builds, run with -Z macro-backtrace for more info)
5 changes: 3 additions & 2 deletions tests/ui/invalid_attribute_syntax.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
error: invalid syntax for dbstruct option

= help: the option should be a single word not enclosed in "

--> tests/ui/invalid_attribute_syntax.rs:3:12
|
3 | #[dbstruct("db"=sled)]
| ^^^^
|
= help: the option should be a single word not enclosed in "
5 changes: 3 additions & 2 deletions tests/ui/invalid_db_value.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
error: incorrect syntax for backend

= help: a backend should be a single world not enclosed in "

--> tests/ui/invalid_db_value.rs:3:15
|
3 | #[dbstruct(db="sled")]
| ^^^^^^
|
= help: a backend should be a single world not enclosed in "
13 changes: 13 additions & 0 deletions tests/ui/map_missing_serialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

enum CustomKeyType {}
struct CustomValType;

fn main() {
// ONCE the output changes and the span starts highlighting the
// key and value type in the struct we can close: https://github.com/dvdsk/dbstruct/issues/13

#[dbstruct::dbstruct(db=sled)]
struct PersistentData {
the_map: HashMap<CustomKeyType, CustomValType>,
}
}
69 changes: 69 additions & 0 deletions tests/ui/map_missing_serialize.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
error[E0277]: the trait bound `CustomKeyType: serde::ser::Serialize` is not satisfied
--> tests/ui/map_missing_serialize.rs:9:5
|
9 | #[dbstruct::dbstruct(db=sled)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `serde::ser::Serialize` is not implemented for `CustomKeyType`
|
= help: the following other types implement trait `serde::ser::Serialize`:
&'a T
&'a mut T
()
(T0, T1)
(T0, T1, T2)
(T0, T1, T2, T3)
(T0, T1, T2, T3, T4)
(T0, T1, T2, T3, T4, T5)
and 126 others
note: required by a bound in `dbstruct::wrappers::Map`
--> src/wrappers/map.rs
|
| Key: Serialize,
| ^^^^^^^^^ required by this bound in `dbstruct::wrappers::Map`
= note: this error originates in the attribute macro `dbstruct::dbstruct` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `CustomValType: serde::ser::Serialize` is not satisfied
--> tests/ui/map_missing_serialize.rs:9:5
|
9 | #[dbstruct::dbstruct(db=sled)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `serde::ser::Serialize` is not implemented for `CustomValType`
|
= help: the following other types implement trait `serde::ser::Serialize`:
&'a T
&'a mut T
()
(T0, T1)
(T0, T1, T2)
(T0, T1, T2, T3)
(T0, T1, T2, T3, T4)
(T0, T1, T2, T3, T4, T5)
and 126 others
note: required by a bound in `dbstruct::wrappers::Map`
--> src/wrappers/map.rs
|
| Value: Serialize + DeserializeOwned,
| ^^^^^^^^^ required by this bound in `dbstruct::wrappers::Map`
= note: this error originates in the attribute macro `dbstruct::dbstruct` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `for<'de> CustomValType: serde::de::Deserialize<'de>` is not satisfied
--> tests/ui/map_missing_serialize.rs:9:5
|
9 | #[dbstruct::dbstruct(db=sled)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `for<'de> serde::de::Deserialize<'de>` is not implemented for `CustomValType`
|
= help: the following other types implement trait `serde::de::Deserialize<'de>`:
&'a Path
&'a [u8]
&'a str
()
(T0, T1)
(T0, T1, T2)
(T0, T1, T2, T3)
(T0, T1, T2, T3, T4)
and 134 others
= note: required because of the requirements on the impl of `serde::de::DeserializeOwned` for `CustomValType`
note: required by a bound in `dbstruct::wrappers::Map`
--> src/wrappers/map.rs
|
| Value: Serialize + DeserializeOwned,
| ^^^^^^^^^^^^^^^^ required by this bound in `dbstruct::wrappers::Map`
= note: this error originates in the attribute macro `dbstruct::dbstruct` (in Nightly builds, run with -Z macro-backtrace for more info)
5 changes: 3 additions & 2 deletions tests/ui/missing_db_option.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
error: no database backend specified

= help: try specifying an db, for example: `db=sled`

--> tests/ui/missing_db_option.rs:3:12
|
3 | #[dbstruct(db)]
| ^^
|
= help: try specifying an db, for example: `db=sled`
9 changes: 5 additions & 4 deletions tests/ui/multiple_backends.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
error: multiple database backends specified
--> tests/ui/multiple_backends.rs:3:15

= help: remove one of the backends

--> tests/ui/multiple_backends.rs:3:24
|
3 | #[dbstruct(db=sled, db=trait)]
| ^^^^^^^^^^^^^^
|
= help: remove one of the backends
| ^^^^^
5 changes: 3 additions & 2 deletions tests/ui/not_a_db_backend.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
error: Not a known database backend: `starship_voyager`

= help: try sled as database backend

--> tests/ui/not_a_db_backend.rs:3:15
|
3 | #[dbstruct(db=starship_voyager)]
| ^^^^^^^^^^^^^^^^
|
= help: try sled as database backend
4 changes: 2 additions & 2 deletions tests/ui/trait_is_missing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ error[E0277]: the trait bound `for<'de> CustomType: serde::de::Deserialize<'de>`
(T0, T1, T2)
(T0, T1, T2, T3)
(T0, T1, T2, T3, T4)
and 125 others
= note: required for `CustomType` to implement `serde::de::DeserializeOwned`
and 134 others
= note: required because of the requirements on the impl of `serde::de::DeserializeOwned` for `CustomType`
note: required by a bound in `DefaultTrait`
--> src/wrappers/default_trait.rs
|
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/unknown_option.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
error: not a known dbstruct option

= help: the only supported option currently is: db

--> tests/ui/unknown_option.rs:3:12
|
3 | #[dbstruct(starship=enterprise)]
| ^^^^^^^^
|
= help: the only supported option currently is: db

0 comments on commit 3ae2651

Please sign in to comment.