Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling Adapter trait from casbin-rs main repo #3

Open
0xethsign opened this issue Aug 27, 2021 · 1 comment
Open

Handling Adapter trait from casbin-rs main repo #3

0xethsign opened this issue Aug 27, 2021 · 1 comment
Assignees
Labels
help wanted Extra attention is needed

Comments

@0xethsign
Copy link
Member

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

@0xethsign 0xethsign added the help wanted Extra attention is needed label Aug 28, 2021
@hsluoyz hsluoyz self-assigned this Aug 28, 2021
@hsluoyz
Copy link
Member

hsluoyz commented Aug 28, 2021

@hackerchai @PsiACE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants