I'm trying to handle the Adapter trait from the main repo.
This is a sample function in the golang version of casbin-server (grpc) -
func (s *Server) NewEnforcer(ctx context.Context, in *pb.NewEnforcerRequest) (*pb.NewEnforcerReply, error) {
var a persist.Adapter
var e *casbin.Enforcer
if in.AdapterHandle != -1 {
var err error
a, err = s.getAdapter(int(in.AdapterHandle))
if err != nil {
return &pb.NewEnforcerReply{Handler: 0}, err
}
}
if in.ModelText == "" {
cfg := LoadConfiguration("config/connection_config.json")
data, err := ioutil.ReadFile(cfg.Enforcer)
if err != nil {
return &pb.NewEnforcerReply{Handler: 0}, err
}
in.ModelText = string(data)
}
if a == nil {
m, err := model.NewModelFromString(in.ModelText)
if err != nil {
return &pb.NewEnforcerReply{Handler: 0}, err
}
e, err = casbin.NewEnforcer(m)
if err != nil {
return &pb.NewEnforcerReply{Handler: 0}, err
}
} else {
m, err := model.NewModelFromString(in.ModelText)
if err != nil {
return &pb.NewEnforcerReply{Handler: 0}, err
}
e, err = casbin.NewEnforcer(m, a)
if err != nil {
return &pb.NewEnforcerReply{Handler: 0}, err
}
}
h := s.addEnforcer(e)
return &pb.NewEnforcerReply{Handler: int32(h)}, nil
}
I'm trying to implement the same in rust like this -
async fn new_enforcer(
&self,
i: Request<casbin_proto::NewEnforcerRequest>,
) -> Result<Response<casbin_proto::NewEnforcerReply>, Status> {
let a: Option<Box<dyn Adapter>>;
let e: Enforcer;
if i.get_mut().adapter_handle != -1 {
a = match self.get_adapter(i.into_inner().adapter_handle) {
Ok(v) => Some(Box::new(v)),
Err(er) => return Ok(Response::new(casbin_proto::NewEnforcerReply { handler: 0 })),
};
}
if i.get_mut().model_text == String::from("") {
let cfg = adapter::load_configuration("config/connection_config.json").await?;
let data = match std::fs::read_to_string(cfg.enforcer.as_str()) {
Ok(v) => v,
Err(er) => return Ok(Response::new(casbin_proto::NewEnforcerReply { handler: 0 })),
};
}
match a {
None => {
let m = DefaultModel::from_str(i.get_mut().model_text.as_str())
.await
.unwrap();
let e = casbin::Enforcer::new(m, ()).await.unwrap();
}
Some(v) => {
let m = DefaultModel::from_str(i.get_mut().model_text.as_str())
.await
.unwrap();
let e = casbin::Enforcer::new(m, v).await.unwrap();
}
}
let h = self.add_enforcer(e);
Ok(Response::new(casbin_proto::NewEnforcerReply { handler: h }))
}
And these are the errors I get -
error[E0277]: the trait bound `&Box<dyn Adapter>: Adapter` is not satisfied
--> src/server/rpc_calls.rs:318:31
|
318 | Ok(v) => Some(Box::new(v)),
| ^^^^^^^^^^^ the trait `Adapter` is not implemented for `&Box<dyn Adapter>`
|
= note: required for the cast to the object type `dyn Adapter`
error[E0277]: the trait bound `Box<dyn Adapter>: Adapter` is not satisfied
--> src/server/rpc_calls.rs:340:50
|
340 | let e = casbin::Enforcer::new(m, v).await.unwrap();
| ^ the trait `Adapter` is not implemented for `Box<dyn Adapter>`
|
::: /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/casbin-2.0.9/src/core_api.rs:29:35
|
29 | async fn new<M: TryIntoModel, A: TryIntoAdapter>(
| - required by this bound in `new`
|
= note: required because of the requirements on the impl of `TryIntoAdapter` for `Box<dyn Adapter>`
Any ideas on how to go around managing these errors?
@hackerchai @hsluoyz @xcaptain @PsiACE
I'm trying to handle the
Adapter
trait from the main repo. This is a sample function in the golang version of casbin-server (grpc) -I'm trying to implement the same in rust like this -
And these are the errors I get -
Any ideas on how to go around managing these errors? @hackerchai @hsluoyz @xcaptain @PsiACE