Files
heartwood/radicle-cli/src/commands/ls.rs
T
Fintan Halpenny adaed6a3ac radicle: validated construction of Project
The `validate` method of `Project` was unused, which is problematic if
the attributes are supposed to be validated based on some
constraints. The issue is that `Project` provided public construction
of its fields with enforcing validation.

This change makes `Project`'s attributes private and introduces a
smart constructor `new` which is fallible if any of the constraints
are not met. The error type was changed to be a vector of errors since
all of these errors can be collected for better error reporting.

Deserialization also provided a way of constructing a `Project`
without ensuring any of the constraints were checked. To prevent this
a hand-rolled `Deserialize` is provided that goes through
`Project::new`.

To access the fields of `Project`, accessor functions are added and
all call sites are updated to use them.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
2022-12-27 12:23:39 +01:00

65 lines
1.6 KiB
Rust

use std::ffi::OsString;
use crate::terminal as term;
use crate::terminal::args::{Args, Error, Help};
use radicle::storage::{ReadRepository, WriteStorage};
pub const HELP: Help = Help {
name: "ls",
description: "List projects",
version: env!("CARGO_PKG_VERSION"),
usage: r#"
Usage
rad ls [<option>...]
Options
--help Print help
"#,
};
pub struct Options {}
impl Args for Options {
fn from_args(args: Vec<OsString>) -> anyhow::Result<(Self, Vec<OsString>)> {
use lexopt::prelude::*;
let mut parser = lexopt::Parser::from_args(args);
if let Some(arg) = parser.next()? {
match arg {
Long("help") => {
return Err(Error::Help.into());
}
_ => return Err(anyhow::anyhow!(arg.unexpected())),
}
}
Ok((Options {}, vec![]))
}
}
pub fn run(_options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
let profile = ctx.profile()?;
let storage = &profile.storage;
let mut table = term::Table::default();
storage.projects()?.into_iter().for_each(|id| {
let Ok(repo) = storage.repository(id) else { return };
let Ok((_, head)) = repo.head() else { return };
let Ok(proj) = repo.project_of(profile.id()) else { return };
let head = term::format::oid(head);
table.push([
term::format::bold(proj.name()),
term::format::tertiary(id),
term::format::secondary(head),
term::format::italic(proj.description()),
]);
});
table.render();
Ok(())
}