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

Introduce extractor trait and patterns #33

Open
soareschen opened this issue Dec 1, 2024 · 0 comments
Open

Introduce extractor trait and patterns #33

soareschen opened this issue Dec 1, 2024 · 0 comments

Comments

@soareschen
Copy link
Collaborator

Summary

Following #32, a context-generic extractor pattern allows context-generic providers to pattern match on a generic context without access to the concrete context.

pub trait HasExtractor {
  type Extractor;

  fn extract(self) -> Self::Extractor;
}

pub trait CanExtractField<Tag, Value> {
  type Next;

  fn try_extract(self) -> Either<Value, Self::Next>;
}

pub trait DoneExtract {
  fn done_extract<T>(self) -> T;
}

pub struct Mismatch;

impl MapType for Mismatch {
  type Map<T> = Void;
}

Example

derive(Extractor)
pub enum Foo {
  Bar(u32),
  Baz(String),
}

would have the following derived:

pub enum FooExtractor<F1: MapType, F2: MapType> {
  Bar(F1::Map<u32>),
  Baz(F2::Map<String>),
}

impl<F2: MapType> CanExtract<symbol!("Bar"), u32> for FooExtractor<Filled, F2> {
  type Next = FooExtractor<Mismatch, F2>;

  fn try_extract(self) -> Either<u32, Self::Next> {
    match self {
      Self::Bar(value) => Either::Left(value),
      Self::Baz(value) => Either::Right(FooExtractor::Baz(value)),
    }
  }
}

impl<F1: MapType> CanExtract<symbol!("Baz"), String> for FooExtractor<F2, Filled> {
  type Next = FooExtractor<F2, Mismatch>;

  fn try_extract(self) -> Either<String, Self::Next> {
    match self {
      Self::Bar(value) => Either::Right(FooExtractor::Bar(value)),
      Self::Baz(value) => Either::Left(value),
    }
  }
}

impl DoneExtract for FooExtractor<Mismatch, Mismatch> {
  fn done_extract<T>(self) -> T {
    match self {
      Self::Bar(value) => match value {},
      Self::Baz(value) => match value {},
    }
  }
}
@soareschen soareschen changed the title Introduce extract trait and patterns Introduce extractor trait and patterns Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant