一个方法的多种返回类型 [英] Multiple return types from a method

查看:29
本文介绍了一个方法的多种返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 Rust 编写一个简单的电视剧集文件重命名器.

I am trying to write a simple TV-episode file renamer in Rust.

文件名被解析,可能是几种类型之一(基于日期、基于季节/剧集编号等).然后将此解析文件转换为填充文件",其中包含来自数据库的数据(然后将其格式化为新文件名)

A filename is parsed, and might be one of several types (date-based, season/episode-number based etc). This parsed file is then turned into a "populated file" with data from a database (which is then formatted into a new filename)

最初我尝试让 parse 方法接受一个文件名并返回一个枚举变量:

Initially I tried having the parse method take a filename and return an enum variant:

enum ParsedFile{
    DateBased{series: String, date: Date},
    SeasonBased{series: String, season: i32, episode: i32},
    // etc
}


fn parse(fname:&str) -> Option<ParsedFile>{
    ...
}

这很好用,但是使用 ParsedFile 并为每一集做不同的事情的方法变得混乱

This worked fine, however the methods to take the ParsedFile and do different things for each episode became messy

例如,要将 ParsedFile->PopulatedFile 翻译分离为单独的方法,我必须匹配变体,然后在方法中对其进行解构

For example, to separate the ParsedFile->PopulatedFile translation into separate methods, I have to match the variants, then destructure this in the method

struct PopulatedFile {
    seriesname: String,
    season: i32,
    episode: i32,
    episodename: String,
    airdate: Date,
}

fn _populate_seasonbased(file: ParsedFile) -> Result<PopulatedFile, TvdbError>{
    // can't just access file.season or file.episode, have to destructure enum again
}

fn populate(f: ParsedFile) -> Result<PopulatedFile, TvdbError> {
    return match f {
        ParsedFile::DateBased{..} => 
            _populate_datebased(f),
        // ...
    }
}

这看起来很笨拙,我相信一定有更好的方法.

This seemed really clunky, and I'm sure there must be a better way.

将每个剧集类型作为单独的结构会更好,例如:

It would be nicer to have each episode type as a separate struct, e.g:

struct DateBased{
    series: String,
    date: Date
}
struct SeasonBased{
    series: String,
    season: i32,
    episode: i32
}

..然后我可以为每个剧集类型实现一个 ToPopulatedFile 特征.但是我找不到在这个例子中编写 parse 方法的方法(即编写一个可能返回 DateBased 结构或 SeasonBased 的方法> 结构)

..then I could, say, implement a ToPopulatedFile trait for each episode type. However I couldn't find a way to write the parse method in this example (i.e write a method which might return a DateBased struct or a SeasonBased struct)

有什么好的方法来构造它吗?

Is there a good way to structure this?

推荐答案

您可以围绕 trait 设计一个解决方案,但这可能比简单地稍微调整您当前的解决方案需要更多的工作:

You could design a solution around trait, but it would probably be much more work than simply adapting your current solution slightly:

struct DateBased{
    series: String,
    date: Date
}

struct SeasonBased{
    series: String,
    season: i32,
    episode: i32
}

enum ParsedFile{
    Date(DateBased),
    Season(SeasonBased),
    // etc
}

fn _populate_datebased(file: DateBased) -> Result<PopulatedFile, TvdbError>;

fn _populate_seasonbased(file: SeasonBased) -> Result<PopulatedFile, TvdbError>;

fn populate(f: ParsedFile) -> Result<PopulatedFile, TvdbError> {
    return match f {
        ParsedFile::Date(d) => _populate_datebased(d),
        // ...
    }
}

你可以在 Rust 中结合 enumstruct,我个人认为给事物命名是值得的,因为它可以更容易地操作它们.

You can combine enum and struct in Rust, and I personally find it worth it to put name on things specifically because it allows manipulating them more easily.

这篇关于一个方法的多种返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆