首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TOML文档的链式查询

TOML文档的链式查询
EN

Stack Overflow用户
提问于 2018-01-17 17:34:22
回答 1查看 56关注 0票数 1

我正在尝试为TOML文档实现一个可链式查询执行。

Query是一种更改TOML文档并可能返回另一个Query对象的东西,该对象将在其自身之后执行。执行的Query将获得上一次查询的结果(如果有的话)。

问题是返回类型是泛型的。查询可能返回Vec<i64>,但后续查询可能返回String .因此,一个Query的返回类型直接依赖于其后续的返回类型。

到目前为止,这是我的代码:

代码语言:javascript
复制
extern crate either;
extern crate toml;

use either::Either;
use toml::Value;

type Result<T> = ::std::result::Result<T, ()>; // for simplicity

pub trait Query<Prev>
where
    Prev: Sized,
    Self: Sized,
{
    type Output: Sized;
    type Next: Query<Self::Output>;

    fn execute(&self, target: &mut Value, prev_result: Option<Prev>) -> Result<Self::Output>;

    fn next(self) -> Option<Self::Next>;
}

fn execute_query<Q, Prev>(
    doc: &mut Value,
    query: &Q,
    prev_result: Option<Prev>,
) -> Result<Either<Q::Output, <Q::Next as Query<Q::Output>>::Output>>
where
    Q: Query<Prev>,
{
    let result = query.execute(doc, prev_result)?;

    if let Some(next_query) = query.next() {
        let next_result: <Q::Next as Query<Q::Output>>::Output =
            match execute_query(doc, &next_query, Some(result)) {
                Ok(Either::Left(t)) => t,
                Ok(Either::Right(t)) => return Ok(Either::Right(t)), // error happens here
                Err(e) => return Err(e),
            };

        Ok(Either::Right(next_result))
    } else {
        Ok(Either::Left(result))
    }
}

(游乐场)

错误是返回类型是递归的(因为整个问题是递归的):

代码语言:javascript
复制
error[E0308]: mismatched types
  --> src/main.rs:37:65
   |
37 |                 Ok(Either::Right(t)) => return Ok(Either::Right(t)), // error happens here
   |                                                                 ^ expected type parameter, found associated type
   |
   = note: expected type `<<Q as Query<Prev>>::Next as Query<<Q as Query<Prev>>::Output>>::Output`
              found type `<<<Q as Query<Prev>>::Next as Query<<Q as Query<Prev>>::Output>>::Next as Query<<<Q as Query<Prev>>::Next as Query<<Q as Query<Prev>>::Output>>::Output>>::Output`

这个标题不是很有表现力。很抱歉,我不知道如何更好地描述。

EN

回答 1

Stack Overflow用户

发布于 2018-01-17 19:26:02

解决这个问题的整个方法都是错误的。我以以下方式实现了它:

  • Query特性提供了一个链接查询的函数。该函数返回一个Chain
  • Chain类型通过执行第一个元素并将结果(如果是Ok)传递给第二个查询来实现Query

使用此方法可以解决以下问题:

代码语言:javascript
复制
use std::marker::PhantomData;

use toml::Value;
use error::Result;

pub trait Query<Prev>
where
    Prev: Sized,
    Self: Sized,
{
    type Output: Sized;

    fn execute(&self, target: &mut Value, prev_result: Option<Prev>) -> Result<Self::Output>;

    fn chain<Q>(self, other: Q) -> Chain<Self, Prev, Q>
    where
        Q: Query<Self::Output>,
    {
        Chain {
            first: self,
            _p: PhantomData,
            second: other,
        }
    }
}

pub struct Chain<A, P, B>
where
    A: Query<P>,
    B: Query<A::Output>,
    P: Sized,
{
    first: A,
    _p: PhantomData<P>,
    second: B,
}

impl<A, P, B> Query<P> for Chain<A, P, B>
where
    A: Query<P>,
    B: Query<A::Output>,
    P: Sized,
{
    type Output = B::Output;

    fn execute(&self, target: &mut Value, prev_result: Option<P>) -> Result<Self::Output> {
        let p = self.first.execute(target, prev_result)?;
        self.second.execute(target, Some(p))
    }
}

pub trait QueryExecutor {
    fn query<Q, T>(&mut self, q: &Q) -> Result<Q::Output>
    where
        Q: Query<T>;
}

impl QueryExecutor for Value {
    fn query<Q, T>(&mut self, q: &Q) -> Result<Q::Output>
    where
        Q: Query<T>,
    {
        q.execute(self, None as Option<T>)
    }
}

(包括测试这里在内的完整代码)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48306882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档