use anyhow::Context;
use anyhow::Result;
use bon::Builder;
mod contents;
pub use contents::Contents;
#[derive(Clone, Debug)]
pub enum Type {
File,
Directory,
}
#[derive(Builder, Clone, Debug)]
#[builder(builder_type = Builder)]
pub struct Input {
#[builder(into)]
pub(crate) name: Option<String>,
#[builder(into)]
pub(crate) description: Option<String>,
#[builder(into)]
pub(crate) contents: Contents,
#[builder(into)]
pub(crate) path: String,
#[builder(into)]
pub(crate) ty: Type,
#[builder(default = true)]
pub(crate) read_only: bool,
}
impl Input {
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
pub fn description(&self) -> Option<&str> {
self.description.as_deref()
}
pub fn contents(&self) -> &Contents {
&self.contents
}
pub fn path(&self) -> &str {
&self.path
}
pub fn ty(&self) -> &Type {
&self.ty
}
pub fn read_only(&self) -> bool {
self.read_only
}
}
impl TryFrom<Input> for tes::v1::types::task::Input {
type Error = anyhow::Error;
fn try_from(input: Input) -> Result<Self, Self::Error> {
let Input {
name,
description,
contents,
path,
ty,
read_only: _,
} = input;
let (url, content) = contents.one_hot()?;
let ty = match ty {
Type::File => tes::v1::types::task::IoType::File,
Type::Directory => tes::v1::types::task::IoType::Directory,
};
Ok(tes::v1::types::task::Input {
name,
description,
url: url.map(|url| url.to_string()),
path,
ty,
content: content
.map(|v| String::from_utf8(v).context("TES requires file content to be UTF-8"))
.transpose()?,
streamable: None,
})
}
}