Skip to content

Commit

Permalink
chore: upgrade dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Cubxity committed Jul 21, 2023
1 parent 299d993 commit 7fecd0f
Show file tree
Hide file tree
Showing 11 changed files with 606 additions and 448 deletions.
725 changes: 433 additions & 292 deletions src-tauri/Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ once_cell = "1.17"
elsa = "1.8"
hex = "0.4"
base64 = "0.21"
notify = "5.1"
notify = "6.0"
arboard = "3.2"
chrono = "0.4"
png = "0.17"
log = "0.4"
env_logger = "0.10"
dirs = "5.0"
walkdir = "2.3"
memmap2 = "0.5"
memmap2 = "0.7"

typst = { git = "https://github.com/typst/typst" }
typst-library = { git = "https://github.com/typst/typst" }
typst = { git = "https://github.com/typst/typst", tag = "v0.6.0" }
typst-library = { git = "https://github.com/typst/typst", tag = "v0.6.0" }
comemo = "0.3"

[features]
Expand Down
5 changes: 2 additions & 3 deletions src-tauri/src/engine/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use once_cell::sync::OnceCell;
use std::fs::File;
use std::path::{Path, PathBuf};
use typst::font::{Font, FontBook, FontInfo};
use typst::util::Buffer;
use typst::util::Bytes;
use walkdir::WalkDir;

// Taken from typst-cli
Expand Down Expand Up @@ -48,8 +48,7 @@ impl FontSearcher {
#[cfg(feature = "embed-fonts")]
fn search_embedded(&mut self) {
let mut search = |bytes: &'static [u8]| {
let buffer = Buffer::from_static(bytes);
for (i, font) in Font::iter(buffer).enumerate() {
for (i, font) in Font::iter(Bytes::from_static(bytes)).enumerate() {
self.book.push(font.info().clone());
self.fonts.push(FontSlot {
path: PathBuf::new(),
Expand Down
6 changes: 3 additions & 3 deletions src-tauri/src/ipc/commands/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ pub async fn fs_write_file_text<R: Runtime>(
path: PathBuf,
content: String,
) -> Result<()> {
let (project, path) = project_path(&window, &project_manager, path)?;
let _ = File::create(&path)
let (project, absolute_path) = project_path(&window, &project_manager, &path)?;
let _ = File::create(&absolute_path)
.map(|mut f| f.write_all(content.as_bytes()))
.map_err(Into::<Error>::into)?;

let mut world = project.world.lock().unwrap();
let _ = world
.slot_update(path.as_path(), Some(content))
.slot_update(&path, Some(content))
.map_err(Into::<Error>::into)?;

Ok(())
Expand Down
28 changes: 16 additions & 12 deletions src-tauri/src/ipc/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ::typst::diag::FileError;
use ::typst::util::PathExt;
use serde::{Serialize, Serializer};
use std::io;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tauri::{Runtime, State, Window};

Expand Down Expand Up @@ -40,25 +40,29 @@ impl Serialize for Error {

pub type Result<T> = std::result::Result<T, Error>;

pub fn project<R: Runtime>(
window: &Window<R>,
project_manager: &State<Arc<ProjectManager<R>>>,
) -> Result<Arc<Project>> {
project_manager
.get_project(&window)
.ok_or(Error::UnknownProject)
}

/// Retrieves the project and resolves the path. Furthermore,
/// this function will resolve the path relative to project's root
/// and checks whether the path belongs to the project root.
pub fn project_path<R: Runtime>(
pub fn project_path<R: Runtime, P: AsRef<Path>>(
window: &Window<R>,
project_manager: &State<Arc<ProjectManager<R>>>,
path: PathBuf,
path: P,
) -> Result<(Arc<Project>, PathBuf)> {
let project = project_manager
.get_project(&window)
.ok_or(Error::UnknownProject)?;
let rel_path = project.root.join(path);

// This will resolve symlinks and reject resolved files outside the project's root
let path = rel_path
.canonicalize()
.unwrap_or_else(|_| rel_path.normalize());
if !path.starts_with(&project.root) {
return Err(Error::UnrelatedPath);
}
let path = project
.root
.join_rooted(path.as_ref())
.ok_or(Error::UnrelatedPath)?;
Ok((project, path))
}
53 changes: 26 additions & 27 deletions src-tauri/src/ipc/commands/typst.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{Error, Result};
use crate::ipc::commands::project_path;
use crate::ipc::commands::{project, project_path};
use crate::ipc::model::TypstRenderResponse;
use crate::ipc::{TypstCompileEvent, TypstDocument, TypstSourceError};
use crate::project::ProjectManager;
Expand All @@ -15,7 +15,6 @@ use std::time::Instant;
use tauri::Runtime;
use typst::geom::Color;
use typst::ide::{Completion, CompletionKind};
use typst::syntax::ErrorPos;
use typst::World;

#[derive(Serialize_repr, Debug)]
Expand Down Expand Up @@ -66,11 +65,11 @@ pub async fn typst_compile<R: Runtime>(
path: PathBuf,
content: String,
) -> Result<()> {
let (project, path) = project_path(&window, &project_manager, path)?;
let project = project(&window, &project_manager)?;

let mut world = project.world.lock().unwrap();
let source_id = world
.slot_update(path.as_path(), Some(content.clone()))
.slot_update(&path, Some(content.clone()))
.map_err(Into::<Error>::into)?;

if !world.is_main_set() {
Expand All @@ -81,7 +80,7 @@ pub async fn typst_compile<R: Runtime>(
}
}

debug!("compiling: {:?}", project);
debug!("compiling {:?}: {:?}", path, project);
let now = Instant::now();
match typst::compile(&*world) {
Ok(doc) => {
Expand Down Expand Up @@ -123,26 +122,25 @@ pub async fn typst_compile<R: Runtime>(
debug!("compilation failed with {:?} errors", errors.len());

let source = world.source(source_id);
let errors: Vec<TypstSourceError> = errors
.iter()
.filter(|e| e.span.source() == source_id)
.map(|e| {
let span = source.range(e.span);
let range = match e.pos {
ErrorPos::Full => span,
ErrorPos::Start => span.start..span.start,
ErrorPos::End => span.end..span.end,
};
let start = content[..range.start].chars().count();
let size = content[range.start..range.end].chars().count();

let message = e.message.to_string();
TypstSourceError {
range: start..start + size,
message,
}
})
.collect();
let errors: Vec<TypstSourceError> = match source {
Ok(source) => errors
.iter()
.filter(|e| e.span.id() == source_id)
.filter_map(|e| {
let span = source.find(e.span)?;
let range = span.range();
let start = content[..range.start].chars().count();
let size = content[range.start..range.end].chars().count();

let message = e.message.to_string();
Some(TypstSourceError {
range: start..start + size,
message,
})
})
.collect(),
Err(_) => vec![],
};

let _ = window.emit(
"typst_compile",
Expand Down Expand Up @@ -216,9 +214,10 @@ pub async fn typst_autocomplete<R: Runtime>(
let source_id = world
.slot_update(&*path, Some(content))
.map_err(Into::<Error>::into)?;
let source = world.source(source_id);

let (offset, completions) = typst::ide::autocomplete(&*world, &[], source, offset, explicit)
let source = world.source(source_id).map_err(Into::<Error>::into)?;

let (offset, completions) = typst::ide::autocomplete(&*world, &[], &source, offset, explicit)
.ok_or_else(|| Error::Unknown)?;

Ok(TypstCompleteResponse {
Expand Down
13 changes: 6 additions & 7 deletions src-tauri/src/project/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use crate::project::ProjectWorld;
use log::debug;
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Formatter};
use std::path::{Path, PathBuf};
use std::path::{Component, Path, PathBuf};
use std::sync::{Mutex, RwLock};
use std::{fs, io};
use thiserror::Error;
use typst::diag::{FileError, FileResult};
use typst::doc::Document;
use typst::util::PathExt;

const PATH_PROJECT_CONFIG_FILE: &str = ".typstudio/project.json";

Expand Down Expand Up @@ -64,10 +63,10 @@ impl ProjectConfig {

pub fn apply_main(&self, project: &Project, world: &mut ProjectWorld) -> FileResult<()> {
if let Some(main) = self.main.as_ref() {
let main = project.root.join(main);
let main = main.canonicalize().unwrap_or_else(|_| main.normalize());
if main.starts_with(&project.root) {
return world.try_set_main(main);
if main.components().next() == Some(Component::RootDir) {
debug!("setting main path {:?} for {:?}", main, project);
world.set_main_path(&main);
return Ok(());
}
}

Expand All @@ -81,7 +80,7 @@ impl ProjectConfig {
impl Default for ProjectConfig {
fn default() -> Self {
Self {
main: Some(PathBuf::from("main.typ")),
main: Some(PathBuf::from("/main.typ")),
}
}
}
Expand Down
Loading

0 comments on commit 7fecd0f

Please sign in to comment.