Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Apr 12, 2024
1 parent 68f23a2 commit 9f03d04
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ical.nimble
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Package

version = "0.1.0"
author = "George Lemon"
description = "A tolerant, minimal iCalendar reader & writer for Nim"
license = "MIT"
srcDir = "src"


# Dependencies

requires "nim >= 2.0.2"
requires "uuid4"
90 changes: 90 additions & 0 deletions src/ical.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# A tolerant, minimal iCalendar reader & writer for Nim.
#
# (c) 2024 George Lemon | MIT license
# Made by Humans from OpenPeeps
# https://github.com/openpeeps/ical

import std/[strutils, tables, times]
import pkg/uuid4

type
CalendarEventStatus* = enum # VEVENT
eventTentative = "TENTATIVE"
eventConfirmed = "CONFIRMED"
eventCanceled = "CANCELED"

CalendarTodoStatus* = enum # VTODO
todoNeedsAction = "NEEDS-ACTION"
todoCompleted = "COMPLETED"
todoInProcess = "IN-PROCESS"
todoCanceled = "CANCELED"

CalendarJournalStatus* = enum # VJOURNAL
journalDraft = "DRAFT"
journalFinal = "FINAL"
journalCanceled = "CANCELED"

CalendarType* = enum
calendarTypeEvent = "VEVENT"
calendarTypeTodo = "VTODO"
calendarTypeJournal = "VJOURNAL"

CalendarEvent* = object
id: Uuid
case calendarType: CalendarType
of calendarTypeEvent:
eventStatus: CalendarEventStatus
eventStartDate, eventEndDate: DateTime
else: discard # todo

Calendar* = object
id: string
locale: string = "EN"
events: OrderedTable[string, CalendarEvent]

const
# https://github.com/peterbraden/ical.js/blob/master/examples/example_rrule.ics
# https://icalendar.org/iCalendar-RFC-5545/4-icalendar-object-examples.html
propProdid = "PRODID:-//$1//$2\n"
propBegin = "BEGIN:$1\n"
propEnd = "END:$1\n"
propVersion = "VERSION:$1\n"
propScale = "CALSCALE:GREGORIAN\n"
propMethod = "METHOD:$1\n"
propUuid = "UID:$1\n"
propDTStart = "DTSTART:$1\n"
propStatus = "STATUS:$1\n"
propDTEnd = "DTEND:$1\n"
propDTStamp = "DTSTAMP:$1\n"
propLastModified = "LAST-MODIFIED:$1\n"

proc initCalendar*(id: string): Calendar =
Calendar(id: id)

proc event*(cal: var Calendar, startDate, endDate: DateTime,
status: CalendarEventStatus = eventConfirmed) =
let id = uuid4()
cal.events[$id] = CalendarEvent(id: id,
eventStartDate: startDate,
eventEndDate: endDate,
eventStatus: status,
calendarType: CalendarType.calendarTypeEvent
)

proc `$`*(cal: Calendar): string =
result = propBegin % ["VCALENDAR"]
add result, propVersion % "2.0"
add result, propProdid % [cal.id, cal.locale]
add result, propMethod % "PUBLISH"
for id, ev in cal.events:
add result, propBegin % $(ev.calendarType)
add result, propDTStamp % format(now(), "yyyyMMdd'T'HHmmss")
add result, propUuid % id
case ev.calendarType
of calendarTypeEvent:
add result, propStatus % $(ev.eventStatus)
add result, propDTStart % format(ev.eventStartDate, "yyyyMMdd")
add result, propDTEND % format(ev.eventEndDate, "yyyyMMdd")
else: discard # todo
add result, propEnd % $(ev.calendarType)
add result, propEnd % ["VCALENDAR"]
12 changes: 12 additions & 0 deletions src/ical/submodule.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This is just an example to get you started. Users of your library will
# import this file by writing ``import ical/submodule``. Feel free to rename or
# remove this file altogether. You may create additional modules alongside
# this file as required.

type
Submodule* = object
name*: string

proc initSubmodule*(): Submodule =
## Initialises a new ``Submodule`` object.
Submodule(name: "Anonymous")
1 change: 1 addition & 0 deletions tests/config.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
switch("path", "$projectDir/../src")
8 changes: 8 additions & 0 deletions tests/test1.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import unittest, times
import ical

test "can add":
var cal = initCalendar("GOODCORP")
cal.event(now() + 1.days, now() + 2.days)
cal.event(now() + 4.days, now() + 10.days)
echo cal

0 comments on commit 9f03d04

Please sign in to comment.