dev: update route model (#1154)

* dev: update route model

* build: update cargo.lock
This commit is contained in:
Myriad-Dreamin 2025-01-11 18:09:36 +08:00 committed by GitHub
parent c0d8f0db1c
commit 0e2874a6c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 64 additions and 18 deletions

View file

@ -17,7 +17,7 @@ anyhow.workspace = true
clap.workspace = true
log.workspace = true
pathdiff.workspace = true
reflexo.workspace = true
reflexo-typst.workspace = true
semver.workspace = true
same-file.workspace = true
serde.workspace = true

View file

@ -17,7 +17,7 @@ use std::{
use anyhow::{bail, Context};
use clap::{ValueEnum, ValueHint};
use reflexo::path::unix_slash;
use reflexo_typst::{path::unix_slash, typst::diag::EcoString};
pub use anyhow::Result;
@ -25,6 +25,8 @@ const LOCKFILE_PATH: &str = "tinymist.lock";
const LOCK_VERSION: &str = "0.1.0-beta0";
pub const PROJECT_ROUTE_USER_ACTION_PRIORITY: u32 = 256;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case", tag = "version")]
pub enum LockFileCompat {
@ -94,6 +96,13 @@ impl LockFile {
}
}
pub fn replace_route(&mut self, route: ProjectRoute) {
let id = route.id.clone();
self.route.retain(|i| i.id != id);
self.route.push(route);
}
pub fn sort(&mut self) {
self.document.sort_by(|a, b| a.id.cmp(&b.id));
self.task
@ -128,15 +137,6 @@ impl LockFile {
}
}
let task = content.get("task");
if let Some(task) = task {
for task in task.as_array().unwrap() {
out.push('\n');
out.push_str("[[task]]\n");
emit_output(task, &mut out);
}
}
let route = content.get("route");
if let Some(route) = route {
for route in route.as_array().unwrap() {
@ -146,6 +146,15 @@ impl LockFile {
}
}
let task = content.get("task");
if let Some(task) = task {
for task in task.as_array().unwrap() {
out.push('\n');
out.push_str("[[task]]\n");
emit_output(task, &mut out);
}
}
return out;
fn emit_document(input: &toml::Value, out: &mut String) {
@ -224,6 +233,11 @@ impl LockFile {
return Ok(());
}
// todo: even if cargo, they don't update the lock file atomically. This
// indicates that we may get data corruption if the process is killed
// while writing the lock file. This is sensible because `Cargo.lock` is
// only a "resolved result" of the `Cargo.toml`. Thus, we should inform
// users that don't only persist configuration in the lock file.
lock_file.file().set_len(0)?;
lock_file.seek(SeekFrom::Start(0))?;
lock_file.write_all(new_data.as_bytes())?;
@ -245,6 +259,12 @@ impl Id {
}
}
impl fmt::Display for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
/// The id of a document.
///
/// If an identifier is not provided, the document's path is used as the id.
@ -275,8 +295,8 @@ impl From<&DocIdArgs> for Id {
}
/// A resource path.
#[derive(Debug, Clone)]
pub struct ResourcePath(String, String);
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ResourcePath(EcoString, String);
impl fmt::Display for ResourcePath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -294,7 +314,7 @@ impl FromStr for ResourcePath {
if parts.next().is_some() {
Err("too many colons")
} else {
Ok(ResourcePath(scheme.to_string(), path.to_string()))
Ok(ResourcePath(scheme.into(), path.to_string()))
}
}
}
@ -327,7 +347,21 @@ impl ResourcePath {
pathdiff::diff_paths(inp, &cwd).unwrap()
};
let rel = unix_slash(&rel);
ResourcePath("file".to_string(), rel.to_string())
ResourcePath("file".into(), rel.to_string())
}
pub fn from_file_id(id: reflexo_typst::typst::TypstFileId) -> Self {
let package = id.package();
match package {
Some(package) => ResourcePath(
"file_id".into(),
format!("{package}{}", unix_slash(id.vpath().as_rooted_path())),
),
None => ResourcePath(
"file_id".into(),
format!("$root{}", unix_slash(id.vpath().as_rooted_path())),
),
}
}
}
@ -502,7 +536,19 @@ pub struct ExportTextTask {
}
/// A project route specifier.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ProjectMaterial {
/// The root of the project that the material belongs to.
pub root: EcoString,
/// A project.
pub id: Id,
/// The files.
pub files: Vec<ResourcePath>,
}
/// A project route specifier.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ProjectRoute {
/// A project.

View file

@ -182,7 +182,7 @@ pub struct DocConfigureArgs {
pub id: DocIdArgs,
/// Set the unsigned priority of these task (lower numbers are higher
/// priority).
#[clap(long = "priority", default_value_t = 0)]
#[clap(long = "priority", default_value_t = PROJECT_ROUTE_USER_ACTION_PRIORITY)]
pub priority: u32,
}